加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

缓存依赖(文件)

发布时间:2020-12-13 20:13:13 所属栏目:百科 来源:网络整理
导读:一个文件修改后,缓存也重新加载,缓存依赖(文件) 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); Sys

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


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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读