示例#1
0
        /// <summary>
        /// 注册一个新缓存项
        /// </summary>
        /// <param name="item">缓存项</param>
        /// <returns>是否注册成功</returns>
        public bool Add(string key, TimeSpan expire, bool isSlidingExpiration, RefreshLocalCacheItem refresh, RefreshLocalCacheItemFail refreshFail)
        {
            LocalCacheItem item = new LocalCacheItem()
            {
                Key    = key,
                Value  = null,
                Expire = expire,
                IsSlidingExpiration = isSlidingExpiration,
                RefreshTime         = DateTime.MinValue,
                Refresh             = refresh,
                RefreshFail         = refreshFail
            };

            using (Lock.WriteLock)
            {
                if (this.CacheItems.ContainsKey(item.Key))
                {
                    if (IsAddOverWriteSameKeyValue)
                    {
                        this.CacheItems[item.Key] = item;
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    this.CacheItems.Add(item.Key, item);
                }
            }
            return(true);
        }
示例#2
0
        /// <summary>
        /// 获取缓存项
        /// </summary>
        /// <param name="key">缓存项键</param>
        /// <returns></returns>
        public LocalCacheItem GetCacheItem(string key)
        {
            DateTime       nowTime = DateTime.Now;
            LocalCacheItem item    = null;

            using (Lock.ReadLock)
            {
                if (CacheItems.TryGetValue(key, out item) && item != null) //缓存项存在的情况
                {
                    if (item.RefreshTime.Add(item.Expire) < nowTime)       //过期了
                    {
                        object    newValue  = null;
                        Exception exception = null;
                        try
                        {
                            newValue = item.Refresh(item.Key);
                        }
                        catch (Exception ex)
                        {
                            exception = ex;
                            newValue  = null;
                        }

                        if (newValue != null)
                        {
                            item.Value       = newValue;
                            item.RefreshTime = nowTime;
                        }
                        else
                        {
                            if (item.RefreshFail != null)
                            {
                                if (!item.RefreshFail(item, exception))
                                {
                                    return(null);
                                }
                            }
                            else if (!ContinueUseLocalCacheWhenRefeshFail)
                            {
                                return(null);
                            }
                        }
                    }
                    else//没过期
                    {
                        if (item.IsSlidingExpiration)//滑动过期
                        {
                            item.RefreshTime = nowTime;
                        }
                    }
                }
                else//缓存项根本就没有
                {
                    return(null);
                }
            }
            return(item);
        }
示例#3
0
        /// <summary>
        /// 获取缓存项值
        /// </summary>
        /// <param name="key">缓存项键</param>
        /// <returns></returns>
        public object Get(string key)
        {
            LocalCacheItem item = GetCacheItem(key);

            if (item != null)
            {
                return(item.Value);
            }

            return(null);
        }