缓存穿透:⼤量的请求⼀个数据库中不存在的数据,⾸先在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);
}