Пример #1
0
        private async Task InternalAddOrUpdate(TKey key, CFileSource src, CancellationToken token, ICacheExpirationPolicy policy)
        {
            //at this point we are not responsible of disposing 'src' cause it's lifetime is wider than this method
            var path = await UploadToCacheAsync(src, token);

            _entries[key] = new CFileCacheEntry()
            {
                Created  = DateTime.UtcNow,
                FilePath = path
            }.Pulse(policy);
        }
Пример #2
0
 public void AddOrUpdateFile(TKey key, string sourceFilePath, CancellationToken token, ICacheExpirationPolicy policy)
 {
     NotNull(sourceFilePath, nameof(sourceFilePath));
     NotNull(key, nameof(key));
     using (var cfs = new CFileSource(sourceFilePath, _fs))
     {
         EnsureInitialized(token);
         using (GlobalReadLock(token))
         {
             InternalAddOrUpdate(key, cfs, token, policy);
         }
     }
 }
Пример #3
0
        public void AddOrUpdateStream(TKey key, Stream stream, CancellationToken token, ICacheExpirationPolicy policy, bool leaveOpen = false)
        {
            NotNull(stream, nameof(stream));
            NotNull(key, nameof(key));

            using (var cfs = new CFileSource(stream, leaveOpen, _fs))
            {
                EnsureInitialized(token);
                using (GlobalReadLock(token))
                {
                    InternalAddOrUpdate(key, cfs, token, policy);
                }
            }
        }
Пример #4
0
        public void GetFileOrAddFile(TKey key, Func <TKey, string> provider, CancellationToken token, string targetFilePath, ICacheExpirationPolicy policy)
        {
            NotNull(provider, nameof(provider));
            NotNull(key, nameof(key));
            NotNull(targetFilePath, nameof(targetFilePath));

            EnsureInitialized(token);
            using (GlobalReadLock(token))
            {
                var entry = InternalGetOrAdd(key, kk => new CFileSource(provider(kk), _fs), token, policy);
                using (var cfs = new CFileSource(entry.FilePath, _fs))
                {
                    cfs.CopyTo(targetFilePath, token, true);
                }
            }
        }
Пример #5
0
        public async Task GetFileOrAddFileAsync(TKey key, Func <TKey, Task <string> > provider, CancellationToken token, string targetFilePath, ICacheExpirationPolicy policy)
        {
            NotNull(provider, nameof(provider));
            NotNull(key, nameof(key));
            NotNull(targetFilePath, nameof(targetFilePath));

            await EnsureInitializedAsync(token);

            using (await GlobalReadLock(token))
            {
                var entry = await InternalGetOrAdd(key, async kk => new CFileSource(await provider(kk), _fs), token, policy);

                using (var cfs = new CFileSource(entry.FilePath, _fs))
                {
                    await cfs.CopyToAsync(targetFilePath, token, true);
                }
            }
        }
Пример #6
0
        private string UploadToCache(CFileSource src, CancellationToken token)
        {
            token.ThrowIfCancellationRequested();
            var path = GetTmpUploadFilePath(token);

            try
            {
                src.CopyTo(path, token, false);
                token.ThrowIfCancellationRequested();
                return(MoveToCache(path, token));
            }
            catch
            {
                if (_fs.FileExist(path, token))
                {
                    MoveToTrash(path, token);
                }
                throw;
            }
        }
Пример #7
0
        private async Task <string> UploadToCacheAsync(CFileSource src, CancellationToken token)
        {
            token.ThrowIfCancellationRequested();
            var path = await GetTmpUploadFilePath(token);

            try
            {
                await src.CopyToAsync(path, token, false);

                token.ThrowIfCancellationRequested();
                return(await MoveToCacheAsync(path, token));
            }
            catch
            {
                if (await _fs.FileExistAsync(path, token))
                {
                    await MoveToTrashAsync(path, token);
                }
                throw;
            }
        }
Пример #8
0
        public bool TryGetFile(TKey key, CancellationToken token, string targetFilePath)
        {
            NotNull(key, nameof(key));
            NotNull(targetFilePath, nameof(targetFilePath));

            EnsureInitialized(token);
            using (GlobalReadLock(token))
            {
                var entry = InternalGetEntry(key, token);
                if (entry == null)
                {
                    return(false);
                }

                using (var cfs = new CFileSource(entry.FilePath, _fs))
                {
                    cfs.CopyTo(targetFilePath, token, true);
                }
                return(true);
            }
        }
Пример #9
0
        public async Task <bool> TryGetFileAsync(TKey key, CancellationToken token, string targetFilePath)
        {
            NotNull(key, nameof(key));
            NotNull(targetFilePath, nameof(targetFilePath));

            await EnsureInitializedAsync(token);

            using (await GlobalReadLock(token))
            {
                var entry = await InternalGetEntry(key, token);

                if (entry == null)
                {
                    return(false);
                }

                using (var cfs = new CFileSource(entry.FilePath, _fs))
                {
                    await cfs.CopyToAsync(targetFilePath, token, true);
                }
                return(true);
            }
        }