redis中缓存穿透

 

 

 

 

 

代码实现

 

//        String key=CACHE_SHOP_KEY + id;
// //1从缓存中查询上铺缓存
// String shopJson = stringRedisTemplate.opsForValue().get(key);
// //2判断是否存在
// if (StrUtil.isBlank(shopJson)) {
// //3存在直接返回
// Shop shop = JSONUtil.toBean(shopJson, Shop.class);
// return Result.ok(shop);
// }
// //4不存在,根据id去查询数据库
// Shop shop = getById(id);
// //5不存在,返回错误
// if (shop==null){
// //将null值写入redis
// stringRedisTemplate.opsForValue().set(key,"",CACHE_NULL_TTL,TimeUnit.MINUTES);
// //返回错误信息
// return Result.fail("店铺不存在");
// }
// //6存在,写入redis
// stringRedisTemplate.opsForValue().set(key,JSONUtil.toJsonStr(shop),CACHE_SHOP_TTL,TimeUnit.MINUTES);
// //7.返回
// return Result.ok(shop);

 

posted @ 2022-11-13 13:45  এএ᭄念卿এএ᭄  阅读(25)  评论(0)    收藏  举报