配置spring cache RedisCacheManager的序列化方法

经过查看autoconfigure源码html

org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration;

部分源码以下:redis

private org.springframework.data.redis.cache.RedisCacheConfiguration determineConfiguration(
ClassLoader classLoader) {
if (this.redisCacheConfiguration != null) {
return this.redisCacheConfiguration;
}
Redis redisProperties = this.cacheProperties.getRedis();
org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration
.defaultCacheConfig();
config = config.serializeValuesWith(SerializationPair
.fromSerializer(new JdkSerializationRedisSerializer(classLoader)));
if (redisProperties.getTimeToLive() != null) {
config = config.entryTtl(redisProperties.getTimeToLive());
}
if (redisProperties.getKeyPrefix() != null) {
config = config.prefixKeysWith(redisProperties.getKeyPrefix());
}
if (!redisProperties.isCacheNullValues()) {
config = config.disableCachingNullValues();
}
if (!redisProperties.isUseKeyPrefix()) {
config = config.disableKeyPrefix();
}
return config;
}

能够看到默认是使用的 JdkSerializationRedisSerializer ,还有就是若是容器里已经有 redisCacheConfiguration 就直接使用了。spring

那么只须要本身注入一个 RedisCacheConfiguration 便可。api

代码能够直接用源码里面的:),调整序列化部分便可。缓存

 1 @Configuration  2 @Conditional(SimpleCacheCondition.class)  3 public class MyRedisCacheConfiguration {  4     private final CacheProperties cacheProperties;  5  MyRedisCacheConfiguration(CacheProperties cacheProperties) {  6         this.cacheProperties = cacheProperties;  7  }  8  @Bean  9     public org.springframework.data.redis.cache.RedisCacheConfiguration determineConfiguration() { 10         CacheProperties.Redis redisProperties = this.cacheProperties.getRedis(); 11         org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration 12  .defaultCacheConfig(); 13         config = config.serializeValuesWith(RedisSerializationContext.SerializationPair 14  .fromSerializer(valueSerializer())); 15         if (redisProperties.getTimeToLive() != null) { 16             config = config.entryTtl(redisProperties.getTimeToLive()); 17  } 18         if (redisProperties.getKeyPrefix() != null) { 19             config = config.prefixKeysWith(redisProperties.getKeyPrefix()); 20  } 21         if (!redisProperties.isCacheNullValues()) { 22             config = config.disableCachingNullValues(); 23  } 24         if (!redisProperties.isUseKeyPrefix()) { 25             config = config.disableKeyPrefix(); 26  } 27         return config; 28  } 29     /**
30  * 使用Jackson序列化器 31  * @return
32      */
33     private RedisSerializer<Object> valueSerializer() { 34         ObjectMapper objectMapper = new ObjectMapper(); 35  objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 36  objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 37         return new GenericJackson2JsonRedisSerializer(objectMapper); 38  } 39 
40 }

这里使用的是 GenericJackson2JsonRedisSerializer ;app

参考文档:ide

https://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/serializer/RedisSerializer.html学习

能够看到已有的可用序列化器:this

GenericJackson2JsonRedisSerializerGenericToStringSerializerJackson2JsonRedisSerializerJdkSerializationRedisSerializerOxmSerializerStringRedisSerializerurl

这里顺便学习一下 @Conditional ,这个注解能够帮咱们控制何时来注册组件。

好比这里写了一个 RedisCacheConfiguration 配置,那我只想在启用了redis缓存时再注册,就能够使用这个注解,如 @Conditional(SimpleCacheCondition.class) 。

定义 SimpleCacheCondition ,部分代码参考 org.springframework.boot.autoconfigure.cache.CacheCondition 

public class SimpleCacheCondition implements Condition { /** * Determine if the condition matches. * * @param context the condition context * @param metadata metadata of the {@link AnnotationMetadata class} * or {@link MethodMetadata method} being checked * @return {@code true} if the condition matches and the component can be registered, * or {@code false} to veto the annotated component's registration */ @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { try { String sourceClass = ""; if (metadata instanceof ClassMetadata) { sourceClass = ((ClassMetadata) metadata).getClassName(); } Environment environment = context.getEnvironment(); BindResult<CacheType> specified = Binder.get(environment) .bind("spring.cache.type", CacheType.class); if (!specified.isBound()) { return false; }
       //redis cache 启用 而且 是自定义的redisCacheConfiguration
if (specified.get().equals(CacheType.REDIS) && sourceClass.equals(MyRedisCacheConfiguration.class.getTypeName())) { return true; } }catch (Exception ex) {} return false; } }
相关文章
相关标签/搜索