예제 #1
0
        public void SetCache(T cacheObj, CacheKeyEnum cacheKey, DateTime cacheTime, object id = null, params object[] param)
        {
            if (!CacheStaticDomain.Instance.CacheEnableSetting.ContainsKey(cacheKey) ||
                CacheStaticDomain.Instance.CacheEnableSetting[cacheKey] == CacheStatusEnum.Enable)
            {
                Dictionary <string, CacheObject <T> > dictCache = GetDictCache(cacheKey, id);

                if (dictCache == null)
                {
                    dictCache = new Dictionary <string, CacheObject <T> >();
                }

                if (param != null && param.Length > 0)
                {
                    string key = String.Join(",", param);

                    CacheObject <T> obj = new CacheObject <T>(cacheObj, cacheTime);

                    lock (lockObj)
                    {
                        dictCache[key] = obj;
                        SetDictCache(dictCache, obj, cacheKey, id);
                    }
                }
            }
        }
예제 #2
0
        public T GetCache(CacheKeyEnum cacheKey, object id = null)
        {
            if (!CacheStaticDomain.Instance.CacheEnableSetting.ContainsKey(cacheKey) ||
                CacheStaticDomain.Instance.CacheEnableSetting[cacheKey] == CacheStatusEnum.Enable)
            {
                CacheObject <T> cacheObj = GetCacheObject(cacheKey, id);

                if (cacheObj == null)
                {
                    return(default(T));
                }

                DateTime refreshTime = RedisCache.Instance.Get <DateTime>("AC_Refresh_" + cacheKey.ToString());
                if (cacheObj.CacheTime < refreshTime)
                {
                    RemoveCache(cacheKey, id);
                    return(default(T));
                }

                return(cacheObj.Data);
            }
            else
            {
                RemoveCache(cacheKey, id);
                return(default(T));
            }
        }
예제 #3
0
        public T GetCache(CacheKeyEnum cacheKey, object id, params object[] param)
        {
            if (!CacheStaticDomain.Instance.CacheEnableSetting.ContainsKey(cacheKey) ||
                CacheStaticDomain.Instance.CacheEnableSetting[cacheKey] == CacheStatusEnum.Enable)
            {
                Dictionary <string, CacheObject <T> > dictCache = GetDictCache(cacheKey, id);

                if (dictCache != null && param != null && param.Length > 0)
                {
                    lock (lockObj)
                    {
                        string key = String.Join(",", param);
                        if (dictCache.ContainsKey(key))
                        {
                            DateTime refreshTime = RedisCache.Instance.Get <DateTime>("AC_Refresh_" + cacheKey.ToString());

                            if (dictCache[key].CacheTime < refreshTime)
                            {
                                //RemoveCache(cacheKey, id);
                                return(default(T));
                            }
                            return(dictCache[key].Data);
                        }
                    }
                }
                return(default(T));
            }

            else
            {
                RemoveCache(cacheKey, id);
                return(default(T));
            }
        }
예제 #4
0
 public static void SetCache <T>(T cacheObj, CacheKeyEnum cacheKey, DateTime cacheTime, object id, params object[] param) where T : class
 {
     if (cacheObj != null)
     {
         CacheFactory.GetCacheHandler <T>(cacheKey).SetCache(cacheObj, cacheKey, cacheTime, id, param);
     }
 }
예제 #5
0
 /// <summary>
 /// 设置对象数据缓存项添加删除或更改缓存后的回调方法在Controler中使用该方法需要实现接口ICacheCallBack并重写其中的方法
 /// 在类中使用该方法需要继承抽象类CacheCallBack并实现其中的方法注:该方法中删除后回调和更新后回调不可同时存在
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <param name="value"></param>
 /// <param name="funR"></param>
 /// <param name="funU"></param>
 /// <param name="cacheTime"></param>
 /// <param name="slidingExpiration"></param>
 public void Set <T>(CacheKeyEnum key, T value, Action <CacheEntryRemovedArguments> funR, Action <CacheEntryUpdateArguments> funU, int?cacheTime = null, int?slidingExpiration = null)
 {
     if (value == null)
     {
         return;
     }
     Cache.Set(new CacheItem(key.Enum2String(), value), SetPolicy(funR, funU, cacheTime, slidingExpiration));
 }
