zoukankan      html  css  js  c++  java
  • 缓存管理公用类(通过委托更新缓存)

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Web;

     

    namespace Cache

    {

        public class IListCache<T>

        {

           public delegate IList<T> UpdateDelegate();//该委托不支持传入参数,如需传参请封装一个类

           public UpdateDelegate delegate1;//该委托变量用于注册更新缓存的函数指针

     

           static Dictionary<string, IList<T>> DicObj = new Dictionary<string, IList<T>>();//缓存对象

           static Dictionary<string, DateTime> DicTime = new Dictionary<string, DateTime>();//超时时间 

           string CacheKey;//缓存Key

           ///<summary>

           ///获取指定CacheKey的缓存

           ///</summary>

           ///<param name="ud"></param>

           ///<param name="CacheKey">缓存KEY</param>

           ///<returns></returns>

           public IList<T> GetCache(string CacheKey)

           {

               this.CacheKey = CacheKey;

               bool isTimeOut = false;

     

               IList<T> obj = GetCache(ref isTimeOut);

               if (obj != null)

               {

                   if (isTimeOut) AsyncUpdateCache();

                   return obj;

               }

               else

               {

                   return GetObjectAndSetCache();

               }

           }

     

     

           ///<summary>

           ///异步更新缓存

           ///</summary>

           void AsyncUpdateCache()

           {

               Func<IList<T>> albumFunC =new Func<IList<T>>(GetObjectAndSetCache);

               albumFunC.BeginInvoke(null,null);

           }

     

           IList<T> GetObjectAndSetCache()

           {

               IList<T> ilt = null;

     

               if (delegate1 != null)

               {     //如果有方法注册委托变量

                   ilt = delegate1();      //通过委托调用方法

               }

     

               //通过名称获取对应的缓存时间

               int intCache = 60;

               int? enumVal =GetExpiredTimeByCacheName(CacheKey);//该缓存时间可从配置文件读取

               if (enumVal.HasValue)

               { intCache = enumVal.Value; }

     

               SetCache(ilt, intCache);

               return ilt;

           }

     

           ///<summary>

           ///获取缓存

           ///</summary>

           ///<returns></returns>

           IList<T> GetCache(refbool isTimeOut)

           {

     

               if (DicObj.ContainsKey(CacheKey))

               {

                   if (DicTime.ContainsKey(CacheKey))

                   {

                       isTimeOut = (DateTime.Now > DicTime[CacheKey]);

                   }

                   return DicObj[CacheKey];

               }

               return null;

           }

     

     

           ///<summary>

           ///设置当前应用程序指定CacheKey的Cache对象

           ///</summary>

           ///<param name="objObject">Cache</param>

           ///<param name="absoluteExpiration">绝对期限</param>

     

           void SetCache(IList<T> objObject,DateTime absoluteExpiration)

           {

               DicObj[CacheKey] = objObject;

               DicTime[CacheKey] = absoluteExpiration;

           }

     

           ///<summary>

           ///设置当前应用程序指定CacheKey的Cache对象

           ///</summary>

           ///<param name="objObject"></param>

           ///<param name="expirationMinutes">过期分钟数</param>

           void SetCache(IList<T> objObject,int expirationMinutes)

           {

               DateTime absoluteExpiration =DateTime.Now.AddMinutes(expirationMinutes);

     

               SetCache(objObject, absoluteExpiration);

           }

     

        }

    }



            /// <summary>
            /// 读取配置文件的缓存时间设置
            /// </summary>
            /// <param name="cacheKey"></param>
            /// <returns></returns>
            private int? GetExpiredTimeByCacheName(string cacheKey)
            {
                try
                {
                    string s = ConfigurationManager.AppSettings[cacheKey].ToString();
                    int ret = PublicFun.ToInt(s);
                    if (ret > 0)
                    {
                        return ret;
                    }
                }
                catch
                {
                }


                return null;
            }


  • 相关阅读:
    P1117 [NOI2016] 优秀的拆分 SA+DP
    P3346 [ZJOI2015]诸神眷顾的幻想乡 广义SAM
    P3705 [SDOI2017]新生舞会 分数规划+费用流
    P2336 [SCOI2012]喵星球上的点名 SA+树状数组
    543. Diameter of Binary Tree
    451. Sort Characters By Frequency
    563. Binary Tree Tilt
    703. Kth Largest Element in a Stream
    743. Network Delay Time
    kaggle _Titanic: Machine Learning from Disaster
  • 原文地址:https://www.cnblogs.com/dashi/p/4034672.html
Copyright © 2011-2022 走看看