版本:
- spring-boot:2.3.0
- redis:latest(Docker)
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/spring_boot?serverTimezone=GMT%2B8
type: com.mysql.cj.jdbc.MysqlDataSource
jpa:
show-sql: true
database: mysql
hibernate:
ddl-auto: update
redis:
host: 127.0.0.1
cache:
redis:
use-key-prefix: true
在spring-boot2.x之前
RedisCacheManager cacheManager = new RedisCacheManager(RedisTemplate redisTemplate);
spring-boot2.0后
RedisCacheManager cacheManager = new RedisCacheManager(RedisCacheWriter redisCacheWriter,RedisCacheConfiguration redisCacheConfiguration);
步骤:
-
开始:
写一个配置类
- 给容器注册一个Bean,返回缓存管理器
- 这里redisTemplate容器中有,所以会自动注入
@Configuration public class CustomizeRedisCacheManager { @Bean public CacheManager customizeCacheManager(RedisTemplate redisTemplate)