Ejemplo n.º 1
0
 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);
 }
Ejemplo n.º 2
0
        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;
        }
Ejemplo n.º 3
0
        public bool TryMerge(ICacheExpirationPolicy toMerge)
        {
            var obj = toMerge as SlidingExpirationPolicy;

            if (obj == null)
            {
                return(false);
            }
            _toUpdate = obj._toUpdate;
            return(true);
        }
Ejemplo n.º 4
0
        public bool TryMerge(ICacheExpirationPolicy toMerge)
        {
            var obj = toMerge as AbsoluteExpirationPolicy;

            if (obj == null)
            {
                return(false);
            }
            _expireIn = obj._expireIn;
            return(true);
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
 public CacheEntry(TKey key, ICacheExpirationPolicy expirationPolicy)
 {
     Key = key;
     ExpirationPolicy = expirationPolicy;
     _lastEntryCheck  = DateTime.Now;
 }
Ejemplo n.º 9
0
 public abstract Task AddOrUpdateAsync(TKey key, TValue value, ICacheExpirationPolicy expirationPolicy, string regionName = null);
Ejemplo n.º 10
0
 public abstract Task <TValue> GetOrAddAsync(TKey key, Func <TValue> getValue,
                                             ICacheExpirationPolicy expirationPolicy, string regionName = null);
Ejemplo n.º 11
0
 public void Dispose()
 {
     Item             = null;
     ExpirationPolicy = null;
 }
Ejemplo n.º 12
0
 public ItemPolicyPair(object item, ICacheExpirationPolicy expirationPolicy)
 {
     Item = item;
     ExpirationPolicy = expirationPolicy;
 }
Ejemplo n.º 13
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);
                }
            }
        }
Ejemplo n.º 14
0
        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);
                }
            }
        }
Ejemplo n.º 15
0
 public override Task <TValue> GetOrAddAsync(TKey key, Func <TValue> getValue, ICacheExpirationPolicy expirationPolicy, string regionName = null)
 {
     return(Task.FromResult(GetOrAdd(key, getValue, expirationPolicy, regionName)));
 }
Ejemplo n.º 16
0
 /// <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);
 }
Ejemplo n.º 17
0
        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));
                }
            }
        }
Ejemplo n.º 18
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);
                }
            }
        }
Ejemplo n.º 19
0
        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));
            }
        }
Ejemplo n.º 20
0
        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));
            }
        }
Ejemplo n.º 21
0
        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);
                }
            }
        }
Ejemplo n.º 22
0
        public override Task AddOrUpdateAsync(TKey key, TValue value, ICacheExpirationPolicy expirationPolicy, string regionName = null)
        {
            AddOrUpdate(key, value, expirationPolicy, regionName);

            return(Task.CompletedTask);
        }
Ejemplo n.º 23
0
 public ItemPolicyPair(object item, ICacheExpirationPolicy expirationPolicy)
 {
     Item             = item;
     ExpirationPolicy = expirationPolicy;
 }
Ejemplo n.º 24
0
 public void Put(object key, object item, ICacheExpirationPolicy expirationPolicy)
 {
     expirationPolicy.SetProvider(this);
     Cache[key] = new ItemPolicyPair(item, expirationPolicy);
     expirationPolicy.ItemAdded(key, item);
 }