Spring缓存注解@Cacheable @CacheEvit @CachePut使用介绍
作者:一灰灰 时间:2022-06-05 01:47:14
目录
I. 项目环境
1. 项目依赖
II. 缓存注解介绍
1. @Cacheable
2. @CachePut
3. @CacheEvict
4. @Caching
5. 异常时,缓存会怎样?
6. 测试用例
7. 小结
III. 不能错过的源码和相关知识点
0. 项目
Spring在3.1版本,就提供了一条基于注解的缓存策略,实际使用起来还是很丝滑的,本文将针对几个常用的注解进行简单的介绍说明,有需要的小伙伴可以尝试一下
本文主要知识点:
@Cacheable: 缓存存在,则使用缓存;不存在,则执行方法,并将结果塞入缓存
@CacheEvit: 失效缓存
@CachePut: 更新缓存
I. 项目环境
1. 项目依赖
本项目借助SpringBoot 2.2.1.RELEASE + maven 3.5.3 + IDEA + redis5.0进行开发
开一个web服务用于测试
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
全程使用默认配置,redis本机,端口6379,无密码
II. 缓存注解介绍
1. @Cacheable
这个注解用于修饰方法or类,当我们访问它修饰的方法时,优先从缓存中获取,若缓存中存在,则直接获取缓存的值;缓存不存在时,执行方法,并将结果写入缓存
这个注解,有两个比较核心的设置
/**
* 与 cacheNames 效果等价
*/
@AliasFor("cacheNames")
String[] value() default {};
@AliasFor("value")
String[] cacheNames() default {};
/**
* 缓存key
*/
String key() default "";
cacheNames可以理解为缓存key的前缀,可以为组件缓存的key变量;当key不设置时,使用方法参数来初始化,注意key为SpEL表达式,因此如果要写字符串时,用单引号括起来
一个简单的使用姿势
/**
* 首先从缓存中查,查到之后,直接返回缓存数据;否则执行方法,并将结果缓存
* <p>
* redisKey: cacheNames + key 组合而成 --> 支持SpEL
* redisValue: 返回结果
*
* @param name
* @return
*/
@Cacheable(cacheNames = "say", key = "'p_'+ #name")
public String sayHello(String name) {
return "hello+" + name + "-->" + UUID.randomUUID().toString();
}
如我们传参为 yihuihui, 那么缓存key为 say::p_yihuihui
除了上面三个配置值之外,查看@Cacheable注解源码的童鞋可以看到还有condition设置,这个表示当它设置的条件达成时,才写入缓存
/**
* 满足condition条件的才写入缓存
*
* @param age
* @return
*/
@Cacheable(cacheNames = "condition", key = "#age", condition = "#age % 2 == 0")
public String setByCondition(int age) {
return "condition:" + age + "-->" + UUID.randomUUID().toString();
}
上面这个case中,age为偶数的时候,才走缓存;否则不写缓存
接下来是unless参数,从名字上可以看出它表示不满足条件时才写入缓存
/**
* unless, 不满足条件才写入缓存
*
* @param age
* @return
*/
@Cacheable(cacheNames = "unless", key = "#age", unless = "#age % 2 == 0")
public String setUnless(int age) {
return "unless:" + age + "-->" + UUID.randomUUID().toString();
}
2. @CachePut
不管缓存有没有,都将方法的返回结果写入缓存;适用于缓存更新
/**
* 不管缓存有没有,都写入缓存
*
* @param age
* @return
*/
@CachePut(cacheNames = "t4", key = "#age")
public String cachePut(int age) {
return "t4:" + age + "-->" + UUID.randomUUID().toString();
}
3. @CacheEvict
这个就是我们理解的删除缓存
/**
* 失效缓存
*
* @param name
* @return
*/
@CacheEvict(cacheNames = "say", key = "'p_'+ #name")
public String evict(String name) {
return "evict+" + name + "-->" + UUID.randomUUID().toString();
}
4. @Caching
在实际的工作中,经常会遇到一个数据变动,更新多个缓存的场景,对于这个场景,可以通过@Caching来实现
/**
* caching实现组合,添加缓存,并失效其他的缓存
*
* @param age
* @return
*/
@Caching(cacheable = @Cacheable(cacheNames = "caching", key = "#age"), evict = @CacheEvict(cacheNames = "t4", key = "#age"))
public String caching(int age) {
return "caching: " + age + "-->" + UUID.randomUUID().toString();
}
上面这个就是组合操作
从 caching::age缓存取数据,不存在时执行方法并写入缓存;
失效缓存 t4::age
5. 异常时,缓存会怎样?
上面的几个case,都是正常的场景,当方法抛出异常时,这个缓存表现会怎样?
/**
* 用于测试异常时,是否会写入缓存
*
* @param age
* @return
*/
@Cacheable(cacheNames = "exception", key = "#age")
@Cacheable(cacheNames = "say", key = "'p_yihuihui'")
public int exception(int age) {
return 10 / age;
}
根据实测结果,当age==0时,上面两个缓存都不会成功
6. 测试用例
接下来验证下缓存注解与上面描述的是否一致
@RestController
public class IndexRest {
@Autowired
private BasicDemo helloService;
@GetMapping(path = {"", "/"})
public String hello(String name) {
return helloService.sayHello(name);
}
}
上面这个主要是验证@Cacheable注解,若缓存不命中,每次返回的结果应该都不一样,然而实际访问时,会发现返回的都是相同的
curl http://localhost:8080/?name=yihuihui
失效缓存
@GetMapping(path = "evict")
public String evict(String name) {
return helloService.evict(String.valueOf(name));
}
失效缓存,需要和上面的case配合起来使用
curl http://localhost:8080/evict?name=yihuihui
curl http://localhost:8080/?name=yihuihui
剩下其他的相关测试类就比较好理解了,一并贴出对应的代码
@GetMapping(path = "condition")
public String t1(int age) {
return helloService.setByCondition(age);
}
@GetMapping(path = "unless")
public String t2(int age) {
return helloService.setUnless(age);
}
@GetMapping(path = "exception")
public String exception(int age) {
try {
return String.valueOf(helloService.exception(age));
} catch (Exception e) {
return e.getMessage();
}
}
@GetMapping(path = "cachePut")
public String cachePut(int age) {
return helloService.cachePut(age);
}
7. 小结
最后管理小结一下Spring提供的几个缓存注解
@Cacheable: 缓存存在,则从缓存取;否则执行方法,并将返回结果写入缓存
@CacheEvit: 失效缓存
@CachePut: 更新缓存
@Caching: 都注解组合
上面虽说可以满足常见的缓存使用场景,但是有一个非常重要的点没有说明,缓存失效时间应该怎么设置???
如何给每个缓存设置不同的缓存失效时间,咱么下篇博文见,我是一灰灰,欢迎关注长草的公众号一灰灰blog
III. 不能错过的源码和相关知识点
0. 项目
工程:https://github.com/liuyueyi/spring-boot-demo
源码:https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/125-cache-ano
来源:https://juejin.cn/post/6980288897154547720
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java Map接口及其实现类原理解析
javaweb Servlet开发总结(二)
![](https://img.aspxhome.com/file/2023/7/58567_0s.png)
Spring AOP底层原理及代理模式
java设计模式学习之工厂方法模式
![](https://img.aspxhome.com/file/2023/7/58607_0s.jpg)
详解Java中Optional类的使用方法
![](https://img.aspxhome.com/file/2023/4/59694_0s.png)
java搭建ftp/sftp进行数据传递的全过程
![](https://img.aspxhome.com/file/2023/6/60486_0s.png)
mybatis-plus 返回部分字段的解决方式
![](https://img.aspxhome.com/file/2023/1/61281_0s.jpg)
SpringMVC中使用@PathVariable绑定路由中的数组的方法
![](https://img.aspxhome.com/file/2023/9/61289_0s.png)
SpringBoot整合Apollo配置中心快速使用详解
![](https://img.aspxhome.com/file/2023/7/61337_0s.jpg)
浅谈c++性能测试工具之计算时间复杂度
![](https://img.aspxhome.com/file/2023/4/79524_0s.jpg)
IDEA GIT 忽略文件的最佳方式推荐
![](https://img.aspxhome.com/file/2023/6/63436_0s.jpg)
SpringBoot如何访问html和js等静态资源配置
![](https://img.aspxhome.com/file/2023/7/63787_0s.png)
java读写二进制文件的解决方法
C++高并发内存池的整体设计和实现思路
![](https://img.aspxhome.com/file/2023/3/103083_0s.png)
java获取json中的全部键值对实例
Springboot 内部服务调用方式
![](https://img.aspxhome.com/file/2023/6/58426_0s.png)
Java实现简单的五子棋小游戏
![](https://img.aspxhome.com/file/2023/2/59892_0s.jpg)
IDEA设置背景为自定义照片的操作方法
![](https://img.aspxhome.com/file/2023/5/61645_0s.jpg)
利用Java写一个学生管理系统
![](https://img.aspxhome.com/file/2023/1/61071_0s.jpg)