Commit 2f49d6d1 authored by Andy Wilkinson's avatar Andy Wilkinson

Merge pull request #17287 from nosan

* gh-17287:
  Introduce RedisCacheManagerBuilderCustomizer

Closes gh-17287
parents 71b09e39 1d9aae82
......@@ -56,6 +56,7 @@ class RedisCacheConfiguration {
public RedisCacheManager cacheManager(CacheProperties cacheProperties,
CacheManagerCustomizers cacheManagerCustomizers,
ObjectProvider<org.springframework.data.redis.cache.RedisCacheConfiguration> redisCacheConfiguration,
ObjectProvider<RedisCacheManagerBuilderCustomizer> redisCacheManagerBuilderCustomizers,
RedisConnectionFactory redisConnectionFactory, ResourceLoader resourceLoader) {
RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(
determineConfiguration(cacheProperties, redisCacheConfiguration, resourceLoader.getClassLoader()));
......@@ -63,6 +64,7 @@ class RedisCacheConfiguration {
if (!cacheNames.isEmpty()) {
builder.initialCacheNames(new LinkedHashSet<>(cacheNames));
}
redisCacheManagerBuilderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
return cacheManagerCustomizers.customize(builder.build());
}
......
/*
* 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);
}
......@@ -274,6 +274,17 @@ public class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationT
});
}
@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 class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationT
}
@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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment