Spring Boot 缓存整合 redis

1.创建子模块

这里我们创建一个子模块

1
2
group = 'com.ray.study'
artifact ='spring-boot-07-cache-redis'

2.引入依赖

2.1 继承父工程依赖

在父工程spring-boot-seedssettings.gradle加入子工程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
rootProject.name = 'spring-boot-seeds'
include 'spring-boot-01-helloworld'
include 'spring-boot-02-restful-test'
include 'spring-boot-03-thymeleaf'
include 'spring-boot-04-swagger2'
include 'spring-boot-05-jpa'
include 'spring-boot-05-mybatis'
include 'spring-boot-05-tk-mybatis'
include 'spring-boot-06-nosql-redis'
include 'spring-boot-06-nosql-mongodb'
include 'spring-boot-07-cache-concurrentmap'
include 'spring-boot-07-cache-ehcache'
include 'spring-boot-07-cache-caffeine'
include 'spring-boot-07-cache-redis'

这样,子工程spring-boot-07-cache-redis就会自动继承父工程中subprojects `函数里声明的依赖,主要包含如下依赖:

1
2
3
4
5
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

2.2 引入redis依赖

将子模块spring-boot-07-cache-redisbuild.gradle修改为如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
dependencies {
// spring-boot-starter-cache
implementation 'org.springframework.boot:spring-boot-starter-cache'

// redis 依赖 和 common pool2 ,要使用redis连接池就需要引入此common pool2
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.apache.commons:commons-pool2'


// mysql 驱动和 jpa
implementation 'mysql:mysql-connector-java'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

}

3.修改配置

3.1 修改application.yml

关于 redis cache 的可以使用默认配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server:
port: 8088
servlet:
context-path: /

spring:
datasource: # 配置数据源
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/integrate-jpa?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
username: root
password: root
jpa: # 配置jpa
database: mysql # 数据库类型
show-sql: true # 打印sql语句
hibernate:
ddl-auto: update # 加载 Hibernate时, 自动更新数据库结构

3.2 CacheConfiguration

开启缓存支持

通常SpringBoot默认的keyGenerator 是SimpleKeyGenerator,这个策略是以参数作为key值,如果参数为空的,就会返回SimpleKey[]字符串,这对于很多无参的方法的就有问题了.
向Spring容器中重新注册一个实现了 org.springframework.cache.interceptor.keyGenerator 这个接口的Bean即可,将key值设置为类名+方法名+参数名,这样就不会冲突了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.ray.study.springboot07cache.caffeine.config;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
* Spring cache 配置类
*
*/
@Configuration
@EnableCaching
public class CacheConfiguration {

@Bean
public KeyGenerator caffeineKeyGenerator() {
return (target, method, params) -> { // KeyGenerator#generate
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
};
}



}

3.其他部分

其他部分(数据库准备、业务实现、单元测试)参加: SpringBoot_07_Cache_01_快速入门

不过请注意要缓存的JavaBean必须实现Serializable接口,因为Spring会将对象先序列化再存入 Redis,因此

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Getter
@Setter
@NoArgsConstructor
@MappedSuperclass
public class BaseDO implements Serializable { // BaseDO需要实现Serializable接口,否则会抛无法序列化的异常

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@JsonIgnore
private Date creationDate;

@JsonIgnore
private Date lastUpdateDate;
}