配置spring cache RedisCacheManager的序列化方法

通过查看autoconfigure源码

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

部分源码如下:

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 就直接使用了。

那么只需要自己注入一个 RedisCacheConfiguration 即可。

代码可以直接用源码里面的:),调整序列化部分即可。

 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 ;

参考文档:

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

可以看到已有的可用序列化器:

GenericJackson2JsonRedisSerializerGenericToStringSerializerJackson2JsonRedisSerializerJdkSerializationRedisSerializerOxmSerializerStringRedisSerializer

这里顺便学习一下 @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; } }

 

 

转载于:https://www.cnblogs.com/huhangfei/p/10136705.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值