/// <summary> /// 是否存在缓存 /// </summary> /// <param name="key">标识</param> /// <returns></returns> public override bool Contains(string key) { if (theState.ContainsKey(key)) { CacheDependencyInfo info = theState[key]; bool isOutTime = info.createTime.AddMinutes(info.cacheMinutes) < DateTime.Now; if (!isOutTime && theCache[key] != null) { return(true); } else { lock (lockObj) { try { theState.Remove(key); if (isOutTime) { theCache.Remove(key); } } catch { //errorCount++; } } } } return(false); }
/// <summary> /// 获取目标的文件依赖是否发生更改 /// </summary> /// <param name="key"></param> /// <returns></returns> public override bool GetFileDependencyHasChanged(string key) { if (Contains(key)) { CacheDependencyInfo info = theState[key]; if (info != null) { return(info.IsChanged); } } return(true); }
/// <summary> /// 手动对缓存象标识为已更改 /// </summary> public override void SetChange(string key, bool isChange) { if (Contains(key)) { CacheDependencyInfo info = theState[key]; if (info != null) { info.SetState(isChange); } } else { Add("Cache:Temp_" + key, isChange, null, 0.1);//缓存失效时,产生6秒的key缓冲 } }
/// <summary> /// 相对底层Cache添加方法,添加一个Cache请用Add方法 /// </summary> /// <param name="key">标识</param> /// <param name="value">对象值</param> /// <param name="fileName">依赖文件</param> /// <param name="cacheMinutes">缓存分钟数</param> /// <param name="level" >缓存级别</param> private void Insert(string key, object value, string fileName, double cacheMinutes, CacheItemPriority level) { CacheDependency theCacheDependency = null; if (!string.IsNullOrEmpty(fileName)) { theCacheDependency = new CacheDependency(fileName); } double cacheTime = cacheMinutes; if (cacheMinutes <= 0) { cacheTime = AppConfig.Cache.DefaultCacheTime; } DateTime cTime = DateTime.Now.AddMinutes(cacheTime); theCache.Insert(key, value == null ? string.Empty : value, theCacheDependency, cTime, TimeSpan.Zero, level, null); CacheDependencyInfo info = new CacheDependencyInfo(theCacheDependency, cacheTime); lock (lockObj) { try { if (theState.ContainsKey(key)) { theState[key] = info; } else { theState.Add(key, info); } TimeSpan ts = cTime - startTime; int workCount = (int)ts.TotalMinutes / taskInterval;//计算出离开始有多少个间隔时间。 if (keyTime.ContainsKey(workCount)) { keyTime[workCount].Append("," + key); } else { keyTime.Add(workCount, new StringBuilder(key)); } } catch { } } getCacheTableTime = DateTime.Now.AddMinutes(-5);//设置缓存表过期。 }
/// <summary> /// 获取缓存对象是否被手工标识为已更改 /// </summary> /// <param name="key"></param> /// <returns></returns> public override bool GetHasChanged(string key) { if (Contains(key)) { CacheDependencyInfo info = theState[key]; if (info != null) { return(info.IsChanged ? false : info.UserChange); } } else if (Contains("Cache:Temp_" + key)) { return((bool)theCache.Get("Cache:Temp_" + key)); } return(false); }
/// <summary> /// ��Եײ�Cache��ӷ���,���һ��Cache����Add���� /// </summary> /// <param name="key">��ʶ</param> /// <param name="value">����ֵ</param> /// <param name="fileName">�����ļ�</param> /// <param name="cacheMinutes">���������</param> /// <param name="level" >���漶��</param> private void Insert(string key, object value, string fileName, double cacheMinutes, CacheItemPriority level) { CacheDependency theCacheDependency = null; if (!string.IsNullOrEmpty(fileName)) { theCacheDependency = new CacheDependency(fileName); } double cacheTime = cacheMinutes; if (cacheMinutes <= 0) { cacheTime = AppConfig.Cache.DefaultCacheTime; } DateTime cTime = DateTime.Now.AddMinutes(cacheTime); theCache.Insert(key, value == null ? string.Empty : value, theCacheDependency, cTime, TimeSpan.Zero, level, null); CacheDependencyInfo info = new CacheDependencyInfo(theCacheDependency, cacheTime); lock (lockObj) { try { if (theState.ContainsKey(key)) { theState[key] = info; } else { theState.Add(key, info); } TimeSpan ts = cTime - startTime; int workCount = (int)ts.TotalMinutes / taskInterval;//������뿪ʼ�ж��ٸ����ʱ�䡣 if (keyTime.ContainsKey(workCount)) { keyTime[workCount].Append("," + key); } else { keyTime.Add(workCount, new StringBuilder(key)); } } catch { } } getCacheTableTime = DateTime.Now.AddMinutes(-5);//���û������ڡ� }