缓存依赖(文件)
发布时间:2020-05-24 02:02:41 所属栏目:程序设计 来源:互联网
导读:一个文件修改后,缓存也重新加载,缓存依赖(文件) protected void Page_Load(object sender, EventArgs e) { if (Cache[Msg] == null) { string filepath = Server.MapPath(~/CacheHot.txt);
|
一个文件修改后,缓存也重新加载,缓存依赖(文件)
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);
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
