zoukankan      html  css  js  c++  java
  • 缓存使用:记录缓存何时被移除

    有些时候我们需要知道缓存被更新的频率,为此探究一下缓存的相关事件(CacheItemRemoveCallback)。下面是一个使用示例:
    页面代码
            const string Key = "anystringtest";
            
    const string path = @"/csdata/siteuser.xml";
            
    private void Bind() {
                System.Web.Caching.Cache ocache 
    = HttpRuntime.Cache;

                SiteUser user 
    = ocache.Get(Key) as SiteUser;
                
    if (user == null)
                {
                    user 
    = new SiteUser();
                    user 
    = user.DeSerializeFromXML(typeof(SiteUser), Server.MapPath(path)) as SiteUser;

                    CacheDependency depend 
    = new CacheDependency(
                        Server.MapPath(path));
                    TimeSpan ts 
    = TimeSpan.Zero;
                    ocache.Insert(Key, user, depend,
                            DateTime.Now.AddMinutes(
    3), ts, CacheItemPriority.Default,
                            
    /***** 使用匿名委托 *****/
                            (
    string key, object obj, CacheItemRemovedReason reason) => {
                                
    string content = string.Format("{0} -  缓存移除. key={1}\r\n",
                                    DateTime.Now, key);
                                file.Write(Server.MapPath(
    "/csdata/log.txt"), content);
                            }
                            );
                    lbldata.Text 
    = "from cache";

                }
                lbluser.Text 
    = user.NickName;
            }



  • 相关阅读:
    csrf 跨站请求伪造
    apache 根据端口访问配置
    对于表单中单双引号问题
    svn常用命令
    urlencode和rawurlencode的区别
    php 显示某一个时间相当于当前时间在多少秒前,多少分钟前,多少小时前
    javaweb基础(24)_jsp一般的标签开发
    javaweb基础(23)_jsp自定义标签
    javaweb基础(22)_Servlet+JSP+JavaBean实战登陆
    javaweb基础(21)_两种开发模式
  • 原文地址:https://www.cnblogs.com/infozero/p/1623519.html
Copyright © 2011-2022 走看看