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

    示例代码如上

  • 相关阅读:
    RocketMQ简介
    redis简介
    netcat
    NOI2018 归程
    图论小专题B
    CF558E A Simple Task
    图论小专题A
    初等网络流初步
    2019炎德杯知识能力竞赛 游记
    POJ2176 Folding
  • 原文地址:https://www.cnblogs.com/mibing/p/7814530.html
Copyright © 2020-2023  润新知