简体   繁体   English

如何将Spring Cache Redis与自定义RestTemplate一起使用?

[英]How to use Spring Cache Redis with a custom RestTemplate?

I'm migrating my Spring application from Spring-boot 1.5.9 to Spring-boot 2.0.0. 我正在将我的Spring应用程序从Spring-boot 1.5.9迁移到Spring-boot 2.0.0。 With this new Spring bundle, I have some issues with caching data in Redis. 有了这个新的Spring包,我在Redis中缓存数据时遇到了一些问题。

In my Configuration, I have 3 CacheManager with differents TTL (long, medium and short) : 在我的配置中,我有3个不同的TTL(长,中,短)CacheManager:

@Bean(name = "longLifeCacheManager")
public CacheManager longLifeCacheManager() {
    RedisCacheConfiguration cacheConfiguration =
            RedisCacheConfiguration.defaultCacheConfig()
                    .entryTtl(Duration.ofSeconds(redisExpirationLong))
                    .disableCachingNullValues();
    return RedisCacheManager.builder(jedisConnectionFactory()).cacheDefaults(cacheConfiguration).build();
}

I also have a custom RestTemplate : 我还有一个自定义的RestTemplate:

@Bean
public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory connectionFactory) {
    RedisTemplate<?, ?> template = new RedisTemplate<>();
    template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
    template.setConnectionFactory(connectionFactory);
    return template;
}

With the previous Spring version, every data that is cached use this RestTemplate and was serialized with the GenericJackson2JsonRedisSerializer. 使用之前的Spring版本,每个缓存的数据都使用此RestTemplate并使用GenericJackson2JsonRedisSerializer进行序列化。

With the new Spring version, the CacheManager don't use the RestTemplate but use its own SerializationPair. 使用新的Spring版本,CacheManager不使用RestTemplate,而是使用自己的SerializationPair。 This result to everything beeing serialized with the default JdkSerializationRedisSerializer. 这个结果是使用默认的JdkSerializationRedisSerializer序列化的所有内容。

Is it possible to configure the CacheManager to use the RestTemplate and how ? 是否可以将CacheManager配置为使用RestTemplate以及如何使用? If it is not possible, what can I do to use the JacksonSerializer instead of the JdkSerializer ? 如果不可能,我该怎么做才能使用JacksonSerializer而不是JdkSerializer?

I finally found a working solution. 我终于找到了一个有效的解决方 I can't configure the CacheManager to use my RedisTemplate, but I can set the Serializer like this : 我无法配置CacheManager来使用我的RedisTemplate,但我可以像这样设置Serializer:

@Bean(name = "longLifeCacheManager")
public CacheManager longLifeCacheManager(JedisConnectionFactory jedisConnectionFactory) {
    RedisCacheConfiguration cacheConfiguration =
            RedisCacheConfiguration.defaultCacheConfig()
                    .entryTtl(Duration.ofSeconds(redisExpirationLong))
                    .disableCachingNullValues()
                    .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
    return RedisCacheManager.builder(jedisConnectionFactory).cacheDefaults(cacheConfiguration).build();
}

The serializeValuesWith method is the key. serializeValuesWith方法是关键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM