public override void AddOrUpdate(TKey key, TValue value, ICacheExpirationPolicy expirationPolicy, string regionName = null) { _regions.Set(key, new CacheEntry <TKey, TValue>(key, expirationPolicy) { Value = value }, regionName); }
public CachedValue(ICache <TKey, TValue> cache, TKey key, Func <TValue> getValue, ICacheExpirationPolicy expirationPolicy, string regionName = null) { _cachedValueMode = CachedValueMode.GetOrAdd; _cache = cache; _key = key; _getValue = getValue; _expirationPolicy = expirationPolicy; _regionName = regionName; }
public bool TryMerge(ICacheExpirationPolicy toMerge) { var obj = toMerge as SlidingExpirationPolicy; if (obj == null) { return(false); } _toUpdate = obj._toUpdate; return(true); }
public bool TryMerge(ICacheExpirationPolicy toMerge) { var obj = toMerge as AbsoluteExpirationPolicy; if (obj == null) { return(false); } _expireIn = obj._expireIn; return(true); }
public override TValue GetOrAdd(TKey key, Func <TValue> getValue, ICacheExpirationPolicy expirationPolicy, string regionName = null) { if (TryGet(key, out var value, regionName)) { return(value); } var newValue = getValue(); AddOrUpdate(key, newValue, expirationPolicy, regionName); return(newValue); }
public bool TryMerge(ICacheExpirationPolicy toMerge) { if (toMerge == null) { return(true); } foreach (var fileCacheExpirationPolicy in _policies) { if (fileCacheExpirationPolicy.TryMerge(toMerge)) { return(true); } } _policies.Add(toMerge); return(true); }
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); }
public CacheEntry(TKey key, ICacheExpirationPolicy expirationPolicy) { Key = key; ExpirationPolicy = expirationPolicy; _lastEntryCheck = DateTime.Now; }
public abstract Task AddOrUpdateAsync(TKey key, TValue value, ICacheExpirationPolicy expirationPolicy, string regionName = null);
public abstract Task <TValue> GetOrAddAsync(TKey key, Func <TValue> getValue, ICacheExpirationPolicy expirationPolicy, string regionName = null);
public void Dispose() { Item = null; ExpirationPolicy = null; }
public ItemPolicyPair(object item, ICacheExpirationPolicy expirationPolicy) { Item = item; ExpirationPolicy = expirationPolicy; }
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); } } }
public async Task AddOrUpdateStreamAsync(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)) { await EnsureInitializedAsync(token); using (await GlobalReadLock(token)) { await InternalAddOrUpdate(key, cfs, token, policy); } } }
public override Task <TValue> GetOrAddAsync(TKey key, Func <TValue> getValue, ICacheExpirationPolicy expirationPolicy, string regionName = null) { return(Task.FromResult(GetOrAdd(key, getValue, expirationPolicy, regionName))); }
/// <summary> /// Updates policy and registers access. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="policy"></param> /// <param name="otherPolicy"></param> /// <returns></returns> internal static T Pulse <T>(this T policy, ICacheExpirationPolicy otherPolicy) where T : ICacheExpirationPolicy { policy.TryMerge(otherPolicy); policy.LogAccess(DateTime.UtcNow); return(policy); }
private async Task <CFileCacheEntry> InternalGetOrAdd(TKey key, Func <TKey, Task <CFileSource> > provider, CancellationToken token, ICacheExpirationPolicy policy) { CFileCacheEntry cacheEntry; if (_entries.TryGetValue(key, out cacheEntry)) { return(cacheEntry.Pulse(policy)); } //per key mutex required to avoid multiple file upload to cache by same key. //files are very large objects, so unnecessary actions with them should be avoided if possible. using (await _perKeyLock.LockAsync(key, token)) { if (_entries.TryGetValue(key, out cacheEntry)) { return(cacheEntry.Pulse(policy)); } token.ThrowIfCancellationRequested(); //we are responsible for creation and disposing of stream we created, so we wrap src in using directive. using (var src = await provider(key)) { token.ThrowIfCancellationRequested(); var path = await UploadToCacheAsync(src, token); cacheEntry = new CFileCacheEntry() { Created = DateTime.UtcNow, FilePath = path }; //###### _entries[key] = cacheEntry; return(cacheEntry.Pulse(policy)); } } }
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); } } }
public async Task <Stream> GetStreamOrAddFileAsync(TKey key, Func <TKey, Task <string> > provider, CancellationToken token, ICacheExpirationPolicy policy) { NotNull(provider, nameof(provider)); NotNull(key, nameof(key)); await EnsureInitializedAsync(token); using (await GlobalReadLock(token)) { var entry = await InternalGetOrAdd(key, async kk => new CFileSource(await provider(kk), _fs), token, policy); return(await _fs.OpenReadAsync(entry.FilePath, token)); } }
public Stream GetStreamOrAddFile(TKey key, Func <TKey, string> provider, CancellationToken token, ICacheExpirationPolicy policy) { NotNull(provider, nameof(provider)); NotNull(key, nameof(key)); EnsureInitialized(token); using (GlobalReadLock(token)) { var entry = InternalGetOrAdd(key, kk => new CFileSource(provider(kk), _fs), token, policy); return(_fs.OpenRead(entry.FilePath, token)); } }
public async Task AddOrUpdateFileAsync(TKey key, string sourceFilePath, CancellationToken token, ICacheExpirationPolicy policy) { NotNull(sourceFilePath, nameof(sourceFilePath)); NotNull(key, nameof(key)); using (var cfs = new CFileSource(sourceFilePath, _fs)) { await EnsureInitializedAsync(token); using (await GlobalReadLock(token)) { await InternalAddOrUpdate(key, cfs, token, policy); } } }
public override Task AddOrUpdateAsync(TKey key, TValue value, ICacheExpirationPolicy expirationPolicy, string regionName = null) { AddOrUpdate(key, value, expirationPolicy, regionName); return(Task.CompletedTask); }
public void Put(object key, object item, ICacheExpirationPolicy expirationPolicy) { expirationPolicy.SetProvider(this); Cache[key] = new ItemPolicyPair(item, expirationPolicy); expirationPolicy.ItemAdded(key, item); }