/// <summary> /// 获取缓存键值下的数据字典 /// </summary> /// <returns></returns> public Dictionary <TKey, CacheObject <TValue> > GetCachedEntities() { Dictionary <TKey, CacheObject <TValue> > cachedEntities = CacheOperate.Get(cacheKey) as Dictionary <TKey, CacheObject <TValue> >; if (cachedEntities == null) { cachedEntities = new Dictionary <TKey, CacheObject <TValue> >(); CacheOperate.Insert(cacheKey, cachedEntities); } return(cachedEntities); }
/// <summary> /// 获取缓存,根据周期自动延长缓存 /// </summary> /// <param name="key"></param> /// <returns></returns> public TValue Get(TKey key, AutoCache autocache) { Dictionary <TKey, CacheObject <TValue> > cachedEntities = GetCachedEntities(); CacheObject <TValue> cachedEntity; if (cachedEntities.ContainsKey(key)) { cachedEntity = cachedEntities[key]; if (autocache == AutoCache.Auto) { if (CheckCycle(cachedEntity.CreateTime)) { CacheOperate.Insert(cacheKey, CacheMinutes, null, CacheMinutes, System.Web.Caching.CacheItemPriority.Default); } } return(cachedEntity.Object); } return(default(TValue)); }
/// <summary> /// 缓存数据 /// </summary> /// <param name="key">键值</param> /// <param name="entity">缓存对象</param> /// <param name="cachedp">缓存依赖项</param> /// <param name="priority">缓存清理优先级</param> public void Cache(TKey key, TValue entity, System.Web.Caching.CacheDependency cachedp, System.Web.Caching.CacheItemPriority priority) { if (entity == null) { return; } Dictionary <TKey, CacheObject <TValue> > cachedEntities = GetCachedEntities(); lock (cachedEntities) { CacheObject <TValue> cachedEntity = new CacheObject <TValue>(entity); if (cachedEntities.ContainsKey(key)) { cachedEntities[key] = cachedEntity; } else { cachedEntities.Add(key, cachedEntity); } CacheOperate.Insert(cacheKey, cachedEntities, cachedp, CacheMinutes, priority); } }
/// <summary> /// 移除缓存键 /// </summary> public void Remove() { CacheOperate.Remove(cacheKey); }