微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

缓存依赖(文件)

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


 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);
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