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.
This commit is contained in:
Christoph Strobl
2019-02-26 14:05:34 +01:00
committed by Mark Paluch
parent 32cc2a60e7
commit 630ff46b45
2 changed files with 80 additions and 4 deletions

View File

@@ -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<String, RedisCacheConfiguration> 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<String> 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<RedisCacheConfiguration> getCacheConfigurationFor(String cacheName) {
return Optional.ofNullable(this.initialCaches.get(cacheName));
}
/**
* Create new instance of {@link RedisCacheManager} with configuration options applied.
*

View File

@@ -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();
}
}