一、Spring Cache框架介绍
Spring Cache是一个框架,实现了基于注解的缓存功能,只需要简单地加一个注解,就能实现缓存功能,大大简化我们在业务中操作缓存的代码。
Spring Cache只是提供了一层抽象,底层可以切换不同的cache实现。具体就是通过CacheManager接口来统一不同的缓存技术。CacheManager是Spring提供的各种缓存技术抽象接口。
注意:实际上数据是缓存在ConcurrentHashMap中,那么当我们的服务器重启之后,缓存中的数据就会丢失。
我们后面集成了Redis来缓存就不存在这样的问题了。
1、针对不同的缓存技术需要实现不同的CacheManager:
| CacheManager | 描述 |
|---|---|
| EhCacheCacheManager | 使用EhCache作为缓存技术 |
| GuavaCacheManager | 使用Google的GuavaCache作为缓存技术 |
| RedisCacheManager | 使用Redis作为缓存技术 |
2、注解
在SpringCache中提供了很多缓存操作的注解,常见的是以下的几个:
| 注解 | 说明 |
|---|---|
| @EnableCaching | 开启缓存注解功能 |
| @Cacheable | 在方法执行前spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中 |
| @CachePut | 将方法的返回值放到缓存中 |
| @CacheEvict | 将一条或多条数据从缓存中删除 |
注解详解
1. @CachePut
作用: 将方法返回值,放入缓存 (一般用于添加方法上,将方法返回的数据放到到缓存中)
value: 缓存的名称, 每个缓存名称下面可以有很多key
key: 缓存的key ----------> 支持Spring的表达式语言SPEL语法
举例:@CachePut(value = "userCache", key = "#user.id")
key的写法如下:
#user.id : #user指的是方法形参的名称, id指的是user的id属性 , 也就是使用user的id属性作为key ;
#user.name: #user指的是方法形参的名称, name指的是user的name属性 ,也就是使用user的name属性作为key ;
#result.id : #result代表方法返回值,该表达式 代表以返回对象的id属性作为key ;
#result.name : #result代表方法返回值,该表达式 代表以返回对象的name属性作为key ;
-----------------------------------------------------------------------------------------------------
2. @CacheEvict (一般用于delete方法中)
作用: 清理指定缓存
value: 缓存的名称,每个缓存名称下面可以有多个key
key: 缓存的key ----------> 支持Spring的表达式语言SPEL语法
举例:@CacheEvict(value = "userCache",key = "#p0") //#p0 代表第一个参数
key的写法如下:
#p0 //代表第一个参数
#root.args[0] //代表第一个参数
#id //代表变量名为id的参数
#root.args[0].id //第一个参数的id属性
#result.id //返回值的id属性
-----------------------------------------------------------------------------------------------------
3. @Cacheable
作用:
在方法执行前,spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;
若没有数据,调用方法并将方法返回值放到缓存中
value: 缓存的名称,每个缓存名称下面可以有多个key
key: 缓存的key ----------> 支持Spring的表达式语言SPEL语法
二、SpringCache 集成Redis 实现数据缓存
1、数据缓存实现思路
1). 导入Spring Cache和Redis相关maven坐标
2). 在application.yml中配置缓存数据的过期时间
3). 在启动类上加入@EnableCaching注解,开启缓存注解功能
4). 在list方法上加入@Cacheable注解
5). 在save和delete方法上加入CacheEvict注解
1、导入Cache和redis坐标
<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、在配置文件中配置相关属性
spring:
redis:
host: 192.168.200.200
port: 6379
password: root@123456
database: 0
cache:
redis:
time-to-live: 1800000 #设置缓存过期时间,可选
3、在启动类上加入@EnableCaching注解,开启缓存注解功能
4、在方法中添加注解
1. 查询方法中添加@Cacheable注解
2. 在删除和添加方法上添加@CacheEvict注解
举例:
1.查询方法
@GetMapping("/list")
@Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_' + #setmeal.status")
public R<List<Setmeal>> list(Setmeal setmeal){
...
return R.success(list);
}
2.删除方法
@DeleteMapping
@CacheEvict(value = "setmealCache",allEntries = true) //清除setmealCache名称下,所有的缓存数据
public R<String> delete(@RequestParam List<Long> ids){
...
return R.success("删除成功");
}
3.新增方法
@PostMapping
@CacheEvict(value = "setmealCache",allEntries = true) //清除setmealCache名称下,所有的缓存数据
public R<String> save(@RequestBody SetmealDto setmealDto){
...
return R.success("添加成功");
}