SpringBoot缓存
作用
将运行结果缓存,以后再要相同的数据,直接从缓存中获取,不用再调用方法;
springboot默认使用的是ConCurrentMapManeger
重要知识点
注解讲解
1.@Cacheable:添加缓存组件,一般在方法执行前执行,先查询cache组件map,有则直接用没有则执行方法
@Cacheable(cacheNames = {
"dept"},keyGenerator = "myKeyGenerator")
public Department getDeptById(Integer id){
Department department = departmentMapper.getDeptById(id);
return department;
}
2.@CachePut:添加缓存,先执行方法再更新缓存,用于更新数据库
@CachePut(value = {
"dept"})
public Department updateDept(Department department){
departmentMapper.updateDept(department);
System.out.println("service方法执行");
return department;
}
3.@CacheEvict:删除缓存
@CacheEvict(cacheNames = "dept",key = "#id")
public void delDept(Integer id){
departmentMapper.deleteDeptById(id)