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;
            }
Пример #2
0
 protected virtual void InvalidateUnlocked(ICacheValue value)
 {
     if (value != null)
     {
         this.ValueCacheById.Remove(value.Value.Id);
         this.ValueCacheByKey.Remove(value.Value.Key);
         this.CacheValueInvalidated(value);
     }
 }
Пример #3
0
        /// <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);
        }
Пример #4
0
        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);
        }
Пример #5
0
        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);
            }
        }
Пример #6
0
        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);
        }
Пример #7
0
        /// <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();
            }
        }
Пример #8
0
        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);
        }
Пример #9
0
 /// <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);
 }
Пример #10
0
        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);
            }
        }
Пример #11
0
 protected abstract void CacheValueInvalidated(ICacheValue cacheValue);
Пример #12
0
 protected abstract void UpdateElementAccess(ICacheValue cacheValue);
Пример #13
0
 public static void AddExpiration <TKey, TValue>(ICacheValue <TKey, TValue> instance)
 {
     Contracts.Requires.That(instance != null);
     Contracts.Requires.That(!instance.IsDisposed);
 }
Пример #14
0
 protected override void CacheValueInvalidated(ICacheValue cacheValue)
 {
     this.indexList.Remove(((CacheValue)cacheValue).IndexRef);
 }
Пример #15
0
        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);
                }
            }
        }