diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/RedisCacheConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/RedisCacheConfiguration.java index 545d51335664..83eb53c4105e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/RedisCacheConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/RedisCacheConfiguration.java @@ -57,6 +57,7 @@ class RedisCacheConfiguration { public RedisCacheManager cacheManager(CacheProperties cacheProperties, CacheManagerCustomizers cacheManagerCustomizers, ObjectProvider redisCacheConfiguration, + ObjectProvider redisCacheManagerBuilderCustomizers, RedisConnectionFactory redisConnectionFactory, ResourceLoader resourceLoader) { RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory).cacheDefaults( determineConfiguration(cacheProperties, redisCacheConfiguration, resourceLoader.getClassLoader())); @@ -64,6 +65,7 @@ public RedisCacheManager cacheManager(CacheProperties cacheProperties, if (!cacheNames.isEmpty()) { builder.initialCacheNames(new LinkedHashSet<>(cacheNames)); } + redisCacheManagerBuilderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder)); return cacheManagerCustomizers.customize(builder.build()); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/RedisCacheManagerBuilderCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/RedisCacheManagerBuilderCustomizer.java new file mode 100644 index 000000000000..79560c4d6ee8 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/RedisCacheManagerBuilderCustomizer.java @@ -0,0 +1,36 @@ +/* + * Copyright 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.cache; + +import org.springframework.data.redis.cache.RedisCacheManager.RedisCacheManagerBuilder; + +/** + * Callback interface that can be used to customize a {@link RedisCacheManagerBuilder}. + * + * @author Dmytro Nosan + * @since 2.2.0 + */ +@FunctionalInterface +public interface RedisCacheManagerBuilderCustomizer { + + /** + * Customize the {@link RedisCacheManagerBuilder}. + * @param builder the builder to customize + */ + void customize(RedisCacheManagerBuilder builder); + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java index b676ca4f30a2..7c4ae09ef9ee 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java @@ -274,6 +274,17 @@ public void redisCacheWithRedisCacheConfiguration() { }); } + @Test + public void redisCacheWithRedisCacheManagerBuilderCustomizer() { + this.contextRunner.withUserConfiguration(RedisWithRedisCacheManagerBuilderCustomizerConfiguration.class) + .withPropertyValues("spring.cache.type=redis", "spring.cache.redis.time-to-live=15000") + .run((context) -> { + RedisCacheManager cacheManager = getCacheManager(context, RedisCacheManager.class); + RedisCacheConfiguration redisCacheConfiguration = getDefaultRedisCacheConfiguration(cacheManager); + assertThat(redisCacheConfiguration.getTtl()).isEqualTo(java.time.Duration.ofSeconds(10)); + }); + } + @Test public void redisCacheWithCustomizers() { this.contextRunner.withUserConfiguration(RedisWithCustomizersConfiguration.class) @@ -755,6 +766,18 @@ public org.springframework.data.redis.cache.RedisCacheConfiguration customRedisC } + @Configuration(proxyBeanMethods = false) + @Import(RedisConfiguration.class) + static class RedisWithRedisCacheManagerBuilderCustomizerConfiguration { + + @Bean + public RedisCacheManagerBuilderCustomizer ttlRedisCacheManagerBuilderCustomizer() { + return (builder) -> builder.cacheDefaults( + RedisCacheConfiguration.defaultCacheConfig().entryTtl(java.time.Duration.ofSeconds(10))); + } + + } + @Configuration(proxyBeanMethods = false) @Import({ RedisConfiguration.class, CacheManagerCustomizersConfiguration.class }) static class RedisWithCustomizersConfiguration {