zoukankan      html  css  js  c++  java
  • ASP.NET如何接收清楚缓存的通知

    1 如果使用cache的add方法或者接受CacheItemPriority值得Insert方法重载接收通知,可以提供CacheItemRemovedCallBack对象,选择在清除对象时接收通知

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Caching;
    
    namespace Cachting
    {
        public partial class Default : System.Web.UI.Page
        {
            private static readonly string CACHE_KEY = "codebehind_ts";
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            public string GetTime()
            {
                string ts = (string)Cache[CACHE_KEY];
                if (ts==null)
                {
                    Cache[CACHE_KEY]=ts= DateTime.Now.ToLongTimeString();
                    Cache.Insert(CACHE_KEY, ts, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(10), 
                        System.Web.Caching.CacheItemPriority.Normal, HandleRemoveNotification);
                }
                else
                {
                    ts += "<b>(Cached)</b>";
                }
                return ts;
            }
    
            private void HandleRemoveNotification(string key, object value, CacheItemRemovedReason reason)
            {
                string cacheData = (string)Cache[CACHE_KEY];
                cacheData = cacheData + "10 秒之后的清楚缓存数据的通知";
                Response.Write("<Script Language='JavaScript'>window.alert('" + cacheData.ToString() + "');</script>");
            }
        }
    }

    示例代码如上

  • 相关阅读:
    图与链表的深拷贝
    Codeforces Round #686(Div.3) [C- Sequence
    前缀和
    递归改非递归
    STL源码剖析笔记
    第六章 进程
    C++ 设计模式--模板模式、策略模式、观察者模式
    宏定义方式 进行枚举类型和枚举类型的相互转换
    Linux常见信号介绍
    git rebase 操作
  • 原文地址:https://www.cnblogs.com/mibing/p/7814530.html
Copyright © 2011-2022 走看看