介绍
- Spring Cache 是一个框架,实现了基于注解的缓存功能,只需要简单地加一个注解,就能实现缓存功能
- Spring Cache 提供了一层抽象,底层可以切换不同的 cache 实现。具体就是通过 CacheManager 接口来统一不同的缓存技术。
- CacheManager 是 Spring 提供的各种缓存技术抽象接口。
针对不同的缓存技术需要实现不同的CacheManager
- EhCacheCacheManager:使用 EhCache 作为缓存技术
- GuavaCacheManager:使用 Google的GuavaCache 作为缓存技术
- RedisCacheManager 使用 Redis 作为缓存技术
常用注解
注解 | 说明 |
---|---|
@EnableCaching | 开启缓存注解功能 |
@Cacheable | 在方法执行前 spring 先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中 |
@CachePut | 将方法的返回值放到缓存中 |
@CacheEvict | 将一条或多条数据从缓存中删除 |
在 spring boot 项目中,使用缓存技术只需在项目中导入相关缓存技术的依赖包,并在启动类上使用 @EnableCaching 开启缓存支持即可
例如,使用 Redis 作为缓存技术,只需要导入 Spring data Redis 的 maven 坐标即可
使用自带基础的缓存
map实现的缓存,基础内存的,重启后便清除
1、只需导入 spring-boot-starter-web 启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>compile</scope>
</dependency>
2、启动类上添加 @EnableCaching 注解
3、注入 CacheManager
@Autowired
private CacheManager cacheManager;
4、给对应的实体类实现序列化接口
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
……………………
}
5、在控制层中使用
/**
* CachePut:将方法返回值放入缓存
* value:缓存的名称,每个缓存名称下面可以有多个key
* key:缓存的key,支持spEl表达式,#user代表传递的操作后的参数,#result代表返回值
*/
@CachePut(value = "userCache",key = "#user.id")
@PostMapping
public User save(User user){
userService.save(user);
return user;
}
/**
* CacheEvict:清理指定缓存
* value:缓存的名称,每个缓存名称下面可以有多个key
* key:缓存的key,#p0、#root.args[0]代表第一个参数
*/
@CacheEvict(value = "userCache",key = "#p0")
//@CacheEvict(value = "userCache",key = "#root.args[0]")
//@CacheEvict(value = "userCache",key = "#id")
//@CacheEvict(value = "userCache",allEntries = true)//删除掉所有userCache中缓存
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id){
userService.removeById(id);
}
//@CacheEvict(value = "userCache",key = "#p0.id")
//@CacheEvict(value = "userCache",key = "#user.id")
//@CacheEvict(value = "userCache",key = "#root.args[0].id")
@CacheEvict(value = "userCache",key = "#result.id")
@PutMapping
public User update(User user){
userService.updateById(user);
return user;
}
/**
* Cacheable:在方法执行前spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中
* value:缓存的名称,每个缓存名称下面可以有多个key
* key:缓存的key
* 当返回数据为null时,也会返回数据,可以使用下面两个属性解决
* condition:条件,满足条件时才缓存数据
* unless:满足条件则不缓存
*/
@Cacheable(value = "userCache",key = "#id",unless = "#result == null")
@GetMapping("/{id}")
public User getById(@PathVariable Long id){
User user = userService.getById(id);
return user;
}
@Cacheable(value = "userCache",key = "#user.id + '_' + #user.name")
@GetMapping("/list")
public List<User> list(User user){
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(user.getId() != null,User::getId,user.getId());
queryWrapper.eq(user.getName() != null,User::getName,user.getName());
List<User> list = userService.list(queryWrapper);
return list;
}
使用Redis缓存数据
1、导入maven坐标
spring-boot-starter-data-redis、spring-boot-starter-cache
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、配置application.yml
spring:
redis: #根据自己情况配置
host: 172.15.8.99
port: 6379
password: root
database: 0
cache:
redis:
time-to-live: 1800000 #设置缓存过期时间,可选
3、在启动类上加入@EnableCaching注解,开启缓存注解功能
4、注入 CacheManager
@Autowired
private CacheManager cacheManager;
5、给对应的实体类实现序列化接口
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
……………………
}
6、在Controller中的方法上加入@Cacheable、@CacheEvict等注解,进行缓存操作
使用方式同上
注意:redis的condition没有#result的使用,unless中可以使用#result