redis-缓存穿透

缓存穿透:⼤量的请求⼀个数据库中不存在的数据,⾸先在redis中⽆法命中,最终所有
的请求都会访问数据库,同样会导致数据库承受巨⼤的访问压⼒

 请求访问时,redis中没有这个数据(或者过期了),数据库中也没有,大量请求一直访问数据库,造成访问压力

 

 @Override
    public ResultVo selectAllCategories() {
        List<Category> res = null;
        String resStr = stringRedisTemplate.boundValueOps("res").get();
        try {
            if (resStr != null) {
                JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, Category.class);
                res = objectMapper.readValue(resStr, javaType);
            } else {
                // 防止缓存击穿   加锁
                synchronized (this) {
                    //再次从redis中取数据
                    String s = stringRedisTemplate.boundValueOps("res").get();
                    // 如果没有数据取数据库取数据并放到redis
                    if (s == null) {
                        res = categoryMapper.selectAllCategories(0);
                        if (res == null){
                            //数据库中的数据也为空,为防止缓存穿透在redis中放置一个空数据,
                            //并设置过期时间,防止一直查询数据库空数据
                            ArrayList<Category> categories = new ArrayList<>();
                            stringRedisTemplate.boundValueOps("res").set(objectMapper.writeValueAsString(categories),10, TimeUnit.SECONDS);
                                //也可以这样写
                                //stringRedisTemplate.boundValueOps("res").set("[]"),10, TimeUnit.SECONDS);
                        }else {
                            stringRedisTemplate.boundValueOps("res").set(objectMapper.writeValueAsString(res),1, TimeUnit.DAYS);
                        }
                    } else {
                        //将redis中数据返回
                        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, Category.class);
                        res = objectMapper.readValue(s, javaType);
                    }
                }

            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return ResultVo.ok().data("res", res);
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值