Ejemplo n.º 1
0
        private Dictionary <EmailEnums.eTemplateKinds, string> GetTemplateDictionary()
        {
            const string cacheKey = "EMAIL_TEMPLATES";

            if (!IsDebugMode)
            {
                var result = CacheProxy.Get <Dictionary <EmailEnums.eTemplateKinds, string> >(cacheKey);

                if (result != null)
                {
                    return(result);
                }
            }

            var res = new Dictionary <EmailEnums.eTemplateKinds, string>();

            foreach (var p in EmailTemplateRepository.GetAll())
            {
                string temp;
                EmailEnums.eTemplateKinds key;

                try
                {
                    key = Utils.ParseEnum <EmailEnums.eTemplateKinds>(p.TemplateKindId);
                }
                catch (Exception)
                {
                    continue;
                }

                if (res.TryGetValue(key, out temp))
                {
                    res.Remove(key);
                }

                res.Add(key, p.Snippet);
            }

            if (IsDebugMode)
            {
                return(res);
            }

            CacheProxy.Add(cacheKey, res, DateTimeOffset.Now.AddDays(30));

            return(res);
        }
Ejemplo n.º 2
0
        public List <UserTagItemDTO> FindUsers(string q)
        {
            const string cacheKey = "USER_LOV";

            var result = GetCachedListByKey <UserTagItemDTO>(cacheKey, CacheProxy);

            if (result != null)
            {
                return(result.Where(x => x.label.ToLower().Contains(q.ToLower())).ToList());
            }

            result = UserRepository.GetAll().Select(x => x.Entity2TagItemDto()).ToList();

            CacheProxy.Add(cacheKey, result, DateTimeOffset.Now.AddMinutes(10));

            return(result.Where(x => x.label.ToLower().Contains(q.ToLower())).ToList());
        }
Ejemplo n.º 3
0
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            CacheMeta cacheMeta = cacheMetaLoader.Load(input.MethodBase);

            if (cacheMeta == null)
            {
                logger.Error("CacheMeta is null", new ArgumentNullException());
                return(getNext()(input, getNext));
            }

            CacheMode cacheMode = cacheMeta.CacheMode;

            ICacheHelper cacheHelper = ChooseCacheHelper(cacheMode);

            if (cacheHelper == null)
            {
                logger.Error(cacheMode + "cache type not found", new CacheModeNotFoundException());
                return(getNext()(input, getNext));
            }

            cacheHelper = new CacheProxy(cacheMode, cacheHelper);
            CacheAction cacheAction = cacheMeta.CacheAction;
            string      cacheKey    = BuildCacheKey(cacheMeta, input.Arguments);
            string      groupName   = cacheMeta.CacheGroup;
            string      cacheType   = cacheHelper.GetType().Name;

            logger.Debug(string.Format("Choose {0} as cache helper, cacheGroup={1}, cacheKey={2}", cacheType, cacheMeta.CacheGroup, cacheKey));

            if (cacheAction == CacheAction.FETCH)
            {
                object obj;
                try
                {
                    obj = cacheHelper.Get(groupName, cacheKey);
                }
                catch (Exception ex)
                {
                    logger.Error("Get cache failed, cacheType=" + cacheType, ex);
                    return(getNext()(input, getNext));
                }

                if (obj != null)
                {
                    logger.Debug(string.Format("Hitting cache, cacheGroup={0}, cacheKey={1}", groupName, cacheKey));
                    return(input.CreateMethodReturn(obj, null));
                }
                else
                {
                    IMethodReturn methodReturn = getNext()(input, getNext);
                    if (methodReturn.Exception == null && methodReturn.ReturnValue != null)
                    {
                        cacheHelper.Add(groupName, cacheKey, methodReturn.ReturnValue);
                        logger.Info(string.Format("Adding to cache, cacheGroup={0}, cacheKey={1}", groupName, cacheKey));
                    }
                    return(methodReturn);
                }
            }

            else if (cacheAction == CacheAction.UPDATE)
            {
                object cacheValue = GetCacheValue(input.Arguments);
                if (cacheValue != null)
                {
                    cacheHelper.Update(groupName, cacheKey, cacheValue);
                    logger.Info(string.Format("Update cache, cacheGroup={0}, cacheKey={1}", groupName, cacheKey));
                }
                return(getNext()(input, getNext));
            }

            else if (cacheAction == CacheAction.CLEAR)
            {
                IMethodReturn methodReturn = getNext()(input, getNext);
                cacheHelper.Remove(groupName, cacheKey);
                logger.Info(string.Format("Remove cache, cacheGroup={0}, cacheKey={1}", groupName, cacheKey));
                return(methodReturn);
            }

            return(getNext()(input, getNext));
        }
