Skip to content

Commit 8142836

Browse files
christophstroblmp911de
authored andcommitted
DATAREDIS-767 - Add flag to disable in flight Cache creation.
We now provide a flag to disable the in flight cache creation for unconfigured caches. This gives eg. a CompositeCacheManager the possibility to chime in. Original pull request: spring-projects#311.
1 parent 3f7f98e commit 8142836

File tree

2 files changed

+99
-7
lines changed

2 files changed

+99
-7
lines changed

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

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager
4949
private final RedisCacheWriter cacheWriter;
5050
private final RedisCacheConfiguration defaultCacheConfig;
5151
private final Map<String, RedisCacheConfiguration> initialCacheConfiguration;
52+
private final boolean locked;
5253

5354
/**
5455
* Creates new {@link RedisCacheManager} using given {@link RedisCacheWriter} and default
@@ -57,15 +58,31 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager
5758
* @param cacheWriter must not be {@literal null}.
5859
* @param defaultCacheConfiguration must not be {@literal null}. Maybe just use
5960
* {@link RedisCacheConfiguration#defaultCacheConfig()}.
61+
* @param locked allow create missing caches.
62+
* @since 2.0.4
6063
*/
61-
public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) {
64+
private RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration,
65+
boolean locked) {
6266

6367
Assert.notNull(cacheWriter, "CacheWriter must not be null!");
6468
Assert.notNull(defaultCacheConfiguration, "DefaultCacheConfiguration must not be null!");
6569

6670
this.cacheWriter = cacheWriter;
6771
this.defaultCacheConfig = defaultCacheConfiguration;
6872
this.initialCacheConfiguration = new LinkedHashMap<>();
73+
this.locked = locked;
74+
}
75+
76+
/**
77+
* Creates new {@link RedisCacheManager} using given {@link RedisCacheWriter} and default
78+
* {@link RedisCacheConfiguration}.
79+
*
80+
* @param cacheWriter must not be {@literal null}.
81+
* @param defaultCacheConfiguration must not be {@literal null}. Maybe just use
82+
* {@link RedisCacheConfiguration#defaultCacheConfig()}.
83+
*/
84+
public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) {
85+
this(cacheWriter, defaultCacheConfiguration, false);
6986
}
7087

7188
/**
@@ -81,7 +98,26 @@ public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration d
8198
public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration,
8299
String... initialCacheNames) {
83100

84-
this(cacheWriter, defaultCacheConfiguration);
101+
this(cacheWriter, defaultCacheConfiguration, false, initialCacheNames);
102+
}
103+
104+
/**
105+
* Creates new {@link RedisCacheManager} using given {@link RedisCacheWriter} and default
106+
* {@link RedisCacheConfiguration}.
107+
*
108+
* @param cacheWriter must not be {@literal null}.
109+
* @param defaultCacheConfiguration must not be {@literal null}. Maybe just use
110+
* {@link RedisCacheConfiguration#defaultCacheConfig()}.
111+
* @param locked if set to {@literal true} no new caches can be acquire at runtime but limited to the given list of
112+
* initial cache names.
113+
* @param initialCacheNames optional set of known cache names that will be created with given
114+
* {@literal defaultCacheConfiguration}.
115+
* @since 2.0.4
116+
*/
117+
public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration,
118+
boolean locked, String... initialCacheNames) {
119+
120+
this(cacheWriter, defaultCacheConfiguration, locked);
85121

86122
for (String cacheName : initialCacheNames) {
87123
this.initialCacheConfiguration.put(cacheName, defaultCacheConfiguration);
@@ -101,7 +137,26 @@ public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration d
101137
public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration,
102138
Map<String, RedisCacheConfiguration> initialCacheConfigurations) {
103139

104-
this(cacheWriter, defaultCacheConfiguration);
140+
this(cacheWriter, defaultCacheConfiguration, initialCacheConfigurations, false);
141+
}
142+
143+
/**
144+
* Creates new {@link RedisCacheManager} using given {@link RedisCacheWriter} and default
145+
* {@link RedisCacheConfiguration}.
146+
*
147+
* @param cacheWriter must not be {@literal null}.
148+
* @param defaultCacheConfiguration must not be {@literal null}. Maybe just use
149+
* {@link RedisCacheConfiguration#defaultCacheConfig()}.
150+
* @param initialCacheConfigurations Map of known cache names along with the configuration to use for those caches.
151+
* Must not be {@literal null}.
152+
* @param locked if set to {@literal true} no new caches can be acquire at runtime but limited to the initial cache
153+
* configurations.
154+
* @since 2.0.4
155+
*/
156+
public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration,
157+
Map<String, RedisCacheConfiguration> initialCacheConfigurations, boolean locked) {
158+
159+
this(cacheWriter, defaultCacheConfiguration, locked);
105160

106161
Assert.notNull(initialCacheConfigurations, "InitialCacheConfigurations must not be null!");
107162

@@ -180,7 +235,7 @@ protected Collection<RedisCache> loadCaches() {
180235
*/
181236
@Override
182237
protected RedisCache getMissingCache(String name) {
183-
return createRedisCache(name, defaultCacheConfig);
238+
return locked ? null : createRedisCache(name, defaultCacheConfig);
184239
}
185240

186241
/**
@@ -222,8 +277,9 @@ public static class RedisCacheManagerBuilder {
222277

223278
private final RedisCacheWriter cacheWriter;
224279
private RedisCacheConfiguration defaultCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
225-
private final Map<String, RedisCacheConfiguration> intialCaches = new LinkedHashMap<>();
280+
private final Map<String, RedisCacheConfiguration> initialCaches = new LinkedHashMap<>();
226281
private boolean enableTransactions;
282+
boolean allowInFlightCacheCreation = true;
227283

228284
private RedisCacheManagerBuilder(RedisCacheWriter cacheWriter) {
229285
this.cacheWriter = cacheWriter;
@@ -313,8 +369,24 @@ public RedisCacheManagerBuilder withInitialCacheConfigurations(
313369
cacheConfigurations.forEach((cacheName, configuration) -> Assert.notNull(configuration,
314370
String.format("RedisCacheConfiguration for cache %s must not be null!", cacheName)));
315371

316-
this.intialCaches.putAll(cacheConfigurations);
372+
this.initialCaches.putAll(cacheConfigurations);
373+
374+
return this;
375+
}
376+
377+
/**
378+
* Disable the in flight {@link org.springframework.cache.Cache} creation for a missing cache.
379+
* <p />
380+
* {@link RedisCacheManager#getMissingCache(String)} returns {@literal null} for any missing
381+
* {@link org.springframework.cache.Cache} instead of a new {@link RedisCache} instance. This allows eg.
382+
* {@link org.springframework.cache.support.CompositeCacheManager} to chime.
383+
*
384+
* @return this {@link RedisCacheManagerBuilder}.
385+
* @since 2.0.4
386+
*/
387+
public RedisCacheManagerBuilder disableCreateOnMissingCache() {
317388

389+
this.allowInFlightCacheCreation = false;
318390
return this;
319391
}
320392

@@ -325,7 +397,8 @@ public RedisCacheManagerBuilder withInitialCacheConfigurations(
325397
*/
326398
public RedisCacheManager build() {
327399

328-
RedisCacheManager cm = new RedisCacheManager(cacheWriter, defaultCacheConfiguration, intialCaches);
400+
RedisCacheManager cm = new RedisCacheManager(cacheWriter, defaultCacheConfiguration, initialCaches,
401+
!allowInFlightCacheCreation);
329402

330403
cm.setTransactionAware(enableTransactions);
331404

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,23 @@ public void transactionAwareCacheManagerShouldDecoracteCache() {
9191
assertThat(cache).isInstanceOfAny(TransactionAwareCacheDecorator.class);
9292
assertThat(ReflectionTestUtils.getField(cache, "targetCache")).isInstanceOf(RedisCache.class);
9393
}
94+
95+
@Test // DATAREDIS-767
96+
public void lockingCacheShouldPreventInFlightCacheCreation() {
97+
98+
RedisCacheManager cacheManager = RedisCacheManager.builder(cacheWriter).disableCreateOnMissingCache().build();
99+
cacheManager.afterPropertiesSet();
100+
101+
assertThat(cacheManager.getCache("not-configured")).isNull();
102+
}
103+
104+
@Test // DATAREDIS-767
105+
public void lockingCacheShouldStillReturnPreconfiguredCaches() {
106+
107+
RedisCacheManager cacheManager = RedisCacheManager.builder(cacheWriter)
108+
.initialCacheNames(Collections.singleton("configured")).disableCreateOnMissingCache().build();
109+
cacheManager.afterPropertiesSet();
110+
111+
assertThat(cacheManager.getCache("configured")).isNotNull();
112+
}
94113
}

0 commit comments

Comments
 (0)