gpt4 book ai didi

java - 如何在 spring-data 2.0.x 中创建 RedisCacheManager

转载 作者:IT王子 更新时间:2023-10-29 05:58:43 26 4
gpt4 key购买 nike

我正在将我的应用程序从 spring boot 1.5.x 迁移到 2.0.x。我想保留 jedis,但我对 RedisCacheManager 的实例化有疑问。

现在构造函数签名是

RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration)

但在此之前:

RedisCacheManager(RedisOperations redisOperations)

我定义这个 bean 在范围内只有 RedisTemplate:

@Bean
public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate) {
HandleRedisCacheManager redisCacheManager = new HandleRedisCacheManager(redisTemplate);
redisCacheManager.setUsePrefix(true);
return redisCacheManager;
}

现在应该如何创建?

最佳答案

尝试下面的代码,它适用于 spring-boot 2.1.0.RELEASE

@Bean
public RedisCacheManager redisCacheManager(LettuceConnectionFactory lettuceConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.disableCachingNullValues()
.entryTtl(Duration.ofHours(1))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()));
redisCacheConfiguration.usePrefix();

return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(lettuceConnectionFactory)
.cacheDefaults(redisCacheConfiguration).build();

}

关于java - 如何在 spring-data 2.0.x 中创建 RedisCacheManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51418161/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com