spring boot cache理论详解
来源:互联网 发布:虚拟机网络连接模式 编辑:程序博客网 时间:2024/06/10 07:08
在上一篇中我们介绍了Spring Boot集成Redis的实战例子,里面使用到了Spring Cache,那么什么是Spring Cache呢,本章将会做一个理论介绍,至于实战的话,可以在上一章节进行实战测试。
Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如EHCache 或者 OSCache、Redis等),而是一个对缓存使用的抽象,通过在既有代码中添加少量它定义的各种 annotation,即能够达到缓存方法的返回对象的效果。
Spring 的缓存技术还具备相当的灵活性,不仅能够使用 SpEL(Spring Expression Language)来定义缓存的 key 和各种 condition,还提供开箱即用的缓存临时存储方案,也支持和主流的专业缓存例如 EHCache 集成
其特点总结如下:
· 通过少量的配置 annotation 注释即可使得既有代码支持缓存
· 支持开箱即用 Out-Of-The-Box,即不用安装和部署额外第三方组件即可使用缓存
· 支持 Spring Express Language,能使用对象的任何属性或者方法来定义缓存的 key 和 condition
· 支持 AspectJ,并通过其实现任何方法的缓存支持
· 支持自定义 key 和自定义缓存管理者,具有相当的灵活性和扩展性
我们以前如何自己实现缓存的呢?
以前我们是在service层通过先判断缓存中是否存在缓存数据,如果存在直接返回数据,如果不存在从数据库中进行查询数据的方式,那么我们会发现这种方式的代码耦合型太高了。如果你之前的代码就是这么设计的,那么你可以考虑使用Spring Cache优化你的代码,你的代码将会变得优雅很多。
实现方式主要在方法上加上注解,可以注解的注解类有:
Cacheable 支持如下几个参数:
value:缓存位置名称,不能为空,如果使用EHCache,就是ehcache.xml中声明的cache的name
key:缓存的key,默认为空,既表示使用方法的参数类型及参数值作为key,支持SpEL
condition:触发条件,只有满足条件的情况才会加入缓存,默认为空,既表示全部都加入缓存,支持SpEL
以下有一个例子:
1
.
//将缓存保存进andCache,并使用参数中的userId加上一个字符串(这里使用方法名称)作为缓存的key
2
.
@Cacheable
(value=
"andCache"
,key=
"#userId + 'findById'"
)
3
.
public
SystemUser findById(String userId) {
4
. SystemUser user = (SystemUser) dao.findById(SystemUser.
class
, userId);
5
.
return
user ;
6
. }
7
.
//将缓存保存进andCache,并当参数userId的长度小于32时才保存进缓存,默认使用参数值及类型作为缓存的key
8
.
@Cacheable
(value=
"andCache"
,condition=
"#userId.length < 32"
)
9
.
public
boolean
isReserved(String userId) {
10
. System.out.println(
"hello andCache"
+userId);
11
.
return
false
;
12
.}
@CacheEvict 支持如下几个参数:
value:缓存位置名称,不能为空,同上
key:缓存的key,默认为空,同上
condition:触发条件,只有满足条件的情况才会清除缓存,默认为空,支持SpEL
allEntries:true表示清除value中的全部缓存,默认为false
以下是一个小例子:
1
.
//清除掉指定key的缓存
2
.
@CacheEvict
(value=
"andCache"
,key=
"#user.userId + 'findById'"
)
3
.
public
void
modifyUserRole(SystemUser user) {
4
. System.out.println(
"hello andCache delete"
+user.getUserId());
5
. }
6
.
7
.
//清除掉全部缓存
8
.
@CacheEvict
(value=
"andCache"
,allEntries=
true
)
9
.
public
void
setReservedUsers() {
10
. System.out.println(
"hello andCache deleteall"
);
11
.}
一般来说,我们的更新操作只需要刷新缓存中某一个值,所以定义缓存的key值的方式就很重要,最好是能够唯一,因为这样可以准确的清除掉特定的缓存,而不会影响到其它缓存值,
比如我这里针对用户的操作,使用(userId+方法名称)的方式设定key值,当然,你也可以找到更适合自己的方式去设定。
@CachePut 注释,这个注释可以确保方法被执行,同时方法的返回值也被记录到缓存中,实现缓存与数据库的同步更新,理解为update语句。
- spring boot cache理论详解
- 36. Spring Boot Cache理论篇【从零开始学Spring Boot】
- 36. Spring Boot Cache理论篇【从零开始学Spring Boot】
- spring boot日志理论详解
- (36)Spring Boot Cache理论篇【从零开始学Spring Boot】
- Spring boot配置cache
- Spring Boot集成cache
- #spring boot 3 -- cache
- spring boot 配置Cache
- 【理论】深入学习spring boot
- Spring-boot中使用Cache
- Spring boot配置cache(ehcache)
- Spring Boot 数据缓存 Cache
- Spring Boot+Spring Data JPA+Spring Cache
- spring boot spring cache ehcache3.x整合
- spring boot中spring cache 整合redis
- Spring Cache抽象详解
- Spring Cache抽象详解
- 如何将 iOS 项目的编译速度提高5倍
- /MD, /MDD, /ML, /MT,/MTD(使用运行时库)
- HDU3411 Snail Alice【矩阵快速幂】
- 不得不看!史上最全的三十多张架构师图谱!
- java webservice
- spring boot cache理论详解
- 事件委托
- C#连接sql server数据库
- DNS | Array | Object | Map | Set | iterable ---[廖雪峰老师js教程笔记]
- 637. Average of Levels in Binary Tree
- Block的内部实现原理
- java的持久化技术选型
- win7 批处理文件默认以管理员身份运行及清除IE缓存脚本
- Unity制作游戏中技能使用后转CD的场景