예제 #6
0
 /// <summary>
 /// 可以提供数据库监测数据变动的缓存策略(需要设置的缓存数据为对象类型参数)(绝对或相对过期时间为分钟)
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <param name="t"></param>
 /// <param name="sql"></param>
 /// <param name="cacheTime"></param>
 /// <param name="slidingExpiration"></param>
 public void Set <T>(CacheKeyEnum key, T t, string sql, int?cacheTime = null, int?slidingExpiration = null)
 {
     if (t != null)
     {
         var policy = SetSqlPolicy(connectionString, sql, cacheTime, slidingExpiration);
         Cache.Set(new CacheItem(key.Enum2String(), t), policy);
     }
 }
예제 #7
0
 /// <summary>
 /// 设置对象类型参数的缓存;该方法具有SqlChangeMonitor策略的CacheItemPolicy
 /// 并且提供当数据库表数据更改时策略被触发后删除缓存时触发的删除和更新回调函数
 /// 在Controler中使用该方法需要实现接口ICacheCallBack并重写其中的方法
 /// 在类中使用该方法需要继承抽象类CacheCallBack并实现其中的方法注:该方法中删除后回调和更新后回调不可同时存在
 /// (绝对或相对过期时间为分钟)
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <param name="t"></param>
 /// <param name="sql"></param>
 /// <param name="funR"></param>
 /// <param name="funU"></param>
 /// <param name="cacheTime"></param>
 /// <param name="slidingExpiration"></param>
 public void Set <T>(CacheKeyEnum key, T t, string sql, Action <CacheEntryRemovedArguments> funR, Action <CacheEntryUpdateArguments> funU, int?cacheTime = null, int?slidingExpiration = null)
 {
     if (t != null)
     {
         var policy = SetSqlPolicy(connectionString, sql, funR, funU, cacheTime, slidingExpiration);
         Cache.Set(new CacheItem(key.Enum2String(), t), policy);
     }
 }
예제 #8
0
 protected override void SetCacheObject(CacheObject <T> cacheObj, CacheKeyEnum cacheKey, object id = null)
 {
     HttpRuntime.Cache.Insert(CacheKey.GetCacheKey(cacheKey, id), cacheObj, null,
                              DateTime.Now.AddSeconds(CacheStaticDomain.Instance.CacheDurations[cacheKey]),
                              System.Web.Caching.Cache.NoSlidingExpiration,
                              System.Web.Caching.CacheItemPriority.Default,
                              null);
 }
예제 #9
0
 /// <summary>
 /// 设置对象数据缓存
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <param name="value"></param>
 /// <param name="cacheTime"></param>
 /// <param name="slidingExpiration"></param>
 public void Set <T>(CacheKeyEnum key, T value, int?cacheTime = null, int?slidingExpiration = null)
 {
     if (value == null)
     {
         return;
     }
     Cache.Set(new CacheItem(key.Enum2String(), value), SetPolicy(cacheTime, slidingExpiration));
 }
예제 #10
0
 public void SetCache(T cacheObj, CacheKeyEnum cacheKey, DateTime cacheTime, object id = null)
 {
     if (!CacheStaticDomain.Instance.CacheEnableSetting.ContainsKey(cacheKey) ||
         CacheStaticDomain.Instance.CacheEnableSetting[cacheKey] == CacheStatusEnum.Enable)
     {
         CacheObject <T> obj = new CacheObject <T>(cacheObj, cacheTime);
         SetCacheObject(obj, cacheKey, id);
     }
 }
