StringRedisTemplate使用总结之作为springcloud的fegin调用缓存方法

在项目中涉及到部分接口需要调用其他服务接口,每次调接口时直接使用fegin调用有时会由于网络问题或者其他服务不稳定问题导致调用服务方接口请求速度和时间造成影响,所以在fegin调用时使用了redis对部分请求接口数据做了缓存处理,代码如下:
value类型

  public TbShopGrade queryShopGradeInfo(Long gradeId) {
        String key = RedisConstants.SHOP_GRADE_INFO.concat(gradeId.toString());
        String json = stringRedisTemplate.opsForValue().get(key);
        log.info("从redis获取配置json={}", json);
        TbShopGrade shopGrade = null;
        if (StringUtil.isNotNull(json)) {
            shopGrade = JsonUtils.fromJson(json, TbShopGrade.class);
        }
        if (shopGrade == null) {
            shopGrade = managerClient.queryShopGradeInfo(gradeId);
            log.info("从feign调用中获取配置shopGrade={}", shopGrade);
            if (shopGrade != null) {
                json = JsonUtils.toJson(shopGrade);
                stringRedisTemplate.opsForValue().set(key, json, 30, TimeUnit.DAYS);
            }
        }
        return shopGrade;
    }

list类型

 public List getBuyerIdsAndTime(Long shopId, String startTime, String endTime) {
        String key = RedisConstants.ORDER_BUYER_USERID.concat(shopId.toString()).concat(startTime).concat(endTime);
        List stringList = stringRedisTemplate.opsForList().range(key, 0, -1);
        log.info("从redis获取店铺id查询所有的买家id和购买时间集合list={}", stringList);
        List list=new ArrayList<>(12);
        if (StringUtil.isNotNull(stringList)) {
            Iterator iterator = stringList.iterator();
            while (iterator.hasNext()){
                String next = iterator.next();
                List list1 = JsonUtils.fromJson(next, List.class);
                if(StringUtil.isNotNull(list1)){
                    Iterator iterator1 = list1.iterator();
                    while (iterator1.hasNext()){
                        Object next1 = iterator1.next();
                        UserGrowthThreadDto count = JsonUtils.fromJson(next1.toString(), UserGrowthThreadDto.class);
                        list.add(count);
                    }
                }
            }
            return list;
        }
        list = orderFeignClient.getBuyerIdsAndTime(shopId,startTime,endTime);
        log.info("从feign调用中获取店铺id查询所有的买家id和购买时间集合list={}", list);
        String json = JsonUtils.toJson(list);
        stringRedisTemplate.opsForList().rightPushAll(key, json);
        stringRedisTemplate.expire(key, 1, TimeUnit.DAYS);
        return list;
    }

hash类型

 public Map getUserWithdrawal(Long userId) {
        String key = RedisConstants.CAPITAL_WITHDRWAL_RECORD;
        Map map;
        if (stringRedisTemplate.opsForHash().hasKey(key,userId.toString())) {
            Object obj = stringRedisTemplate.opsForHash().get(key, userId.toString());
            map = JsonUtils.fromJson(obj.toString(), Map.class);
            log.info("从redis获取充值记录map={}", map);
        }else {
            map = capitalClient.getUserWithdrawal(userId);
            log.info("从feign调用中获取充值记录map={}", map);
            if (StringUtil.isNotNull(map)) {
                String s = JsonUtils.toJson(map);
                stringRedisTemplate.opsForHash().put(key, userId.toString(), s);
                stringRedisTemplate.expire(key, 10, TimeUnit.MINUTES);
            }
        }
        return map;
    }

你可能感兴趣的:(redis,springboot)