@@ -49,6 +49,7 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager
49
49
private final RedisCacheWriter cacheWriter ;
50
50
private final RedisCacheConfiguration defaultCacheConfig ;
51
51
private final Map <String , RedisCacheConfiguration > initialCacheConfiguration ;
52
+ private final boolean locked ;
52
53
53
54
/**
54
55
* Creates new {@link RedisCacheManager} using given {@link RedisCacheWriter} and default
@@ -57,15 +58,31 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager
57
58
* @param cacheWriter must not be {@literal null}.
58
59
* @param defaultCacheConfiguration must not be {@literal null}. Maybe just use
59
60
* {@link RedisCacheConfiguration#defaultCacheConfig()}.
61
+ * @param locked allow create missing caches.
62
+ * @since 2.0.4
60
63
*/
61
- public RedisCacheManager (RedisCacheWriter cacheWriter , RedisCacheConfiguration defaultCacheConfiguration ) {
64
+ private RedisCacheManager (RedisCacheWriter cacheWriter , RedisCacheConfiguration defaultCacheConfiguration ,
65
+ boolean locked ) {
62
66
63
67
Assert .notNull (cacheWriter , "CacheWriter must not be null!" );
64
68
Assert .notNull (defaultCacheConfiguration , "DefaultCacheConfiguration must not be null!" );
65
69
66
70
this .cacheWriter = cacheWriter ;
67
71
this .defaultCacheConfig = defaultCacheConfiguration ;
68
72
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 );
69
86
}
70
87
71
88
/**
@@ -81,7 +98,26 @@ public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration d
81
98
public RedisCacheManager (RedisCacheWriter cacheWriter , RedisCacheConfiguration defaultCacheConfiguration ,
82
99
String ... initialCacheNames ) {
83
100
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 );
85
121
86
122
for (String cacheName : initialCacheNames ) {
87
123
this .initialCacheConfiguration .put (cacheName , defaultCacheConfiguration );
@@ -101,7 +137,26 @@ public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration d
101
137
public RedisCacheManager (RedisCacheWriter cacheWriter , RedisCacheConfiguration defaultCacheConfiguration ,
102
138
Map <String , RedisCacheConfiguration > initialCacheConfigurations ) {
103
139
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 );
105
160
106
161
Assert .notNull (initialCacheConfigurations , "InitialCacheConfigurations must not be null!" );
107
162
@@ -180,7 +235,7 @@ protected Collection<RedisCache> loadCaches() {
180
235
*/
181
236
@ Override
182
237
protected RedisCache getMissingCache (String name ) {
183
- return createRedisCache (name , defaultCacheConfig );
238
+ return locked ? null : createRedisCache (name , defaultCacheConfig );
184
239
}
185
240
186
241
/**
@@ -222,8 +277,9 @@ public static class RedisCacheManagerBuilder {
222
277
223
278
private final RedisCacheWriter cacheWriter ;
224
279
private RedisCacheConfiguration defaultCacheConfiguration = RedisCacheConfiguration .defaultCacheConfig ();
225
- private final Map <String , RedisCacheConfiguration > intialCaches = new LinkedHashMap <>();
280
+ private final Map <String , RedisCacheConfiguration > initialCaches = new LinkedHashMap <>();
226
281
private boolean enableTransactions ;
282
+ boolean allowInFlightCacheCreation = true ;
227
283
228
284
private RedisCacheManagerBuilder (RedisCacheWriter cacheWriter ) {
229
285
this .cacheWriter = cacheWriter ;
@@ -313,8 +369,24 @@ public RedisCacheManagerBuilder withInitialCacheConfigurations(
313
369
cacheConfigurations .forEach ((cacheName , configuration ) -> Assert .notNull (configuration ,
314
370
String .format ("RedisCacheConfiguration for cache %s must not be null!" , cacheName )));
315
371
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 () {
317
388
389
+ this .allowInFlightCacheCreation = false ;
318
390
return this ;
319
391
}
320
392
@@ -325,7 +397,8 @@ public RedisCacheManagerBuilder withInitialCacheConfigurations(
325
397
*/
326
398
public RedisCacheManager build () {
327
399
328
- RedisCacheManager cm = new RedisCacheManager (cacheWriter , defaultCacheConfiguration , intialCaches );
400
+ RedisCacheManager cm = new RedisCacheManager (cacheWriter , defaultCacheConfiguration , initialCaches ,
401
+ !allowInFlightCacheCreation );
329
402
330
403
cm .setTransactionAware (enableTransactions );
331
404
0 commit comments