예제 #11
0
        /// <summary>
        /// 可以提供数据库监测数据变动的缓存策略(需要设置的缓存数据为方法类型参数)(绝对或相对过期时间为分钟)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="func"></param>
        /// <param name="sql"></param>
        /// <param name="cacheTime"></param>
        /// <param name="slidingExpiration"></param>
        public void Set <T>(CacheKeyEnum key, Func <T> func, string sql, int?cacheTime = null, int?slidingExpiration = null)
        {
            var data = func();

            if (data != null)
            {
                var policy = SetSqlPolicy(connectionString, sql, cacheTime, slidingExpiration);
                Cache.Set(new CacheItem(key.Enum2String(), data), policy);
            }
        }
예제 #12
0
        protected override CacheObject <T> GetCacheObject(CacheKeyEnum cacheKey, object id)
        {
            CacheObject <T> cacheObject = (CacheObject <T>)HttpRuntime.Cache[CacheKey.GetCacheKey(cacheKey, id)];

            if (cacheObject == null)
            {
                cacheObject = (CacheObject <T>)HttpRuntime.Cache[CacheKey.GetCacheKey(cacheKey, id) + "_5"];
            }

            return(cacheObject);
        }
예제 #13
0
        /// <summary>
        /// 最新方法如果缓存过时为空将使用传入的方法获取需要设置的最新数据进行缓存
        /// 该方法将返回List数据类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="func"></param>
        public List <T> Get <T>(CacheKeyEnum key, Func <List <T> > func)
        {
            var data = Cache[key.Enum2String()] as List <T>;

            if (data == null)
            {
                data = func();
                Cache.Set(new CacheItem(key.Enum2String(), data), SetPolicy(cacheTime, slidingExpiration));
            }
            return(data);
        }
예제 #14
0
        public static void RemoveCache(CacheKeyEnum cacheKey, object id = null)
        {
            //if (CacheStaticDomain.Instance.CacheTypeMapping[cacheKey] == CacheTypeEnum.LocalCache || CacheStaticDomain.Instance.CacheTypeMapping[cacheKey] == CacheTypeEnum.Composite)
            //{
            //    RedisCache.Instance.Set<DateTime>("AC_Refresh_" + cacheKey.ToString(), DateTime.Now, 3600);//1小时,目前所有缓存最长时间是1小时
            //}

            RedisCache.Instance.Set <DateTime>("AC_Refresh_" + cacheKey.ToString(), DateTime.Now, 3600);//1小时,目前所有缓存最长时间是1小时

            CacheFactory.GetCacheHandler <object>(cacheKey).RemoveCache(cacheKey, id);
        }
예제 #15
0
        /// <summary>
        /// 获取缓存数据请使用该方法常用方法如果缓存过期为null可传入重新获取数据方法并设置缓存数据
        /// </summary>
        /// <typeparam name="T">返回List类型</typeparam>
        /// <param name="key"></param>
        /// <param name="func"></param>
        /// <param name="funR"></param>
        /// <param name="funU"></param>
        /// <param name="cacheTime"></param>
        /// <param name="slidingExpiration"></param>
        /// <returns></returns>
        public List <T> GetList <T>(CacheKeyEnum key, Func <List <T> > func, Action <CacheEntryRemovedArguments> funR, Action <CacheEntryUpdateArguments> funU, int?cacheTime = null, int?slidingExpiration = null)
        {
            var data = Cache[key.Enum2String()] as List <T>;

            if (data == null)
            {
                data = func();
                Cache.Set(new CacheItem(key.Enum2String(), data), SetPolicy(funR, funU, cacheTime, slidingExpiration));
            }
            return(data);
        }
예제 #16
0
 public static void RefreshCache(CacheKeyEnum cacheKey)
 {
     if (CacheKey.CacheKeyExpression[cacheKey].EndsWith("_{0}"))
     {
         RedisCache.Instance.Set <DateTime>("AC_Refresh_" + cacheKey.ToString(), DateTime.Now, 3600);//1小时,目前所有缓存最长时间是1小时
     }
     else
     {
         RemoveCache(cacheKey);
     }
 }
