public async Task <ICacheEntry> GetOrAddAsync(string key, Func <string, Stream, ICacheControl, Task> valueFactory, ICacheControl cacheControl, CancellationToken ct) { using (await _rwLock.ReadLockAsync()) { ICacheEntry found = GetInternal(key); if (!(found is EmptyCacheEntry)) { found.Ref(); // this entry remain valid after releasing the lock return(found); } } using (await _rwLock.WriteLockAsync()) { ICacheEntry found = GetInternal(key); if (!(found is EmptyCacheEntry)) { found.Ref(); // this entry remain valid after releasing the lock return(found); } var randomPath = await _fs.WriteAtomicAsync((sink, cancel) => valueFactory(key, sink, cacheControl), _cacheTmpPath, ct).ConfigureAwait(false); var tmpEntry = new DiskCacheEntry(key, randomPath, this); if (!cacheControl.NoStore) { SetInternal(key, tmpEntry); } var entry = GetInternal(key); entry.Ref(); return(entry); } }
public async Task <ICacheEntry> GetAsync(string key, CancellationToken ct) { using (await _rwLock.ReadLockAsync()) { ICacheEntry entry = GetInternal(key); entry.Ref(); return(entry); } }