Skip to content

Commit 630ff46

Browse files
christophstroblmp911de
authored andcommitted
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: spring-projects#387.
1 parent 32cc2a6 commit 630ff46

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.LinkedList;
2323
import java.util.List;
2424
import java.util.Map;
25+
import java.util.Optional;
2526
import java.util.Set;
2627

2728
import org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager;
@@ -352,10 +353,8 @@ public RedisCacheManagerBuilder initialCacheNames(Set<String> cacheNames) {
352353

353354
Assert.notNull(cacheNames, "CacheNames must not be null!");
354355

355-
Map<String, RedisCacheConfiguration> cacheConfigMap = new LinkedHashMap<>(cacheNames.size());
356-
cacheNames.forEach(it -> cacheConfigMap.put(it, defaultCacheConfiguration));
357-
358-
return withInitialCacheConfigurations(cacheConfigMap);
356+
cacheNames.forEach(it -> withCacheConfiguration(it, defaultCacheConfiguration));
357+
return this;
359358
}
360359

361360
/**
@@ -372,7 +371,22 @@ public RedisCacheManagerBuilder withInitialCacheConfigurations(
372371
String.format("RedisCacheConfiguration for cache %s must not be null!", cacheName)));
373372

374373
this.initialCaches.putAll(cacheConfigurations);
374+
return this;
375+
}
376+
377+
/**
378+
* @param cacheName
379+
* @param cacheConfiguration
380+
* @return this {@link RedisCacheManagerBuilder}.
381+
* @since 2.2
382+
*/
383+
public RedisCacheManagerBuilder withCacheConfiguration(String cacheName,
384+
RedisCacheConfiguration cacheConfiguration) {
375385

386+
Assert.notNull(cacheName, "CacheName must not be null!");
387+
Assert.notNull(cacheConfiguration, "CacheConfiguration must not be null!");
388+
389+
this.initialCaches.put(cacheName, cacheConfiguration);
376390
return this;
377391
}
378392

@@ -392,6 +406,28 @@ public RedisCacheManagerBuilder disableCreateOnMissingCache() {
392406
return this;
393407
}
394408

409+
/**
410+
* Get the {@link Set} of cache names for which the builder holds {@link RedisCacheConfiguration configuration}.
411+
*
412+
* @return an unmodifiable {@link Set} holding the name of caches for which a {@link RedisCacheConfiguration
413+
* configuration} has been set.
414+
* @since 2.2
415+
*/
416+
public Set<String> getConfiguredCaches() {
417+
return Collections.unmodifiableSet(this.initialCaches.keySet());
418+
}
419+
420+
/**
421+
* Get the {@link RedisCacheConfiguration} for a given cache by its name.
422+
*
423+
* @param cacheName must not be {@literal null}.
424+
* @return {@link Optional#empty()} if no {@link RedisCacheConfiguration} set for the given cache name.
425+
* @since 2.2
426+
*/
427+
public Optional<RedisCacheConfiguration> getCacheConfigurationFor(String cacheName) {
428+
return Optional.ofNullable(this.initialCaches.get(cacheName));
429+
}
430+
395431
/**
396432
* Create new instance of {@link RedisCacheManager} with configuration options applied.
397433
*

src/test/java/org/springframework/data/redis/cache/RedisCacheManagerUnitTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.mockito.junit.MockitoJUnitRunner;
2626
import org.springframework.cache.Cache;
2727
import org.springframework.cache.transaction.TransactionAwareCacheDecorator;
28+
import org.springframework.data.redis.cache.RedisCacheManager.RedisCacheManagerBuilder;
2829
import org.springframework.test.util.ReflectionTestUtils;
2930

3031
/**
@@ -110,4 +111,43 @@ public void lockedCacheManagerShouldStillReturnPreconfiguredCaches() {
110111

111112
assertThat(cacheManager.getCache("configured")).isNotNull();
112113
}
114+
115+
@Test // DATAREDIS-935
116+
public void cacheManagerBuilderReturnsConfiguredCaches() {
117+
118+
RedisCacheManagerBuilder cmb = RedisCacheManager.builder(cacheWriter)
119+
.initialCacheNames(Collections.singleton("configured")).disableCreateOnMissingCache();
120+
121+
assertThat(cmb.getConfiguredCaches()).containsExactly("configured");
122+
assertThatExceptionOfType(UnsupportedOperationException.class)
123+
.isThrownBy(() -> cmb.getConfiguredCaches().add("another"));
124+
}
125+
126+
@Test // DATAREDIS-935
127+
public void cacheManagerBuilderDoesNotAllowSneakingInConfiguration() {
128+
129+
RedisCacheManagerBuilder cmb = RedisCacheManager.builder(cacheWriter)
130+
.initialCacheNames(Collections.singleton("configured")).disableCreateOnMissingCache();
131+
132+
assertThatExceptionOfType(UnsupportedOperationException.class)
133+
.isThrownBy(() -> cmb.getConfiguredCaches().add("another"));
134+
}
135+
136+
@Test // DATAREDIS-935
137+
public void cacheManagerBuilderReturnsConfigurationForKnownCache() {
138+
139+
RedisCacheManagerBuilder cmb = RedisCacheManager.builder(cacheWriter)
140+
.initialCacheNames(Collections.singleton("configured")).disableCreateOnMissingCache();
141+
142+
assertThat(cmb.getCacheConfigurationFor("configured")).isPresent();
143+
}
144+
145+
@Test // DATAREDIS-935
146+
public void cacheManagerBuilderReturnsEmptyOptionalForUnknownCache() {
147+
148+
RedisCacheManagerBuilder cmb = RedisCacheManager.builder(cacheWriter)
149+
.initialCacheNames(Collections.singleton("configured")).disableCreateOnMissingCache();
150+
151+
assertThat(cmb.getCacheConfigurationFor("unknown")).isNotPresent();
152+
}
113153
}

0 commit comments

Comments
 (0)