private void GetDataByCacheKey(IInvocation invocation, IRedisCacheService redisCacheService, ParameterInfo[] parameters, ServiceCacheAttribute svcCacheAttribute, string cacheKey)
        {
            int retryTimes = 0;

            if (redisCacheService.Contains(cacheKey))
            {
                GetDataByCacheKey(invocation, redisCacheService, parameters, cacheKey);
                retryTimes = 0;
            }
            else
            {
Label_RETRY:
                var isLock = CheckCacheKeyLock(invocation, redisCacheService, parameters, svcCacheAttribute, cacheKey);
                if (isLock)
                {
                    while (retryTimes <= 3)
                    {
                        Thread.Sleep(500);
                        retryTimes++;
                        goto Label_RETRY;
                    }
                    var cacheKeyLocked = string.Format("{0}:LOCK", cacheKey);
                    redisCacheService.Remove(cacheKeyLocked);
                }
            }
        }
Esempio n. 2
0
        public virtual TModel Add <TModel>(TModel entry)
        {
            if (entry == null)
            {
                return(default(TModel));
            }

            var entity = mapper.Map <TEntity>(entry);

            entity.TaoBoi  = entity.CapNhatBoi = LogedInUserId;
            entity.NgayTao = entity.NgayCapNhat = DateTime.UtcNow;
            unitOfWork.Repository <TEntity>().Add(entity);
            var saveResult = unitOfWork.SaveChanges();

            if (saveResult > 0)
            {
                var key = $"{typeof(TEntity).Name}_all";
                redisCache.Remove(key);

                return(mapper.Map <TModel>(entity));
            }

            throw new Exception();
        }
        private bool CheckCacheKeyLock(IInvocation invocation, IRedisCacheService redisCacheService, ParameterInfo[] parameters, ServiceCacheAttribute svcCacheAttribute, string cacheKey)
        {
            var cacheKeyLocked = string.Format("{0}:LOCK", cacheKey);

            if (redisCacheService.SetNX(cacheKeyLocked))
            {
                try
                {
                    AddCacheKey(invocation, redisCacheService, parameters, svcCacheAttribute, cacheKey);
                }
                catch (Exception ex)
                {
                    LogHelper.Error(string.Format("{0}\r\n{1}", ex.Message, ex.StackTrace.ToString()));
                }
                finally
                {
                    redisCacheService.Remove(cacheKeyLocked);
                }

                return(false);
            }
            return(true);
        }