SpringBoot 集成redis 缓存击穿 单机

SpringBoot 集成redis 缓存击穿

 

redis缓存

用户在访问数据查询数据时候,第一次查询到则存到数据库存储到redis,以后查询直接去redis查询!

我们先看下面代码,然后做个小测验。

public User selectByPrimaryKey(Integer id) {
 
        //查询缓存
        User user = (User) redisTemplet.opsForValue().get("user");
 
        //缓存中没有获取到则从数据中查询
        if(user==null){
            
                
                    user = userMapper.selectByPrimaryKey(id);
                    //存进缓存
                    redisTemplet.opsForValue().set("user",user);
                    System.out.println("db  ======== ");              
            
 
        }else {
             System.out.println("redis...");
        }
 
        return user;
    }

这样子看着没有什么问题

 

我们在前台调用

    @RequestMapping("users/{id}")
    @ResponseBody
    public User user(@PathVariable("id") int id ,User user){

        user =  userServiceimpl.selectByPrimaryKey(id);

        return  user;
    }

多次调用后 控制台输出

 

然后我们用多线程来调用,假设有10000个请求来访问

controller

    @RequestMapping("users/{id}")
    @ResponseBody
    public User user(@PathVariable("id") int id ,User user){

        //线程池30个线程
        final ExecutorService executorService = Executors.newFixedThreadPool(30);


        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                userServiceimpl.selectByPrimaryKey(id);
            }
        };

        //1w个请求
        for (int i = 0;i<10000;i++){
            executorService.submit(runnable);
        }



        user =  userServiceimpl.selectByPrimaryKey(id);

        return  user;
    }

 

 

然后控制台 输出日志

 

我们可以看到查询了很多次数据库,然后才从redis中获取。

多线程情况下,当至少两个线程进入到service中,if(user == null ) 这里,redis中没有缓存,会直接从db中查询,如果数据量再大点,会有很多次请求从db 中查询,这样子数据库压力很大。

这种情况成为 缓存穿透 。

 

所有我们需要改代码,让线程同步

采用 synchronized 关键字

  • 在方法上使用功能可以实现,但是每次请求只能有一个线程进来。其他的必须等我这个线程使用完成后你才能使用,其他线程处于阻塞状态,效率太低。
public synchronized  User selectByPrimaryKey(Integer id) {
  • 在代码块上使用
public synchronized  User selectByPrimaryKey(Integer id) {

 
    //查询缓存
    User user = (User) redisTemplet.opsForValue().get("user");

    //缓存中没有获取到则从数据中查询
    if(user==null){

         synchronized (this){
 
                user = userMapper.selectByPrimaryKey(id);
                //存进缓存
                redisTemplet.opsForValue().set("user",user);
                System.out.println("db  ======== ");
           

        }


    }else {
         System.out.println("redis...");
    }

    return user;
}

 

我们运一下看看结果

 

还有从数据库中查找,为什么?

还是多线程问题,多个线程同时进来在 redis 里查询时候发现没有数据,

User user = (User) redisTemplet.opsForValue().get("user");

然后都回去db中查找,此时的 synchronized 意义并不是很好。

既然会有同时查询redis为null ,然后之后都得进入 synchronized  语句块中查询,我门可以在 synchronized  中在进行判断,在查询数据库 之前判断 redis 中有没有值,有的话直接获取,没有的话查询,然后设置到redis中。

这样子就会只查询一次数据库。

  • 所以采用双重校验方式
//查询缓存
User user = (User) redisTemplet.opsForValue().get("user");

//缓存中没有获取到则从数据中查询
if(user==null){

     synchronized (this){
        //从redis 查 如果没有 下面 查一次数据库
         user = (User) redisTemplet.opsForValue().get("user");

        if (user==null){
            user = userMapper.selectByPrimaryKey(id);
            //存进缓存
            redisTemplet.opsForValue().set("user",user);
            System.out.println("db  ======== ");
         }else {
            System.out.println(" 同时查询到redis为 空 ,然后去db中查询 ,设置到 redis 中再查询。。 ");
         }

    }


}else {
     System.out.println("redis...");
}

 

 

我们来测试一下

 

 

 

总结 :

当数据量非常大的时候,采用双重校验可以减轻数据库压力,提高系统反应效率。

当数据量不是很大的时候,采用关键字即可。

 

 

 

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TizzyGoodhealth

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值