Spring Boot: Overriding CacheManager bean makes cache related properties not work

Sasha Shpota :

I have a Spring Boot 2 application with Redis cache. It worked just fine until I overridden CacheManager bean.

Problem: The following configuration property gets ignored (I can't turn off caching anymore):

spring.cache.type=none

Although according to the documentation it should work.

Question: How to make the spring.cache.type=none work?

There is a workaround like this, but it is far from being a good solution.

More details: Here is how my configuration looks like:

@Configuration
public class CacheConfiguration {
    @Bean
    RedisCacheWriter redisCacheWriter(RedisConnectionFactory connectionFactory) {
        return RedisCacheWriter.lockingRedisCacheWriter(connectionFactory);
    }

    @Bean
    CacheManager cacheManager(RedisCacheWriter redisCacheWriter) {
        Map<String, RedisCacheConfiguration> ttlConfiguration = ...
        RedisCacheConfiguration defaultTtlConfiguration = ...
        return new RedisCacheManager(
                redisCacheWriter, defaultTtlConfiguration, ttlConfiguration
        );
    }
}
Simon Martinelli :

Because you are creating the CacheManager yourself you also have to check spring.cache.type if you want to turn it of.

@Bean
@ConditionalOnExpression("${spring.cache.type} != 'none'")
CacheManager cacheManager(RedisCacheWriter redisCacheWriter) {

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=122994&siteId=1