Spring Boot+Mybatis Plus 使用Redis实现二级缓存具体步骤以及代码
时间: 2023-05-19 19:07:16 浏览: 221
非常感谢您的提问。以下是 Spring Boot Mybatis Plus 使用 Redis 实现二级缓存的具体步骤和代码:
1. 首先,在 pom.xml 文件中添加 Redis 相关依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
```
2. 在 application.properties 文件中添加 Redis 相关配置:
```
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.database=0
spring.redis.password=
spring.redis.timeout=3000
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=-1
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
```
3. 在 Mybatis Plus 的配置文件中开启二级缓存,并配置 Redis 缓存:
```
@Configuration
@MapperScan("com.example.mapper")
public class MybatisPlusConfig {
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return new ConfigurationCustomizer() {
@Override
public void customize(Configuration configuration) {
// 开启二级缓存
configuration.setCacheEnabled(true);
// 配置 Redis 缓存
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(30)); // 设置缓存过期时间为 30 分钟
configuration.addCache(new RedisCache("mybatis-plus", new RedisCacheWriter() {
@Override
public void put(String key, byte[] value) {
redisTemplate().opsForValue().set(key, value, Duration.ofMinutes(30));
}
@Override
public byte[] get(String key) {
return redisTemplate().opsForValue().get(key);
}
@Override
public void put(String key, byte[] value, long time, TimeUnit unit) {
redisTemplate().opsForValue().set(key, value, Duration.ofMillis(unit.toMillis(time)));
}
@Override
public void delete(String key) {
redisTemplate().delete(key);
}
@Override
public void clean() {
redisTemplate().getConnectionFactory().getConnection().flushDb();
}
@Override
public long size() {
return redisTemplate().getConnectionFactory().getConnection().dbSize();
}
}, redisCacheConfiguration));
}
};
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
return redisTemplate;
}
@Bean
public RedisConnectionFactory redisConnectionFactory() {
LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory();
lettuceConnectionFactory.setHostName("127.0.0.1");
lettuceConnectionFactory.setPort(6379);
lettuceConnectionFactory.setPassword("");
lettuceConnectionFactory.setDatabase(0);
return lettuceConnectionFactory;
}
}
```
4. 在需要使用二级缓存的 Mapper 中添加 @CacheNamespace 注解:
```
@CacheNamespace(implementation = MybatisRedisCache.class, eviction = MybatisRedisCache.class)
public interface UserMapper extends BaseMapper<User> {
// ...
}
```
5. 最后,实现 MybatisRedisCache 类,继承自 RedisCache,重写 clear 方法:
```
public class MybatisRedisCache extends RedisCache {
public MybatisRedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration configuration) {
super(name, cacheWriter, configuration);
}
@Override
public void clear() {
RedisConnection connection = Objects.requireNonNull(getRedisCacheWriter().getRedisConnectionFactory().getConnection());
connection.flushDb();
connection.close();
}
}
```
以上就是 Spring Boot Mybatis Plus 使用 Redis 实现二级缓存的具体步骤和代码。希望能对您有所帮助!
阅读全文