Improve RedisCacheManagerBuilder to expose applied RedisCacheConfiguration.
Now it is possible to construct a new, custom default RedisCacheConfiguration from an existing, default RedisCacheConfiguration. This is useful in a Spring Boot context using the RedisCacheManagerBuilderCustomizer and acquiring access to the default RedisCacheConfiguration, which likely originated from Spring Boot Redis CacheProperties. Resolves #2583
This commit is contained in:
@@ -41,6 +41,7 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
* @author Yanming Zhou
|
||||
* @since 2.0
|
||||
* @see RedisCacheConfiguration
|
||||
* @see RedisCacheWriter
|
||||
@@ -336,6 +337,15 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns applied {@link RedisCacheConfiguration}, allow customization base on it.
|
||||
*
|
||||
* @return applied {@link RedisCacheConfiguration}.
|
||||
*/
|
||||
public RedisCacheConfiguration cacheDefaults() {
|
||||
return this.defaultCacheConfiguration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure a {@link RedisCacheWriter}.
|
||||
*
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.data.redis.cache;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -34,6 +35,7 @@ import org.springframework.test.util.ReflectionTestUtils;
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
* @author Yanming Zhou
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class RedisCacheManagerUnitTests {
|
||||
@@ -188,4 +190,16 @@ class RedisCacheManagerUnitTests {
|
||||
|
||||
verify(cacheWriter, never()).withStatisticsCollector(any());
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-481
|
||||
void customizeRedisCacheConfigurationBaseOnApplied() {
|
||||
|
||||
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig().disableKeyPrefix();
|
||||
RedisCacheManagerBuilder cmb = RedisCacheManager.builder().cacheDefaults(configuration);
|
||||
|
||||
cmb.cacheDefaults(cmb.cacheDefaults().entryTtl(Duration.ofSeconds(10)));
|
||||
|
||||
assertThat(cmb.cacheDefaults().usePrefix()).isFalse();
|
||||
assertThat(cmb.cacheDefaults().getTtl()).isEqualTo(Duration.ofSeconds(10));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user