在springboot2.0中使用@cacheable时如何为每个Redis缓存配置不同的ttl

时间:2018-06-27 04:56:21

标签: spring-boot caching redis

我在带有Redis的springboot2.0中使用@cacheable。我已将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;
}

但这会使所有密钥的ttl变为30秒,如何为每个具有不同缓存名称的redis缓存配置不同的ttl?

3 个答案:

答案 0 :(得分:2)

通过为每个缓存创建不同的配置,然后将它们放置在用于创建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);
}

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

以下是使用Redisson Java客户端使用不同的TTLmaxIdleTime定义多个基于Redis的缓存的方法:

    @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);
    }