欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

System.Web.Caching.Cache删除某键后,希望同时触发其它动作(关键时刻,还是事件靠的住)

程序员文章站 2022-03-26 11:05:39
system.web.caching.cache是.net为我们提供的一个轻量级的缓存,它提供了最基础的增,增过期,删除,获取等操作,下面是对cache进行的一个封装,方便以后统一...

system.web.caching.cache是.net为我们提供的一个轻量级的缓存,它提供了最基础的增,增过期,删除,获取等操作,下面是对cache进行的一个封装,方便以后统一调用和扩展。
/// <summary>
    /// 缓o存?相à关?的?操ù作&pide;类à
    /// 占?占?
    /// </summary>
    public class datacache
    {
        /// <summary>
        /// 获?取?当±前°应|用?程ì序ò指?定¨cachekey的?cache值μ
        /// </summary>
        /// <param name="cachekey"></param>
        /// <returns></returns>
        public static object getcache(string cachekey)
        {
            system.web.caching.cache objcache = httpruntime.cache;
            return objcache[cachekey];

        }
        /// <summary>
        /// 设è置?当±前°应|用?程ì序ò指?定¨cachekey的?cache值μ
        /// 调&pide;用?:odatacache.setcache("name", "zzl");
        /// </summary>
        /// <param name="cachekey"></param>
        /// <param name="objobject"></param>
        public static void setcache(string cachekey, object objobject)
        {
            system.web.caching.cache objcache = httpruntime.cache;
            objcache.insert(cachekey, objobject);
        }

        /// <summary>
        /// 设è置?当±前°应|用?程ì序ò指?定¨cachekey的?cache值μ
        /// 调&pide;用?:odatacache.setcache("name", "zzl", datetime.now.addminutes(1), timespan.zero);
        /// </summary>
        /// <param name="cachekey"></param>
        /// <param name="objobject"></param>
        public static void setcache(string cachekey, object objobject, datetime absoluteexpiration, timespan slidingexpiration)
        {
            system.web.caching.cache objcache = httpruntime.cache;
            objcache.insert(cachekey, objobject, null, absoluteexpiration, slidingexpiration);
        }

        /// <summary>
        /// 移?除y指?定¨cache
        /// </summary>
        /// <param name="cachekey"></param>
        public static void removecache(string cachekey)
        {
            system.web.caching.cache objcache = httpruntime.cache;
            objcache.remove(cachekey);

        }

        #region events
        /// <summary>
        /// 缓o存?删?除y事?件t
        /// </summary>
        public static event cacheeventhandler cachedeleted;
        #endregion

        #region methods
        /// <summary>
        /// 触¥发¢缓o存?删?除y事?件t
        /// </summary>
        public static void oncachedeleted(string key)
        {
            if ((cachedeleted != null))
            {
                cachedeleted(null, new cacheeventargs(key));
            }

           remove(key);

        }
        #endregion
    }

    #region delegates
    /// <summary>
    /// 缓o存?委ˉ托d
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public delegate void cacheeventhandler(object sender, cacheeventargs e);
    #endregion

    /// <summary>
    /// 缓o存?事?件t源′
    /// </summary>
    public class cacheeventargs
    {
        public cacheeventargs()
        {

        }
        public cacheeventargs(string cachekey)
        {
            this.cachekey = cachekey;
        }
        public string cachekey { get; set; }
    }

在这个类中,有一个关于删除键时的事件,它的作用是在删除某个键时,进行与它关联的其它操作,例如:在用户登出时,希望将它的cache记录清除,并删除一些记录性的操作,但记录性的操作是不固定的,为了为破坏程序的完整性,我们可以在登出时,先去将其需要的操作进行有订阅,如果以后有其它删除cache键需要的关联操作,只要订阅就可以了
代码如下:
static commonmethod()
       {
           iuserrepository = new userrepository();
           //订?阅?了?cachedeleted这a个?事?件t,?当±它ü被?触¥发¢时±,?将?自?动ˉ执′行d被?绑ó定¨的?方?法¨
           datacache.cachedeleted += new cacheeventhandler(clearuserrecord);
           datacache.cachedeleted += new cacheeventhandler(clearproductrecord);
       }
    #region 删?除ycache后ó,?需è要a做?的?其?它ü事?情é
        static void clearuserrecord(object sender, cacheeventargs e)
        {
            datacache.setcache("msg1", e.cachekey + "被?删?除y了?");
        }


        static void clearproductrecord(object sender, cacheeventargs e)
        {
            datacache.setcache("msg2", e.cachekey + "真?的?被?删?除y了?");
        }
        #endregion


在登出操作后,可以去触发删除cache键的操作:
 
public static actionresult userlogoutmodule(controller controller)
       {
           string returnurl = controller.request.querystring["returnurl"];
           standard.clienthelper.userlogout();
          datacache.oncachedeleted("name");


摘自 lose.zhang