예제 #17
0
        protected override CacheObject <T> GetCacheObject(CacheKeyEnum cacheKey, object id)
        {
            CacheObject <T> cacheObject = (CacheObject <T>)HttpRuntime.Cache[CacheKey.GetCacheKey(cacheKey, id)];

            if (cacheObject == null)
            {
                cacheObject = RedisCache.Instance.Get <CacheObject <T> >(CacheKey.GetCacheKey(cacheKey, id));
            }

            return(cacheObject);
        }
예제 #18
0
 public static void FlushCache()
 {
     foreach (FieldInfo field in typeof(CacheKeyEnum).GetFields())
     {
         if (field.FieldType == typeof(CacheKeyEnum))
         {
             CacheKeyEnum cacheKey = (CacheKeyEnum)Enum.Parse(typeof(CacheKeyEnum), field.Name);
             RefreshCache(cacheKey);
         }
     }
 }
예제 #19
0
        protected override Dictionary <string, CacheObject <T> > GetDictCache(CacheKeyEnum cacheKey, object id)
        {
            Dictionary <string, CacheObject <T> > dict = (Dictionary <string, CacheObject <T> >)HttpRuntime.Cache[CacheKey.GetCacheKey(cacheKey, id)];

            if (dict == null)
            {
                dict = (Dictionary <string, CacheObject <T> >)HttpRuntime.Cache[CacheKey.GetCacheKey(cacheKey, id) + "_5"];
            }

            return(dict);
        }
예제 #20
0
        public static T GetCacheWithFill <T, T1>(CacheKeyEnum cacheKey, GetObjInvokeDelegate <T, T1> getObjInvokeDele, T1 id, DateTime cacheTime, params object[] param) where T : class
        {
            T result = GetCache <T>(cacheKey, id, param);

            if (result == null)
            {
                result = getObjInvokeDele(id);
                if (result != null)
                {
                    SetCache(result, cacheKey, cacheTime, id, param);
                }
            }
            return(result);
        }
예제 #21
0
        private static void InitCacheDuration()
        {
            foreach (FieldInfo memberInfo in typeof(CacheKeyEnum).GetFields())
            {
                if (memberInfo.FieldType.Name == "CacheKeyEnum")
                {
                    CacheKeyEnum cacheKey = (CacheKeyEnum)Enum.Parse(typeof(CacheKeyEnum), memberInfo.Name);

                    IList <CacheDurationAttibute> cacheDurationAttibutes = memberInfo.GetCustomAttributes <CacheDurationAttibute>().ToList();
                    if (cacheDurationAttibutes != null && cacheDurationAttibutes.Count() > 0)
                    {
                        CacheStaticDomain.Instance.CacheDurations[cacheKey] = cacheDurationAttibutes[0].CacheDuration;
                    }
                    else
                    {
                        CacheStaticDomain.Instance.CacheDurations[cacheKey] = 3600;
                    }
                }
            }
        }
예제 #22
0
        private static void InitCacheMaping()
        {
            foreach (FieldInfo memberInfo in typeof(CacheKeyEnum).GetFields())
            {
                if (memberInfo.FieldType.Name == "CacheKeyEnum")
                {
                    CacheKeyEnum cacheKey = (CacheKeyEnum)Enum.Parse(typeof(CacheKeyEnum), memberInfo.Name);

                    IList <CacheKeyTypeAttribue> cacheKeyTypeAttribues = memberInfo.GetCustomAttributes <CacheKeyTypeAttribue>().ToList();
                    if (cacheKeyTypeAttribues != null && cacheKeyTypeAttribues.Count() > 0)
                    {
                        CacheStaticDomain.Instance.CacheTypeMapping[cacheKey] = cacheKeyTypeAttribues[0].CacheTypeEnum;
                    }
                    else
                    {
                        CacheStaticDomain.Instance.CacheTypeMapping[cacheKey] = CacheTypeEnum.Redis;
                    }
                }
            }
        }
