• 缓存管理公用类(通过委托更新缓存)


    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;
            }


  • 相关阅读:
    java内存分析 栈 堆 常量池的区别
    了解struts2 action的一些原理
    条件语句的写法
    Library中的title与Name
    样式优先级、margin
    文件夹IsShow字段为空
    Ispostback
    HierarchicalDataBoundControl 错误
    DBNull与Null
    sharepoint中的YesNo字段
  • 原文地址:https://www.cnblogs.com/dashi/p/4034672.html
Copyright © 2020-2023  润新知