/// <summary> /// 获取当前应用程序指定CacheKey的Cache对象值 /// </summary> /// <param name="CacheKey">索引键值</param> /// <returns>返回缓存对象</returns> public static object GetCache(string CacheKey) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; return objCache[CacheKey]; } /// <summary> /// 设置以缓存依赖的方式缓存数据 /// </summary> /// <param name="CacheKey">索引键值</param> /// <param name="objObject">缓存对象</param> /// <param name="cacheDepen">依赖对象</param> public static void SetCache(string CacheKey,object objObject,System.Web.Caching.CacheDependency dep) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; objCache.Insert( CacheKey, objObject, dep, System.Web.Caching.Cache.NoAbsoluteExpiration,//从不过期 System.Web.Caching.Cache.NoSlidingExpiration,//禁用可调过期 System.Web.Caching.CacheItemPriority.Default, null); } protected void Page_Load(object sender,EventArgs e) { string CacheKey = "cachetest"; object objModel = GetCache(CacheKey);//从缓存中获取 if (objModel == null) //缓存里没有 { objModel = DateTime.Now;//把当前时间进行缓存 if (objModel != null) { //依赖 C://test.txt 文件的变化来更新缓存 System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency("C://test.txt"); SetCache(CacheKey,objModel,dep);//写入缓存 } } Label1.Text = objModel.ToString(); } |