【发布时间】:2015-10-17 14:00:28
【问题描述】:
在我的 spring boot(1.2.6) 应用程序中,我需要针对不同对象使用不同的过期策略。缓存后端是redis。
归档它的最佳做法是什么?
【问题讨论】:
标签: spring spring-boot spring-cache spring-data-redis
在我的 spring boot(1.2.6) 应用程序中,我需要针对不同对象使用不同的过期策略。缓存后端是redis。
归档它的最佳做法是什么?
【问题讨论】:
标签: spring spring-boot spring-cache spring-data-redis
我搞定了,现在可以了。
最初我创建了具有不同过期时间的不同缓存,但是它不起作用。看起来spring redis缓存没有使用缓存实例中指定的过期时间。
不工作
@Bean
public Cache cacheObjectName(StringRedisTemplate template) {
return new RedisCache(CACHE_OBJNAME, CACHE_OBJNAME.getBytes(), template, 10 * 24 * 60 * 60);
}
最后我不得不创建具有不同过期时间的不同缓存管理器,
工作实施
@Bean(name = MANAGER_NAME_1D)
public CacheManager cacheManager1D(StringRedisTemplate redisTemplate) throws Exception {
final RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate(factory), Arrays.asList(CACHE_A, CACHE_B));
redisCacheManager.setUsePrefix(true);
redisCacheManager.setDefaultExpiration(60 * 60 * 24);
return redisCacheManager;
}
【讨论】: