缓存依赖(文件)

前端之家收集整理的这篇文章主要介绍了缓存依赖(文件)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一个文件修改后,缓存也重新加载,缓存依赖(文件)


 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

猜你在找的设计模式相关文章