protected void Page_Load(object sender,EventArgs e) { if (Cache["Msg"] == null) { string filepath = Server.MapPath("~/CacheHot.txt"); string content = System.IO.File.ReadAllText(filepath); System.Web.Caching.CacheDependency dency = new System.Web.Caching.CacheDependency(filepath); //Cache.Insert("Msg",content,dency,System.Web.Caching.Cache.NoAbsoluteExpiration,System.Web.Caching.Cache.NoSlidingExpiration,new System.Web.Caching.CacheItemRemovedCallback(callback)); //键,值,依赖对象,绝对过期,滑动过期,优先级,当删除时,回调函数 Cache.Insert("Msg",DateTime.Now.ToString(),CacheItemPriority.Normal,callback); } else { Response.Write(Cache["Msg"].ToString()); } } public void callback(string key,object value,CacheItemRemovedReason reason) { string filepath = Server.MapPath("~/CacheLog.txt"); string content = "Cache["+key+"]="+value.ToString()+"被删除 了"+reason.ToString(); System.IO.File.AppendAllText(filepath,content); } }原文链接:https://www.f2er.com/javaschema/286935.html