예제 #23
0
        public static CacheHandler <T> GetCacheHandler <T>(CacheKeyEnum cacheKey)
        {
            CacheTypeEnum cacheType = CacheStaticDomain.Instance.CacheTypeMapping[cacheKey];

            if (cacheType == CacheTypeEnum.Redis)
            {
                return(new RedisCacheHandler <T>());
            }
            else if (cacheType == CacheTypeEnum.LocalCache)
            {
                return(new LocalCacheHandler <T>());
            }
            else if (cacheType == CacheTypeEnum.Composite)
            {
                return(new DepositeCacheHandler <T>());
            }
            else
            {
                throw new NotImplementedException();
            }
        }
예제 #24
0
        private static void InitCacheDependency()
        {
            foreach (FieldInfo memberInfo in typeof(CacheKeyEnum).GetFields())
            {
                if (!memberInfo.FieldType.Name.Equals("CacheKeyEnum"))
                {
                    continue;
                }
                IList <CacheDependencyAttibute> cacheDimensionAttibutes = memberInfo.GetCustomAttributes <CacheDependencyAttibute>().ToList();

                if (cacheDimensionAttibutes != null && cacheDimensionAttibutes.Count() > 0)
                {
                    foreach (CacheDependencyAttibute cacheDimensionAttribute in cacheDimensionAttibutes)
                    {
                        if (cacheDimensionAttribute != null)
                        {
                            foreach (KeyValuePair <CacheDependencyEnum, CacheDependencyActionType[]> cacheDependency in
                                     cacheDimensionAttribute.CacheDependencys)
                            {
                                foreach (CacheDependencyActionType actionType in cacheDependency.Value)
                                {
                                    string key = cacheDependency.Key + "_" + actionType;

                                    if (!CacheStaticDomain.Instance.CacheDependencys.ContainsKey(key))
                                    {
                                        CacheStaticDomain.Instance.CacheDependencys.Add(key, new List <CacheKeyEnum>());
                                    }

                                    CacheKeyEnum cacheKey = (CacheKeyEnum)Enum.Parse(typeof(CacheKeyEnum), memberInfo.Name);
                                    if (!CacheStaticDomain.Instance.CacheDependencys[key].Contains(cacheKey))
                                    {
                                        CacheStaticDomain.Instance.CacheDependencys[key].Add(cacheKey);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
예제 #25
0
 public override void Refresh(CacheKeyEnum cacheKeyEnum, int Id)
 {
     throw new NotImplementedException();
 }
예제 #26
0
 public override void Refresh(CacheKeyEnum cacheKeyEnum, int Id)
 {
     CacheHelper.RemoveCache(cacheKeyEnum, Id);
 }
예제 #27
0
 public virtual void Refresh(CacheKeyEnum cacheKeyEnum, int Id)
 {
 }
예제 #28
0
        protected override void SetDictCache(Dictionary <string, CacheObject <T> > dictCache, CacheObject <T> currentObj, CacheKeyEnum cacheKey, object id)
        {
            HttpRuntime.Cache.Insert(CacheKey.GetCacheKey(cacheKey, id), dictCache, null,
                                     DateTime.Now.AddSeconds(CacheStaticDomain.Instance.CacheDurations[cacheKey]),
                                     System.Web.Caching.Cache.NoSlidingExpiration,
                                     System.Web.Caching.CacheItemPriority.Default,
                                     null);

            HttpRuntime.Cache.Insert(CacheKey.GetCacheKey(cacheKey, id) + "_5", dictCache, null,
                                     DateTime.Now.AddSeconds(60 * 5),
                                     System.Web.Caching.Cache.NoSlidingExpiration,
                                     System.Web.Caching.CacheItemPriority.Default,
                                     null);
        }
예제 #29
0
 protected abstract CacheObject <T> GetCacheObject(CacheKeyEnum cacheKey, object id);
예제 #30
0
 public override void RemoveCache(CacheKeyEnum cacheKey, object id = null)
 {
     HttpRuntime.Cache.Remove(CacheKey.GetCacheKey(cacheKey, id));
     HttpRuntime.Cache.Remove(CacheKey.GetCacheKey(cacheKey, id) + "_5");
 }