⼀个应⽤⾥⾯可以有多个缓存提供者(CachingProvider),⼀个缓存提供者可以获取多个缓存管理器(CacheManager),⼀个缓存管理器管理着不同的缓存(Cache),缓存中是⼀个个的缓存键值对(Entry),每个键值对都有⼀个有效期(Expiry)。缓存管理器和缓存之间的关系有点类似于数据库中连接池和连接的关系。
2.Spring缓存抽象
2.1 简介
使⽤Spring缓存抽象时我们需要关注以下两点:确认⽅法需要被缓存以及他们的缓存策略
从缓存中读取之前缓存存储的数据
keyGenerator:key的⽣成器,可以⾃⼰指定key的⽣成器的组件id。注意:key/keyGenerator⼆选⼀使⽤
condition:缓存条件,使⽤SpEL编写, 在调⽤⽅法之前之后都能判断
unless(@CachePut,@Cacheable):⽤于是否缓存的条件,只在⽅法执⾏之后判断
beforeInvocation(@CacheEvict):是否在执⾏前清空缓存,默认为false,false的情况下⽅法执⾏异常则不会清空allEntries(@CacheEvict):是否清空所有缓存内容,默认为false。
2.缓存可⽤的SpELl表达式
root:表⽰根对象,不可省略
被调⽤⽅法名methodName,如#hodName
被调⽤⽅法method
⽬标对象target
被调⽤的⽬标对象类targetClass
被调⽤的⽅法的参数列表
⽅法调⽤使⽤的缓存列表caches,如#root.cache[0].name
参数名:⽅法参数的名字,可以直接使⽤#参数名,也可以使⽤#p0
返回值:⽅法执⾏后的返回值,如#result
2.3 缓存使⽤
2.3.1 基本环境搭建
1.创建SpringBoot应⽤:选中Cache,Mysql,MyBatis,Web模块,pom⽂件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId&st</groupId>
<artifactId>spring-boot-cache</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-cache</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<spring-boot.version>2.3.0.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<artifactId>mybatis-spring-boot-starter</artifactId>            <version>2.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-dependencies</artifactId>                <version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>
</plugins>
</build>
</project>
2.创建数据库表
-- ----------------------------
-- Table structure for department
-- ----------------------------
DROP TABLE IF EXISTS `department`;
CREATE TABLE `department` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`departmentName` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for employee
-- ----------------------------
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`lastName` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`gender` int(2) DEFAULT NULL,
`d_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3.创建JavaBean封装数据
public class Employee implements Serializable {
private Integer id;
private String lastName;
private String email;
private Integer gender; //性别 1男  0⼥
private Integer dId;
public Employee() {
super();
}
public Employee(Integer id, String lastName, String email, Integer gender, Integer dId) {  super();
this.id = id;
this.lastName = lastName;
this.dId = dId;
cacheable}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}