【问题标题】:how to configure different ttl for each redis cache when using @cacheable in springboot2.0springboot2.0中使用@cacheable时如何为每个redis缓存配置不同的ttl
【发布时间】:2018-12-05 21:02:35
【问题描述】:

我在 springboot2.0 中使用 @cacheable 和 redis。我已将 RedisCacheManager 配置如下:

@Bean
public RedisCacheManager redisCacheManager(RedisConnectionFactory connectionFactory) {

    RedisCacheWriter redisCacheWriter = RedisCacheWriter.lockingRedisCacheWriter(connectionFactory);
    SerializationPair<Object> valueSerializationPair = RedisSerializationContext.SerializationPair
            .fromSerializer(new GenericJackson2JsonRedisSerializer());
    RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
    cacheConfiguration = cacheConfiguration.serializeValuesWith(valueSerializationPair);
    cacheConfiguration = cacheConfiguration.prefixKeysWith("myPrefix");
    cacheConfiguration = cacheConfiguration.entryTtl(Duration.ofSeconds(30));

    RedisCacheManager redisCacheManager = new RedisCacheManager(redisCacheWriter, cacheConfiguration);
    return redisCacheManager;
}

但是这使得所有key的ttl为30秒,如何为每个redis缓存配置不同的ttl并使用不同的cachename?

【问题讨论】:

  • 我想我已经通过使用不同的 bean 名称配置不同的 RedisCacheManager 来解决这个问题。现在一切看起来都很好
  • @c-y-in-sof 你可以回答你自己的问题

标签: spring-boot caching redis


【解决方案1】:

您可以只使用一个 CacheManager 为每个缓存配置不同的过期时间,方法是为每个缓存创建不同的配置,并将它们放在创建 CacheManager 的映射中。

例如:

@Bean
RedisCacheWriter redisCacheWriter() {
    return RedisCacheWriter.lockingRedisCacheWriter(jedisConnectionFactory());
}

@Bean
RedisCacheConfiguration defaultRedisCacheConfiguration() {
    return RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(defaultCacheExpiration));
}

@Bean
CacheManager cacheManager() {
    Map<String, RedisCacheConfiguration> cacheNamesConfigurationMap = new HashMap<>();
    cacheNamesConfigurationMap.put("cacheName1", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(ttl1)));
    cacheNamesConfigurationMap.put("cacheName2", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(ttl2)));
    cacheNamesConfigurationMap.put("cacheName3", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(ttl3)));

    return new RedisCacheManager(redisCacheWriter(), defaultRedisCacheConfiguration(), cacheNamesConfigurationMap);
}

【讨论】:

    【解决方案2】:

    如果在使用@cacheable时需要为缓存配置不同的过期时间, 你可以用不同的ttl配置不同的CacheManager,并在你的服务中使用缓存时指定cacheManager。

     @Cacheable(cacheManager = "expireOneHour", value = "onehour", key = "'_onehour_'+#key", sync = true)
    

    【讨论】:

      【解决方案3】:

      以下是如何使用Redisson Java 客户端定义多个基于 Redis 的缓存以及不同的 TTLmaxIdleTime

          @Bean(destroyMethod="shutdown")
          RedissonClient redisson() throws IOException {
              Config config = new Config();
              config.useClusterServers()
                    .addNodeAddress("redis://127.0.0.1:7004", "redis://127.0.0.1:7001");
              return Redisson.create(config);
          }
      
          @Bean
          CacheManager cacheManager(RedissonClient redissonClient) {
              Map<String, CacheConfig> config = new HashMap<String, CacheConfig>();
      
              // create "myCache1" cache with ttl = 20 minutes and maxIdleTime = 12 minutes
              config.put("myCache", new CacheConfig(24*60*1000, 12*60*1000));
      
              // create "myCache2" cache with ttl = 35 minutes and maxIdleTime = 24 minutes
              config.put("myCache2", new CacheConfig(35*60*1000, 24*60*1000));
              return new RedissonSpringCacheManager(redissonClient, config);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-27
        • 2020-02-06
        • 2019-10-18
        • 2020-01-09
        • 2014-10-17
        • 2023-01-19
        • 2020-10-01
        • 2020-06-17
        相关资源
        最近更新 更多