【问题标题】:How to having two caches(redis backend) with different expire time in spring boot如何在春季启动中拥有两个具有不同过期时间的缓存(redis后端)
【发布时间】:2015-10-17 14:00:28
【问题描述】:

在我的 spring boot(1.2.6) 应用程序中,我需要针对不同对象使用不同的过期策略。缓存后端是redis。

归档它的最佳做法是什么?

【问题讨论】:

    标签: spring spring-boot spring-cache spring-data-redis


    【解决方案1】:

    我搞定了,现在可以了。

    最初我创建了具有不同过期时间的不同缓存,但是它不起作用。看起来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;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-24
      • 2019-01-28
      • 1970-01-01
      • 2019-10-28
      • 2018-01-16
      • 2017-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多