zoukankan      html  css  js  c++  java
  • 实现缓存移除时通知应用程序

    创建一个静态类
    该类负责从缓存中检索项并处理回调方法,以将项添加回缓存中。
    1.在该类中,创建用于将项添加到缓存中的方法。

    2.在该类中,创建用于从缓存中获取项的方法。

    3.创建用于处理缓存项移除回调的方法。
    该方法必须具备与 CacheItemRemovedCallback 委托相同的函数签名。从缓存中删除项时,会在该方法中执行要运行的逻辑,如重新生成项并将其添加回缓存中。

    using System;
    using System.Web;
    using System.Web.Caching;
    public static class ReportManager
    {
        private static bool _reportRemovedFromCache = false;
        static ReportManager()
        { }

        public static String GetReport()
        {
            lock (typeof(ReportManager))
            {
                if (HttpContext.Current.Cache["MyReport"] != null)
                    return (string)HttpRuntime.Cache["MyReport"];
                else
                {
                    CacheReport();
                    return (string)HttpRuntime.Cache["MyReport"];
                }
            }
        }

        public static void CacheReport()
        {
            lock (typeof(ReportManager))
            {
                HttpContext.Current.Cache.Add("MyReport",
                    CreateReport(), null, DateTime.MaxValue,
                    new TimeSpan(0, 1, 0),
                    System.Web.Caching.CacheItemPriority.Default,
                    ReportRemovedCallback);
            }
        }

        private static string CreateReport()
        {
            System.Text.StringBuilder myReport =
                new System.Text.StringBuilder();
            myReport.Append("Sales Report<br />");
            myReport.Append("2005 Q2 Figures<br />");
            myReport.Append("Sales NE Region - $2 million<br />");
            myReport.Append("Sales NW Region - $4.5 million<br />");
            myReport.Append("Report Generated: " + DateTime.Now.ToString()
                + "<br />");
            myReport.Append("Report Removed From Cache: " +
                _reportRemovedFromCache.ToString());
            return myReport.ToString();
        }

        public static void ReportRemovedCallback(String key, object value,
            CacheItemRemovedReason removedReason)
        {
            _reportRemovedFromCache = true;
            CacheReport();
        }
    }

    也可以写委托方法:

    HttpContext.Current.Cache.Insert (
            "MyReport",
            CreateReport(),
            new CacheDependency ("C:\\CacheDemo\\Dependence"),
            Cache.NoAbsoluteExpiration,
            Cache.NoSlidingExpiration,
            CacheItemPriority.Default,
            new CacheItemRemovedCallback (CreateReport)

    );

    页面调用:
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Label1.Text = ReportManager.GetReport();
    }

     

  • 相关阅读:
    struts2 标签为简单标签
    html a标签链接使用action 参数传递中文乱码
    html 字体加粗
    Unity3D学习笔记(一):Unity简介、游戏物体、组件和生命周期函数
    Unity3D学习笔记(一):Unity3D简介 111
    C#学习笔记(二十):C#总结和月考讲解
    C#学习笔记(十九):字典
    C#学习笔记(十八):数据结构和泛型
    C#学习笔记(十七):委托、事件、观察者模式、匿名委托和lambert表达式
    C#学习笔记(十六):索引器和重载运算符
  • 原文地址:https://www.cnblogs.com/goooto/p/1129627.html
Copyright © 2011-2022 走看看