Julie
Julie

Reputation: 189

Disable redis cache using Spring, redission client - no Spring boot

We need to enable/disable caching based on whether there is a redis host configured or not. I added the below 3 bean configs. Is this the right way to disable? I am still seeing cache resolver error after startup - "No CacheResolver specified, and no bean of type CacheManager found. Register a CacheManager bean or remove the @EnableCaching annotation from your configuration"

    @Bean(destroyMethod="shutdown")
    @ConditionalOnProperty(prefix = "spring", name = "redis.host")
    public RedissonClient redisson() {
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://127.0.0.1:6379");
        return Redisson.create(config);
    }

    @ConditionalOnBean(RedissonClient.class)
    @Bean
    public RedissonSpringCacheManager cacheManager(RedissonClient redissonClient) {
        Map<String, CacheConfig> config = new HashMap<>();
        // create "testMap" spring cache with ttl = 24 minutes and maxIdleTime = 12 minutes
        config.put("testMap", new CacheConfig(60*60*1000, 12*60*1000));
        return new RedissonSpringCacheManager(redissonClient, config);
    }

    @Bean
    @ConditionalOnBean(RedissonSpringCacheManager.class)
    @Primary
    public CompositeCacheManager compositeCacheManager(RedissonSpringCacheManager cacheManager) {
        logger.info("composite cache-manager init...");
        CompositeCacheManager compositeCacheManager = new CompositeCacheManager(cacheManager);
        compositeCacheManager.setFallbackToNoOpCache(true);
        return compositeCacheManager;
    }

Thanks for your help!

Upvotes: 0

Views: 890

Answers (1)

Shivendra Kumar
Shivendra Kumar

Reputation: 11

You have enabled caching using @EnableCaching annotation which will always look fro relevant Cache Manager Beans. A workaound is removing @EnableCaching and implementing your own CachingConfigurer and mark this bean as conditional or profile based.

Upvotes: 0

Related Questions