1、缓存穿透
指查询一个一定不存在的数据,由于缓存是不命中,将去查询数据库,但是数据库也无次记录,我们没有将这次查询的null写入缓存,这将导致这个不存在的数据每次请求都要到存储层去查询,失去了缓存的意义。
风险:
利用不存在的数据进行攻击,数据库瞬时压力增大,最终导致崩溃
解决:
null结果缓存,并加入短暂过期时间
2、缓存雪崩
缓存雪崩是指在我们设置缓存key采用了相同的过期时间,导致缓存存在某一时刻相同失效,请求全部转发到DB,DB瞬时压力过重雪崩
解决:
原有的失效时间基础上增加一个随机值,比如1-5分钟随机,这样每一个缓存的过期时间的重复率会降低,就很难引发集体失效的事件。
3、缓存击穿
- 对于一些设置了过期时间的key,如果这些key可能会在某些时间点被超高并发访问,是一种非常 热点 的数据.
- 如果这个key在大量的数据查询请求同时进来正好失效,那么所有对这个key的数据查询都落到DB,我们称为缓存击穿。
解决:
加锁,大量并发只让一个人去查,其他人等待,查到以后释放锁,其他人获到锁,先查缓存,就会有数据,不用去DB
4、加锁解决缓存击穿问题
a、单体版本
package com.xdrs.file.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.xdrs.file.entity.UserInfo;
import com.xdrs.file.feign.UserInfoFeign;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Demo class
*
* @author XDRS
* @date 2022/8/1 15:58
*/
@RequestMapping("/file")
@RestController
public class TestController {
@Autowired
private RedisTemplate redisTemplate;
@GetMapping("/test")
public Map<String, List<UserInfo>> test() {
Map<String, List<UserInfo>> result = null;
// synchronized (this) ;SpringBoot 所有的组件在容器中是单例的。
// TODO 本地锁:synchronized ,JUC (lock) , 在分布式情况下,想要锁住所有,必须使用分布式锁
synchronized (this) {
// 得到锁以后,我们应该再去缓存中确定一次,如果没有才需要继续查询
String userJson = Objects.requireNonNull(redisTemplate.opsForValue().get("userJSON")).toString();
if (StringUtils.isNotEmpty(userJson)) {
// 缓存不为 null 直接返回
result = JSON.parseObject(userJson, new TypeReference<>());
return result;
}
}
return result;
}
}
b、分布式版本
package com.xdrs.file.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* Demo class
*
* @author XDRS
* @date 2022/8/1 15:58
*/
@RequestMapping("/file")
@RestController
public class TestController {
@Autowired
private RedisTemplate redisTemplate;
@GetMapping("/test")
public void test() {
// 1、占分布式锁,去redis占坑
String uuid = UUID.randomUUID().toString();
Boolean lock = redisTemplate.opsForValue().setIfAbsent("lock", uuid, 30, TimeUnit.SECONDS);
if (lock) {
System.out.println("获取分布式锁成功");
try {
// 加锁成功,执行业务
} finally {
String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
// 删除锁
redisTemplate.execute(new DefaultRedisScript<Long>(script, Long.class), Arrays.asList("lock"), uuid);
}
} else {
/**
* 1、加锁失败,重试
* 2、自旋的方式
* 3、休眠100ms重试
*/
System.out.println("获取分布式锁失败。。。等待重试");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
版权声明:本文为qq_32431981原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。