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> public static void SetCaChe(string CaCheKey, object objValue) { System.Web.Caching.Cache ca = HttpRuntime.Cache; ca.Insert(CaCheKey, objValue); } #endregion #region 设置当前应用程序中指定的缓存项 重载 /// <summary> /// 设置当前应用程序中指定的缓存项 重载 /// </summary> /// <param name="CaCheKey">索引键值</param> /// <param name="objValue">缓存对象</param> /// <param name="ab">绝对过期时间</param> /// <param name="sp">最后一次访问所插入对象时与该对象过期时之间的时间间隔<</param> public static void SetCaChe(string CaCheKey, object objValue, DateTime ab, TimeSpan sp) { System.Web.Caching.Cache ca = HttpRuntime.Cache; ca.Insert(CaCheKey, objValue, null, ab, sp); } #endregion
页面调用代码:
string CaCheKey = "cachekey"; object objModel = CaCheHelp.GetCaChe(CaCheKey); if (objModel == null) { objModel = DateTime.Now; //缓存对象:当前系统时间 if (objModel != null) { int CaCheTime = 5; //五秒 CaCheHelp.SetCaChe(CaCheKey, objModel, DateTime.Now.AddSeconds(CaCheTime), TimeSpan.Zero); } } lable_time.InnerText = objModel.ToString();