Exemple #1
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);
                }
            }
        }
Exemple #2
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;
            }
        }
Exemple #3
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);
            }
        }