SpringBoot集成redis 解决高并发

public List<Dept> select() {
    RedisSerializer<String>redisSerializer=new StringRedisSerializer();
    redisTemplate.setKeySerializer(redisSerializer);
   List<Dept>list=(List<Dept>) redisTemplate.opsForValue().get("select");
   if(null==list){
      synchronized (this){
          list=(List<Dept>) redisTemplate.opsForValue().get("select");
          if(null==list){
              list=deptMapper.select();
              redisTemplate.opsForValue().set("select",list);
          }
      }
   }
   return list;

}                                                                        

 说明; 

100 人进入该方法时,查询key "select", 没有找到的键,则list为null ,

进入同步锁内,第一个人查询key,未找到  ,开始查询数据库,并设置key ,返回结果

其余人查询key时已经存在,直接返回list,

猜你喜欢

转载自blog.csdn.net/qq_35002313/article/details/80663928