如何在 spring 引导中拥有两个具有不同过期时间的缓存(redis 后端)

How to having two caches(redis backend) with different expire time in spring boot

在我的 spring boot(1.2.6) 应用程序中,我需要针对不同的对象使用不同的过期策略。缓存后端是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;
}