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); } } }
public async Task <BaseResponse <object> > GetAll() { //BaseResponse<IEnumerable<CityDto>> baseResponse = new BaseResponse<IEnumerable<CityDto>>(); string key = RedisConsts.City + DateTime.Now.ToString("yyyyMMdd_hh"); var keyExists = await _redisCacheService.Contains(key); if (keyExists) { var recordsInCache = _redisCacheService.Get <IEnumerable <CityDto> >(key); _response.IsSuccess = true; _response.TimeStamp = DateTime.Now; _response.Result = recordsInCache; } else { var result = await _cityRepository.GetAll(); var mappedResult = _mapper.Map <IEnumerable <CityDto> >(result); _response.IsSuccess = true; _response.TimeStamp = DateTime.Now; _response.Result = mappedResult; _redisCacheService.SetAsync <IEnumerable <CityDto> >(key, mappedResult); } return(_response); }
private bool CheckCacheKey(IInvocation invocation, IRedisCacheService redisCacheService, ParameterInfo[] parameters, ServiceCacheAttribute svcCacheAttribute, string cacheKey) { if (redisCacheService.Contains(cacheKey)) { GetDataByCacheKey(invocation, redisCacheService, parameters, cacheKey); return(false); } else { CheckCacheKeyLock(invocation, redisCacheService, parameters, svcCacheAttribute, cacheKey); return(true); } }