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


  • 相关阅读:
    el-select下拉框选项太多导致卡顿,使用下拉框分页来解决
    vue+elementui前端添加数字千位分割
    Failed to check/redeclare auto-delete queue(s)
    周末啦,做几道面试题放松放松吧!
    idea快捷键
    解决flink运行过程中报错Could not allocate enough slots within timeout of 300000 ms to run the job. Please make sure that the cluster has enough resources.
    用.net平台实现websocket server
    MQTT实战3
    Oracle 查看当前用户下库里所有的表、存储过程、触发器、视图
    idea从svn拉取项目不识别svn
  • 原文地址:https://www.cnblogs.com/dashi/p/4034672.html
Copyright © 2011-2022 走看看