谷粒商城学习笔记p156
缓存击穿:单个key缓存突然失效,这时大量的请求进行访问,导致数据库压力过大。缓存击穿是指缓存中没有但数据库中有的数据(一般是缓存时间到期),这时由于并发用户特别多,同时读缓存没读到数据,又同时去数据库去取数据,引起数据库压力瞬间增大。
解决办法:加锁,锁有本地锁,也有分布式锁。每种锁有其优缺点和适用场景
本地锁的优点是使用相对来说较为简便,缺点是在分布式应用中锁不住资源。下面是具体实现:
@Override
public Map<String, List> getCatelogJson() {
String catelogJson = redisTemplate.opsForValue().get("catelogJson");
//如果缓存中不存在,则去查库
if(StringUtils.isBlank(catelogJson)){
System.out.println("缓存未命中......查询数据库..........");
Map<String, List> catelogJsonFromDb = getCatelogJsonFromDb();
String s = JSON.toJSONString(collect);
//将查库获得的数据添加到缓存中
redisTemplate.opsForValue().set("catelogJson",s,1, TimeUnit.DAYS);
return catelogJsonFromDb;
}
System.out.println("缓存命中......直接返回..........");
Map<String, List>map=JSON.parseObject(catelogJson, new TypeReference<Map<String, List>>() {});
return map;
}
查询数据库的方法:
public Map<String, List> getCatelogJsonFromDb() {
//加同步锁,防止缓存击穿
synchronized (this){
//获得锁以后,需要去查询缓存,缓存中不存在,才去查库
String catelogJson = redisTemplate.opsForValue().get("catelogJson");
if(StringUtils.isNotBlank(catelogJson)){
Map<String, List>map = JSON.parseObject(catelogJson, new TypeReference<Map<String, List>>() {});
return map;
}
System.out.println("查询了数据库..........");
List selectList = baseMapper.selectList(null);
//获得一级分类的集合
List level1Categorys = this.getLevel1Categorys();
Map<String, List> collect = level1Categorys.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {
//根据一级分类id查询其下所有的二级分类
List level2Categorys = getCategoryEntities(selectList,v.getCatId());
//封装成前端需要的数据格式
List catelog2Vos = null;
if (!CollectionUtils.isEmpty(level2Categorys)) {
catelog2Vos = level2Categorys.stream().map(level2 -> {
//根据二级分类id查询名下所有三级分类信息
List level3Categorys = getCategoryEntities(selectList,level2.getCatId());
Catelog2Vo catelog2Vo = new Catelog2Vo(v.getCatId().toString(), null, level2.getCatId().toString(), level2.getName());
if (!CollectionUtils.isEmpty(level3Categorys)) {
List catelog3VoList = level3Categorys.stream().map(level3 -> {
Catelog2Vo.Catelog3Vo catelog3Vo = new Catelog2Vo.Catelog3Vo();
catelog3Vo.setCatalog2Id(level2.getCatId().toString());
catelog3Vo.setId(level3.getCatId().toString());
catelog3Vo.setName(level3.getName());
return catelog3Vo;
}).collect(Collectors.toList());
catelog2Vo.setCatalog3List(catelog3VoList);
}
return catelog2Vo;
}).collect(Collectors.toList());
}
return catelog2Vos;
}));
return collect;
}
}
使用上面的方法仍然存在一定的弊端,在实际测试中也发现查询了2次数据库而不是只查1次。原因在于添加数据到缓存和查库不在同一把锁的操作控制内,也行第一个线程查询到数据释放了锁,然后在添加数据到缓存的间隙,第二个线程获得了锁,但数据还没成功完整添加到缓存中,导致再去查询一次数据库。显然这样设计是不合理的,给数据库造成额外的负担。正确的设计是将添加数据到缓存放在查库的同一把锁控制范围内,保证原子性。附改造后的代码:
@Override
public Map<String, List> getCatelogJson() {
String catelogJson = redisTemplate.opsForValue().get("catelogJson");
//如果缓存中不存在,则去查库
if(StringUtils.isBlank(catelogJson)){
System.out.println("缓存未命中......查询数据库..........");
Map<String, List> catelogJsonFromDb = getCatelogJsonFromDb();
return catelogJsonFromDb;
}
System.out.println("缓存命中......直接返回..........");
Map<String, List>map=JSON.parseObject(catelogJson, new TypeReference<Map<String, List>>() {});
return map;
}
查询数据库的方法:
public Map<String, List> getCatelogJsonFromDb() {
//加同步锁,防止缓存击穿
synchronized (this){
//获得锁以后,需要去查询缓存,缓存中不存在,才去查库
String catelogJson = redisTemplate.opsForValue().get("catelogJson");
if(StringUtils.isNotBlank(catelogJson)){
Map<String, List>map = JSON.parseObject(catelogJson, new TypeReference<Map<String, List>>() {});
return map;
}
System.out.println("查询了数据库..........");
List selectList = baseMapper.selectList(null);
//获得一级分类的集合
List level1Categorys = this.getLevel1Categorys();
Map<String, List> collect = level1Categorys.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {
//根据一级分类id查询其下所有的二级分类
List level2Categorys = getCategoryEntities(selectList,v.getCatId());
//封装成前端需要的数据格式
List catelog2Vos = null;
if (!CollectionUtils.isEmpty(level2Categorys)) {
catelog2Vos = level2Categorys.stream().map(level2 -> {
//根据二级分类id查询名下所有三级分类信息
List level3Categorys = getCategoryEntities(selectList,level2.getCatId());
Catelog2Vo catelog2Vo = new Catelog2Vo(v.getCatId().toString(), null, level2.getCatId().toString(), level2.getName());
if (!CollectionUtils.isEmpty(level3Categorys)) {
List catelog3VoList = level3Categorys.stream().map(level3 -> {
Catelog2Vo.Catelog3Vo catelog3Vo = new Catelog2Vo.Catelog3Vo();
catelog3Vo.setCatalog2Id(level2.getCatId().toString());
catelog3Vo.setId(level3.getCatId().toString());
catelog3Vo.setName(level3.getName());
return catelog3Vo;
}).collect(Collectors.toList());
catelog2Vo.setCatalog3List(catelog3VoList);
}
return catelog2Vo;
}).collect(Collectors.toList());
}
return catelog2Vos;
}));
String s = JSON.toJSONString(collect);
//查询到以后立即将数据库查询结果添加到缓存中,防止时间差,导致第2个获得锁的线程继续查库
//将查库和添加到redis缓存中要放在同个锁操作中,保证原子性,
redisTemplate.opsForValue().set("catelogJson",s,1, TimeUnit.DAYS);
return collect;
}
}
改造后经测试,终于只查询了一次数据库,查询完库,添加数据到redis,再释放锁。这样第二个获得锁的线程就可直接从缓存中读数据而无须再次查库。