how to configure different ttl for each redis cache when using @cacheable in springboot2.0

后端 未结 3 463

I am using @cacheable in springboot2.0 with redis. I have configured RedisCacheManager as follow:

@Bean
public RedisCacheManager redisCacheManager(RedisConne         


        
3条回答
  •  甜味超标
    2021-01-06 06:12

    Here is how you can define multiple Redis based caches with different TTL and maxIdleTime using Redisson Java client:

        @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 config = new HashMap();
    
            // 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);
        }
    

提交回复
热议问题