CaCheHelp 类中代码如下:
#region 根据键从缓存中读取保持的数据 /// <summary> /// 根据键从缓存中读取保持的数据 /// </summary> /// <param name="CaCheKey">索引键值</param> /// <returns></returns> public static object GetCaChe(string CaCheKey) { System.Web.Caching.Cache ca = HttpRuntime.Cache; return ca[CaCheKey]; } #endregion #region 设置当前应用程序中指定的缓存项 重载 设置依赖项 /// <summary> /// 设置当前应用程序中指定的缓存项 重载 设置依赖项 /// </summary> /// <param name="CaCheKey">索引键值</param> /// <param name="objValue">缓存对象</param> /// <param name="dep">依赖对象</param> public static void SetCaChe(string CaCheKey, object objValue, System.Web.Caching.CacheDependency dep) { System.Web.Caching.Cache ca = HttpRuntime.Cache; ca.Insert(CaCheKey, objValue, dep,System.Web.Caching.Cache.NoAbsoluteExpiration,System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default,null); } #endregion
页面调用:
string CaCheKey = "cachekey"; object objModel = CaCheHelp.GetCaChe(CaCheKey); if (objModel == null) { objModel = DateTime.Now; if (objModel != null) { System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency("D:\CacheTest.txt");//依赖D盘下CacheTest.txt内容改变时更新缓存 CaCheHelp.SetCaChe(CaCheKey, objModel, dep); } } lable_time.InnerText = objModel.ToString();