在springboot1.x时自定义RedisCacheManager是使用RestTemplate作为参数注入的:
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
//设置缓存过期时间
cacheManager.setDefaultExpiration(10000);
return cacheManager;
}
在springboot2.X时自定义CacheManager有很大的不同,RedisCacheManager构造器如下:
看出已经没有RestTemplate作为参数的构造器了,
作为初学者的我,这让我琢磨了很久,最终在官网找到了答案:
官网文档介绍springboot缓存时有这么一段话:
说明现在配置RedisCacheManager需要一个RedisCacheConfiguration来作为配置对象,再看RedisCacheConfiguration的源码:
/**
* Define the {@link SerializationPair} used for de-/serializing cache values.
*
* @param valueSerializationPair must not be {@literal null}.
* @return new {@link RedisCacheConfiguration}.
*/
public RedisCacheConfiguration serializeValuesWith(SerializationPair<?> valueSerializationPair) {
Assert.notNull(valueSerializationPair, "ValueSerializationPair must not be null!");
return new RedisCacheConfiguration(ttl, cacheNullValues, usePrefix, keyPrefix, keySerializationPair,
valueSerializationPair, conversionService);
}
就是这个方法可以设置SerializationPair 即redis 缓存时value的序列化类型,默认使用的时JdkSerializationRedisSerializer,要换成Jackson2JsonRedisSerializer能再缓存时使用Json数据缓存,不然在redis里面就是一堆看不懂的乱码。
那么怎么怎么创建RedisCacheConfiguration对象呢:
RedisCacheConfiguration里有如下静态方法:可以返回一个默认的RedisCacheConfiguration对象:
public static RedisCacheConfiguration defaultCacheConfig() {
return defaultCacheConfig(null);
}
拿到了RedisCacheConfiguration过后还差一个SerializationPair,再看SerializationPair的源码,它是一个 RedisSerializationContext
interface SerializationPair<T> {
/**
* Creates a {@link SerializationPair} adapter given {@link RedisSerializer}.
*
* @param serializer must not be {@literal null}.
* @return a {@link SerializationPair} adapter for {@link RedisSerializer}.
*/
static <T> SerializationPair<T> fromSerializer(RedisSerializer<T> serializer) {
Assert.notNull(serializer, "RedisSerializer must not be null!");
return new RedisSerializerToSerializationPairAdapter<>(serializer);
}
经过研究后发现如下方法获取SerializationPair:
SerializationPair<Employee> serializationPair = RedisSerializationContext.SerializationPair
.fromSerializer(jackson2JsonRedisSerializer);
那么拿到这个对象过后设置到RedisCacheConfiguration中就可以实现自定义序列化格式,然后再把RedisCacheConfiguration放入到RedisCacheManager中了。
@Bean
public RedisCacheConfiguration empRedisCacheConfiguration() {
RedisSerializer<Employee> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Employee>(
Employee.class);
SerializationPair<Employee> serializationPair = RedisSerializationContext.SerializationPair
.fromSerializer(jackson2JsonRedisSerializer);
return RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(serializationPair);
}
@Primary
@Bean
public RedisCacheManager empCacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheManagerBuilder builder = RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory)
.cacheDefaults(empRedisCacheConfiguration());
RedisCacheManager cm = builder.build();
return cm;
}
果然自己找找到的答案比别人总结的答案要深刻得多,以后工作学习中要多多思考原理,多看源码,思考背后设计模式,这样才能成为一个具有思考能力学习能力的码农。