上一篇文章介绍了通过自定义注解实现添加有效时间,接下来就说说通过重新实现CacheManager添加有效时间。
1.jar包依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
2.redis配置
spring:
application:
name: redis
redis:
host: 10.17.1.61 #redis服务器地址
timeout: 10000 #超时时间
database: 9 #0-15 16个库 默认0
port: 6379
lettuce:
pool:
max-active: 8 #最大连接数
max-wait: -1 #默认-1 最大连接阻塞等待时间
max-idle: 8 #最大空闲连接 默认8
min-idle: 0 #最小空闲连接
long_cache_time: 30
short_cache_time: 20
default_cache_time: 10
3.创建RedisCacheConfig继承CachingConfigurerSupport
通过重新定义RedisCacheConfiguration配置过期时间
package com.atem.redis.baobao;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class RedisCacheConfig extends CachingConfigurerSupport {
/**
@Description: 申明缓存管理器, 会创建一个切面(Aspect)并触发Spring缓存注解的切点(Pointcut)
@param redisConnectionFactory
@return
*/
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
return new RedisCacheManager(
RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
this.getRedisCacheConfigurationWithTtl(246060), // 默认缓存时间策略
this.getRedisCacheConfigurationMap()); // 指定缓存时间策略
}
/**
@Description: 设置缓存时间, 设置key与value的序列化.
可设置禁止缓存null: redisCacheConfiguration.disableCachingNullValues(), 若有null缓存, 则会报错.
@param seconds 缓存时间, 单位: 秒.
@return
*/
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {
return RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair
.fromSerializer(new Jackson2JsonRedisSerializer<>(String.class)))
.serializeValuesWith(RedisSerializationContext.SerializationPair
.fromSerializer(new Jackson2JsonRedisSerializer<>(Object.class)))
.entryTtl(Duration.ofSeconds(seconds));
}
/**
@Description: 可以自定义缓存时间,
@return
*/
private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
redisCacheConfigurationMap.put("short_cache", this.getRedisCacheConfigurationWithTtl(30));
redisCacheConfigurationMap.put("long_cache", this.getRedisCacheConfigurationWithTtl(60));
return redisCacheConfigurationMap;
}
4.同样不要忘了开启缓存
5.还可以重新实现keyGenerator自定义key生成
@Override
public KeyGenerator keyGenerator() {
return (target, method, params) -> {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(target.getClass().getCanonicalName());
stringBuilder.append(":");
stringBuilder.append(method.getName());
stringBuilder.append(":");
for (int i = 0; i < params.length; i++) {
stringBuilder.append(params[i]);
if (i < params.length - 1) {
stringBuilder.append(",");
}
}
return stringBuilder.toString();
};
}
@Cacheable的注解使用没有做改变,只需要注意注解的value与配置的RedisCacheConfiguration对应即可。