gpt4 book ai didi

spring - 如何将默认到期的 RedisCacheManager 迁移到 Spring Data Redis 2.0?

转载 作者:IT王子 更新时间:2023-10-29 05:56:50 30 4
gpt4 key购买 nike

我正在将一个使用 Spring Data Redis 的项目升级到 2.x。以前,可以使用 RedisCacheManager.setDefaultExpiration(defaultExpireTime) 为缓存管理器配置默认过期时间。 .这个选项在 2.x 中似乎不存在。 2.x 中的等价物是什么?是RedisCacheConfiguration.entryTtl(ttl) ,还是别的?

我可能遗漏了一些东西,但我没有找到 Spring Data Redis 2.x 的迁移指南。是否存在这样的迁移指南?

简而言之,我想将以下代码迁移到 Redis 2.x:

public CacheManager cacheManager() {
RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate());
redisCacheManager.setDefaultExpiration(DEFAULT_EXPIRATION_SECONDS);
return redisCacheManager;
}

public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory());
return template;
}

下面是等价的吗?如果不是,Redis 2.x 中的等效代码是什么?

public CacheManager cacheManager() {
return RedisCacheManager.builder(redisConnectionFactory())
.cacheDefaults(RedisCacheConfiguration.defaultCacheConfig().entryTtl(DEFAULT_EXPIRATION_DURATION))
.build();
}

最佳答案

最初,我正在运行以下源代码和依赖项...

    @Bean(value ="redisTemplate")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}

@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate){
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
cacheManager.setDefaultExpiration(resourceConfigValue.getCacheExpireSeconds());
return cacheManager;
}

org.springframework.boot:spring-boot-starter-web:1.5.10.RELEASE

org.springframework.boot:spring-boot-starter-data-redis:1.5.10.RELEASE

我已经确认这个想法按预期工作......

    @Bean(value ="redisTemplate")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}

@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
Duration expiration = Duration.ofSeconds(resourceConfigValue.getCacheExpireSeconds());
return RedisCacheManager.builder(redisConnectionFactory)
.cacheDefaults(RedisCacheConfiguration.defaultCacheConfig().entryTtl(expiration)).build();
}

org.springframework.boot:spring-boot-starter-web:2.0.2.RELEASE

org.springframework.boot:spring-boot-starter-data-redis:2.0.2.RELEASE

关于spring - 如何将默认到期的 RedisCacheManager 迁移到 Spring Data Redis 2.0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49655094/

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