public async Task GetFileOrAddStreamAsync(TKey key, Func <TKey, Task <Stream> > 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), false, _fs), token, policy); using (var cfs = new CFileSource(entry.FilePath, _fs)) { await cfs.CopyToAsync(targetFilePath, token, true); } } }
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; } }
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); } }