Ejemplo n.º 4
0
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            CacheMode cacheMode = GetCacheMode(input.MethodBase);
            ICacheAPI cacheAPI  = ChooseCacheAPI(cacheMode);

            if (cacheAPI == null)
            {
                logger.Error("No cache used");
                return(getNext()(input, getNext));
            }

            string cacheImplName = cacheAPI.GetType().Name;


            cacheAPI = new CacheProxy(cacheAPI, cacheMode, cacheStatistics);

            CacheType cacheType = GetCacheType(input.MethodBase);

            string cacheGroup = GetCacheGroup(input.MethodBase);
            string cacheKey   = CreateCacheKey(input.MethodBase, input.Arguments);

            logger.Info(string.Format("Choose cache api as {0}, cacheGroup={1}, cacheKey={2}", cacheImplName, cacheGroup, cacheKey));

            if (cacheMode.Equals(CacheMode.REMOTE) && cacheStatistics.IsError(cacheMode, cacheGroup))
            {
                logger.Error("Remote cache error occured too many, skip cache, get data from DB.");
                return(getNext()(input, getNext));
            }

            if (CacheType.FETCH.Equals(cacheType))
            {
                object obj = null;
                try
                {
                    obj = cacheAPI.Get(cacheGroup, cacheKey);
                }
                catch (Exception ex)
                {
                    logger.Error("get cache failed, cachetype=" + cacheType, ex);
                    return(getNext()(input, getNext));
                }

                if (obj != null)
                {
                    logger.Info("Hitting cache, cacheGroup=" + cacheGroup + ", cacheKey=" + cacheKey);
                    cacheStatistics.AddHitCount(cacheMode, cacheGroup);
                    return(input.CreateMethodReturn(obj, null));
                }
                else
                {
                    cacheStatistics.AddUnhitCount(cacheMode, cacheGroup);
                    IMethodReturn methodReturn = getNext()(input, getNext);
                    if (methodReturn.Exception == null && methodReturn.ReturnValue != null)
                    {
                        cacheAPI.Add(cacheGroup, cacheKey, methodReturn.ReturnValue);
                        logger.Info("Adding into cache, cacheGroup=" + cacheGroup + ", cacheKey=" + cacheKey);
                    }
                    return(methodReturn);
                }
            }
            else if (CacheType.CLEAR.Equals(cacheType))
            {
                IMethodReturn methodReturn = getNext()(input, getNext);
                cacheAPI.Remove(cacheGroup, cacheKey);
                return(methodReturn);
            }
            else if (CacheType.UPDATE.Equals(cacheType))
            {
                object cacheValue = GetCacheValue(input.Arguments);
                if (cacheValue != null)
                {
                    cacheAPI.Update(cacheGroup, cacheKey, cacheValue);
                    logger.Info("Update cache, cacheGroup=" + cacheGroup + ", cacheKey=" + cacheKey);
                }
                return(getNext()(input, getNext));
            }
            else
            {
                logger.Error("Invalid cache type, cachetype=" + cacheType);
                return(getNext()(input, getNext));
            }
        }