コード例 #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);
 }
コード例 #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;
        }
コード例 #3
0
        public bool TryMerge(ICacheExpirationPolicy toMerge)
        {
            var obj = toMerge as SlidingExpirationPolicy;

            if (obj == null)
            {
                return(false);
            }
            _toUpdate = obj._toUpdate;
            return(true);
        }
コード例 #4
0
        public bool TryMerge(ICacheExpirationPolicy toMerge)
        {
            var obj = toMerge as AbsoluteExpirationPolicy;

            if (obj == null)
            {
                return(false);
            }
            _expireIn = obj._expireIn;
            return(true);
        }
コード例 #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);
        }
コード例 #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);
        }
コード例 #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);
        }
コード例 #8
0
 public CacheEntry(TKey key, ICacheExpirationPolicy expirationPolicy)
 {
     Key = key;
     ExpirationPolicy = expirationPolicy;
     _lastEntryCheck  = DateTime.Now;
 }
コード例 #9
0
ファイル: CacheBase.cs プロジェクト: CreativeCodersTeam/Core
 public abstract Task AddOrUpdateAsync(TKey key, TValue value, ICacheExpirationPolicy expirationPolicy, string regionName = null);
コード例 #10
0
ファイル: CacheBase.cs プロジェクト: CreativeCodersTeam/Core
 public abstract Task <TValue> GetOrAddAsync(TKey key, Func <TValue> getValue,
                                             ICacheExpirationPolicy expirationPolicy, string regionName = null);
コード例 #11
0
 public void Dispose()
 {
     Item             = null;
     ExpirationPolicy = null;
 }
コード例 #12
0
ファイル: ItemPolicyPair.cs プロジェクト: damonrpayne/Shinto
 public ItemPolicyPair(object item, ICacheExpirationPolicy expirationPolicy)
 {
     Item = item;
     ExpirationPolicy = expirationPolicy;
 }
コード例 #13
0
ファイル: FileCache.cs プロジェクト: eocron/Algorithm
        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);
                }
            }
        }
コード例 #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);
                }
            }
        }
コード例 #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)));
 }
コード例 #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);
 }
コード例 #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));
                }
            }
        }
コード例 #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);
                }
            }
        }
コード例 #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));
            }
        }
コード例 #20
0
ファイル: FileCache.cs プロジェクト: eocron/Algorithm
        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));
            }
        }
コード例 #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);
                }
            }
        }
コード例 #22
0
        public override Task AddOrUpdateAsync(TKey key, TValue value, ICacheExpirationPolicy expirationPolicy, string regionName = null)
        {
            AddOrUpdate(key, value, expirationPolicy, regionName);

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