/// <summary> /// 本地DB数据,走Redis /// </summary> /// <returns></returns> public async Task <ApiResponse <IEnumerable <UserDTO> > > GetUserForRedis() { IEnumerable <UserDTO> userList; var cacheKey = "userListcacheKey"; Func <Task <IEnumerable <UserDTO> > > func = async() => { var rediscacheKey = RedisKey.GetAPIRedisKey(cacheKey); var rediscache = await _redis.StringGetAsync(rediscacheKey); if (!string.IsNullOrEmpty(rediscache)) { userList = JsonConvert.DeserializeObject <IEnumerable <UserDTO> >(rediscache); } else { userList = await _testRepository.GetUser(); _redis.StringSet(rediscacheKey, JsonConvert.SerializeObject(userList), RedisExpiration); } return(userList); }; var errorMessage = "获取用户信息失败"; return(await ServiceHandlerAsync(func, errorMessage)); }
public void Set <TValue>(string key, TValue value, TimeSpan absoluteExpiration, int RedisExpiration) { _localMemoryCache.SetDataOrEmptyTag(key, value, absoluteExpiration); try { _redis.StringSet(RedisKey.GetAPIRedisKey(key), JsonConvert.SerializeObject(value), (int)absoluteExpiration.TotalSeconds); } catch (Exception e) { _redis.StringSet(RedisKey.GetAPIRedisKey(key), JsonConvert.SerializeObject(value), int.Parse(_configuration["Redis:ExpiredMinute"])); } }
public bool Get <TValue>(string key, TimeSpan absoluteExpiration, out TValue value) where TValue : class { value = default; if (_localMemoryCache.Get(key, out TValue cacheValue)) { value = cacheValue; } else { var redisValue = _redis.StringGet(RedisKey.GetAPIRedisKey(key)); if (!string.IsNullOrEmpty(redisValue)) { value = JsonConvert.DeserializeObject <TValue>(redisValue); _localMemoryCache.SetDataOrEmptyTag(key, value, absoluteExpiration); } else { return(false); } } return(true); }