From 630ff46b454bb73fbea90e87c5c67fec09df3b10 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 26 Feb 2019 14:05:34 +0100 Subject: [PATCH] DATAREDIS-935 - Allow configuration of a single initial cache in RedisCacheManagerBuilder. Also, add introspection methods to expose the configured cache names/configurations. Original pull request: #387. --- .../data/redis/cache/RedisCacheManager.java | 44 +++++++++++++++++-- .../cache/RedisCacheManagerUnitTests.java | 40 +++++++++++++++++ 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java b/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java index f04cbc24f..b41d00099 100644 --- a/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java +++ b/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java @@ -22,6 +22,7 @@ import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager; @@ -352,10 +353,8 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager Assert.notNull(cacheNames, "CacheNames must not be null!"); - Map cacheConfigMap = new LinkedHashMap<>(cacheNames.size()); - cacheNames.forEach(it -> cacheConfigMap.put(it, defaultCacheConfiguration)); - - return withInitialCacheConfigurations(cacheConfigMap); + cacheNames.forEach(it -> withCacheConfiguration(it, defaultCacheConfiguration)); + return this; } /** @@ -372,7 +371,22 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager String.format("RedisCacheConfiguration for cache %s must not be null!", cacheName))); this.initialCaches.putAll(cacheConfigurations); + return this; + } + /** + * @param cacheName + * @param cacheConfiguration + * @return this {@link RedisCacheManagerBuilder}. + * @since 2.2 + */ + public RedisCacheManagerBuilder withCacheConfiguration(String cacheName, + RedisCacheConfiguration cacheConfiguration) { + + Assert.notNull(cacheName, "CacheName must not be null!"); + Assert.notNull(cacheConfiguration, "CacheConfiguration must not be null!"); + + this.initialCaches.put(cacheName, cacheConfiguration); return this; } @@ -392,6 +406,28 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager return this; } + /** + * Get the {@link Set} of cache names for which the builder holds {@link RedisCacheConfiguration configuration}. + * + * @return an unmodifiable {@link Set} holding the name of caches for which a {@link RedisCacheConfiguration + * configuration} has been set. + * @since 2.2 + */ + public Set getConfiguredCaches() { + return Collections.unmodifiableSet(this.initialCaches.keySet()); + } + + /** + * Get the {@link RedisCacheConfiguration} for a given cache by its name. + * + * @param cacheName must not be {@literal null}. + * @return {@link Optional#empty()} if no {@link RedisCacheConfiguration} set for the given cache name. + * @since 2.2 + */ + public Optional getCacheConfigurationFor(String cacheName) { + return Optional.ofNullable(this.initialCaches.get(cacheName)); + } + /** * Create new instance of {@link RedisCacheManager} with configuration options applied. * diff --git a/src/test/java/org/springframework/data/redis/cache/RedisCacheManagerUnitTests.java b/src/test/java/org/springframework/data/redis/cache/RedisCacheManagerUnitTests.java index af1cd87dd..e0560cfda 100644 --- a/src/test/java/org/springframework/data/redis/cache/RedisCacheManagerUnitTests.java +++ b/src/test/java/org/springframework/data/redis/cache/RedisCacheManagerUnitTests.java @@ -25,6 +25,7 @@ import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.springframework.cache.Cache; import org.springframework.cache.transaction.TransactionAwareCacheDecorator; +import org.springframework.data.redis.cache.RedisCacheManager.RedisCacheManagerBuilder; import org.springframework.test.util.ReflectionTestUtils; /** @@ -110,4 +111,43 @@ public class RedisCacheManagerUnitTests { assertThat(cacheManager.getCache("configured")).isNotNull(); } + + @Test // DATAREDIS-935 + public void cacheManagerBuilderReturnsConfiguredCaches() { + + RedisCacheManagerBuilder cmb = RedisCacheManager.builder(cacheWriter) + .initialCacheNames(Collections.singleton("configured")).disableCreateOnMissingCache(); + + assertThat(cmb.getConfiguredCaches()).containsExactly("configured"); + assertThatExceptionOfType(UnsupportedOperationException.class) + .isThrownBy(() -> cmb.getConfiguredCaches().add("another")); + } + + @Test // DATAREDIS-935 + public void cacheManagerBuilderDoesNotAllowSneakingInConfiguration() { + + RedisCacheManagerBuilder cmb = RedisCacheManager.builder(cacheWriter) + .initialCacheNames(Collections.singleton("configured")).disableCreateOnMissingCache(); + + assertThatExceptionOfType(UnsupportedOperationException.class) + .isThrownBy(() -> cmb.getConfiguredCaches().add("another")); + } + + @Test // DATAREDIS-935 + public void cacheManagerBuilderReturnsConfigurationForKnownCache() { + + RedisCacheManagerBuilder cmb = RedisCacheManager.builder(cacheWriter) + .initialCacheNames(Collections.singleton("configured")).disableCreateOnMissingCache(); + + assertThat(cmb.getCacheConfigurationFor("configured")).isPresent(); + } + + @Test // DATAREDIS-935 + public void cacheManagerBuilderReturnsEmptyOptionalForUnknownCache() { + + RedisCacheManagerBuilder cmb = RedisCacheManager.builder(cacheWriter) + .initialCacheNames(Collections.singleton("configured")).disableCreateOnMissingCache(); + + assertThat(cmb.getCacheConfigurationFor("unknown")).isNotPresent(); + } }