Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ 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()));
List<String> cacheNames = cacheProperties.getCacheNames();
if (!cacheNames.isEmpty()) {
builder.initialCacheNames(new LinkedHashSet<>(cacheNames));
}
redisCacheManagerBuilderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
return cacheManagerCustomizers.customize(builder.build());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down