本章介绍SpringBoot的缓存机制及使用
Spring从3.1开始定义了org.springframework.cache.Cache 和org.springframework.cache.CacheManager接口来统一不同的缓存技术; 并支持使用JCache(JSR-107)注解简化我们开发;
• Cache接口为缓存的组件规范定义,包含缓存的各种操作集合;
• Cache接口下Spring提供了各种xxxCache的实现;如RedisCache,EhCacheCache,ConcurrentMapCache等;
• 每次调用需要缓存功能的方法时,Spring会检查检查指定参数的指定的目标方法是否 已经被调用过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法 并缓存结果后返回给用户。下次调用直接从缓存中获取。
• 使用Spring缓存抽象时我们需要关注以下两点;
1、确定方法需要被缓存以及他们的缓存策略
2、从缓存中读取之前缓存存储的数据
几个重要概念&缓存注解
缓存组件关系图
|
Cache |
缓存接口,定义缓存操作。实现有:RedisCache、EhCacheCache、ConcurrentMapCache等 |
|
CacheManager |
缓存管理器,管理各种缓存(Cache)组件 |
|
@Cacheable |
主要针对方法配置,能够根据方法的请求参数对其结果进行缓存 |
|
@CacheEvict |
清空缓存 |
|
@CachePut |
保证方法被调用,又希望结果被缓存。 |
|
@EnableCaching |
开启基于注解的缓存 |
|
keyGenerator |
缓存数据时key生成策略 |
|
serialize |
缓存数据时value序列化策略 |
缓存注解的主要参数
|
@Cacheable/@CachePut/@CacheEvict 主要的参数 |
||
|
value |
缓存的名称,在 spring 配置文件中定义,必须指定 至少一个 |
例如: @Cacheable(value=”mycache”) 或者 @Cacheable(value={”cache1”,”cache2”} |
|
key |
缓存的 key,可以为空,如果指定要按照 SpEL 表达 式编写,如果不指定,则缺省按照方法的所有参数 进行组合 |
例如: @Cacheable(value=”testcache”,key=”#userName”) |
|
condition |
缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存/清除缓存,在 调用方法之前之后都能判断 |
例如: @Cacheable(value=”testcache”,condition=”#userNam e.length()>2”) |
|
allEntries (@CacheEvict ) |
是否清空所有缓存内容,缺省为 false,如果指定为 true,则方法调用后将立即清空所有缓存 |
例如: @CachEvict(value=”testcache”,allEntries=true) |
|
beforeInvocation (@CacheEvict) |
是否在方法执行前就清空,缺省为 false,如果指定 为 true,则在方法还没有执行的时候就清空缓存, 缺省情况下,如果方法执行抛出异常,则不会清空 缓存 |
例如: @CachEvict(value=”testcache”, beforeInvocation=true) |
|
unless (@CachePut) (@Cacheable) |
用于否决缓存的,不像condition,该表达式只在方 法执行之后判断,此时可以拿到返回值result进行判 断。条件为true不会缓存,fasle才缓存 |
例如: @Cacheable(value=”testcache”,unless=”#result == null”) |
缓存注解的SpEL(Spring Expression Language)
|
名字 |
位置 |
描述 |
示例 |
|
methodName |
root object |
当前被调用的方法名 |
#root.methodName |
|
method |
root object |
当前被调用的方法 |
#root.method.name |
|
target |
root object |
当前被调用的目标对象 |
#root.target |
|
targetClass |
root object |
当前被调用的目标对象类 |
#root.targetClass |
|
args |
root object |
当前被调用的方法的参数列表 |
#root.args[0] |
|
caches |
root object |
当前方法调用使用的缓存列表(如@Cacheable(value={"cache1", "cache2"})),则有两个cache |
#root.caches[0].name |
|
argument name |
evaluation context |
方法参数的名字. 可以直接 #参数名 ,也可以使用 #p0或#a0 的形式,0代表参数的索引; |
#iban 、 #a0 、 #p0 |
|
result |
evaluation context |
方法执行后的返回值(仅当方法执行之后的判断有效,如‘unless’,’cache put’的表达式 ’cache evict’的表达式beforeInvocation=false) |
#result |
缓存使用
项目搭建
1、新建一个Spring Web工程,整合了mybatis,pom.xml文件如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.test</groupId> 8 <artifactId>test-springboot-cache</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <parent> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-parent</artifactId> 14 <version>2.1.8.RELEASE</version> 15 </parent> 16 17 <properties> 18 19 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 20 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 21 <java.version>1.8</java.version> 22 </properties> 23 24 <dependencies> 25 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-starter-web</artifactId> 29 </dependency> 30 31 <dependency> 32 <groupId>org.mybatis.spring.boot</groupId> 33 <artifactId>mybatis-spring-boot-starter</artifactId> 34 <version>2.0.1</version> 35 </dependency> 36 37 <!-- mysql --> 38 <dependency> 39 <groupId>mysql</groupId> 40 <artifactId>mysql-connector-java</artifactId> 41 <version>8.0.12</version> 42 </dependency> 43 44 <dependency> 45 <groupId>org.springframework.boot</groupId> 46 <artifactId>spring-boot-starter-test</artifactId> 47 <scope>test</scope> 48 </dependency> 49 50 </dependencies> 51 52 53 <!-- SpringBoot打包插件,可以将代码打包成一个可执行的jar包 --> 54 <build> 55 <plugins> 56 <plugin> 57 <groupId>org.springframework.boot</groupId> 58 <artifactId>spring-boot-maven-plugin</artifactId> 59 </plugin> 60 </plugins> 61 </build> 62 63 </project>