public CacheEntry(AsyncCache <TKey, TState, TValue> cache, ICacheValue <TKey, TValue> value) { Contracts.Requires.That(cache != null); Contracts.Requires.That(value != null); this.cache = cache; this.value = value; }
protected virtual void InvalidateUnlocked(ICacheValue value) { if (value != null) { this.ValueCacheById.Remove(value.Value.Id); this.ValueCacheByKey.Remove(value.Value.Key); this.CacheValueInvalidated(value); } }
/// <summary> /// 获取缓存中的值 /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="key">键</param> /// <returns></returns> public T GetValue <T>(string key) { ICacheValue cache = GetCache(key); if (cache == null) { return(default(T)); } return((T)cache.Value); }
protected virtual ICacheValue CheckValueExpiration(ICacheValue value) { var maxAge = this.MaxEntryAge; if (maxAge != null && value != null && (((CacheValue)value).CreatedAt + maxAge.Value) < DateTime.Now) { // remove elemnt on access if cache value expired this.InvalidateUnlocked(value); value = null; } return(value); }
protected override void CacheValueInvalidated(ICacheValue cacheValue) { var value = (CacheValue)cacheValue; if (value.IndexRef != null) { this.indexList.Remove(value.IndexRef); } if (value.IndexTimeRef != null) { this.indexTimeList.Remove(value.IndexTimeRef); } }
public bool TryGetValue(string key, out TValue value) { value = default(TValue); lock (this.SyncRoot) { ICacheValue v = this.GetCacheValueUnlocked(key); if (v != null) { value = v.Value; this.UpdateElementAccess(v); return(true); } } return(false); }
/// <summary> /// 根据key移除缓存中的值 /// </summary> /// <param name="key"></param> public void Remove(string key) { ICacheValue cache = GetCache(key); if (cache == null) { return; } if (!_lock.TryEnterWriteLock(50)) { return; } try { cache.Dispose(); _cache.Remove(key); } finally { _lock.ExitWriteLock(); } }
protected virtual ICacheValue AddValueUnlocked(TValue value) { ICacheValue cacheValue = this.GetCacheValueUnlocked(value.Id); if (cacheValue == null) { ICacheValue cacheValueByKey = this.GetCacheValueUnlocked(value.Key); if (cacheValueByKey != null) { throw new ExBadCacheKeyException(); } cacheValue = this.CreateCacheValue(value); this.ValueCacheById[value.Id] = cacheValue; this.ValueCacheByKey[value.Key] = cacheValue; } else { cacheValue.Value = value; } this.UpdateElementAccess(cacheValue); return(cacheValue); }
/// <summary> /// 添加至缓存 /// </summary> /// <param name="key"></param> /// <param name="icv"></param> public Boolean AddCache(string key, ICacheValue icv) { if (!string.IsNullOrEmpty(key) && _lock.TryEnterWriteLock(50)) { try { if (_cache.ContainsKey(key)) { _cache[key] = icv; } else { _cache.Add(key, icv); } return(true); } finally { _lock.ExitWriteLock(); } } return(false); }
protected override void UpdateElementAccess(ICacheValue cacheValue) { var value = (CacheValue)cacheValue; // put element at front of the index list // remove first if already present in list, create new otherwise var idxRef = value.IndexRef; if (idxRef != null) { this.indexList.Remove(idxRef); } else { idxRef = new LinkedListNode <KeyValuePair <int, CacheValue> >(new KeyValuePair <int, CacheValue>(cacheValue.Value.Id, value)); value.IndexRef = idxRef; } this.indexList.AddFirst(idxRef); // remove all entries from end of list until max size is satisfied while (this.indexList.Count > this.MaxSize) { this.InvalidateUnlocked(this.indexList.Last.Value.Value); } }
protected abstract void CacheValueInvalidated(ICacheValue cacheValue);
protected abstract void UpdateElementAccess(ICacheValue cacheValue);
public static void AddExpiration <TKey, TValue>(ICacheValue <TKey, TValue> instance) { Contracts.Requires.That(instance != null); Contracts.Requires.That(!instance.IsDisposed); }
protected override void CacheValueInvalidated(ICacheValue cacheValue) { this.indexList.Remove(((CacheValue)cacheValue).IndexRef); }
protected override void UpdateElementAccess(ICacheValue cacheValue) { var value = (CacheValue)cacheValue; if (this.MaxEntryAge != null) { var idxTimeRef = value.IndexTimeRef; if (idxTimeRef == null) { idxTimeRef = new LinkedListNode <KeyValuePair <int, CacheValue> >(new KeyValuePair <int, CacheValue>(cacheValue.Value.Id, value)); value.IndexTimeRef = idxTimeRef; this.indexTimeList.AddFirst(idxTimeRef); } } if (this.MaxSize > 0) { // put element at front of the index list // remove first if already present in list, create new otherwise var idxRef = value.IndexRef; if (idxRef != null) { this.indexList.Remove(idxRef); } else { idxRef = new LinkedListNode <KeyValuePair <int, CacheValue> >(new KeyValuePair <int, CacheValue>(cacheValue.Value.Id, value)); value.IndexRef = idxRef; } this.indexList.AddFirst(idxRef); if (this.MaxEntryAge != null && this.indexTimeList.Count > 0) { var maxAge = this.MaxEntryAge; LinkedListNode <KeyValuePair <int, CacheValue> > node; while ((node = this.indexTimeList.Last) != null && node.Value.Value.CreatedAt + maxAge < DateTime.Now) { this.InvalidateUnlocked(this.indexList.Last.Value.Value); } } // remove all entries from end of list until max size is satisfied while (this.indexList.Count > this.MaxSize) { this.InvalidateUnlocked(this.indexList.Last.Value.Value); } } if (this.MaxAccessCount > 0) { value.AccessCount++; // remove element if max access count limit exceeded if (value.AccessCount > this.MaxAccessCount) { this.InvalidateUnlocked(value); } } }