@@ -67,10 +67,10 @@ public class RedisCache extends AbstractValueAdaptingCache {
67
67
* Create a new {@link RedisCache}.
68
68
*
69
69
* @param name {@link String name} for this {@link Cache}; must not be {@literal null}.
70
- * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations by executing appropriate
71
- * Redis commands; must not be {@literal null}.
72
- * @param cacheConfiguration {@link RedisCacheConfiguration} applied to this {@link RedisCache on creation; must not
73
- * be {@literal null}.
70
+ * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations by executing
71
+ * the necessary Redis commands; must not be {@literal null}.
72
+ * @param cacheConfiguration {@link RedisCacheConfiguration} applied to this {@link RedisCache} on creation;
73
+ * must not be {@literal null}.
74
74
* @throws IllegalArgumentException if either the given {@link RedisCacheWriter} or {@link RedisCacheConfiguration}
75
75
* are {@literal null} or the given {@link String} name for this {@link RedisCache} is {@literal null}.
76
76
*/
@@ -88,18 +88,33 @@ protected RedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfig
88
88
}
89
89
90
90
/**
91
- * Get {@link RedisCacheConfiguration} used.
91
+ * Get the {@link RedisCacheConfiguration} used to configure this {@link RedisCache} on initialization .
92
92
*
93
- * @return immutable {@link RedisCacheConfiguration}. Never {@literal null}.
93
+ * @return an immutable {@link RedisCacheConfiguration} used to configure this {@link RedisCache} on initialization;
94
+ * never {@literal null}.
94
95
*/
95
96
public RedisCacheConfiguration getCacheConfiguration () {
96
97
return this .cacheConfiguration ;
97
98
}
98
99
100
+ /**
101
+ * Gets the configured {@link RedisCacheWriter} used to modify Redis for cache operations.
102
+ *
103
+ * @return the configured {@link RedisCacheWriter} used to modify Redis for cache operations.
104
+ */
99
105
protected RedisCacheWriter getCacheWriter () {
100
106
return this .cacheWriter ;
101
107
}
102
108
109
+ /**
110
+ * Gets the configured {@link ConversionService} used to convert {@link Object cache keys} to a {@link String}
111
+ * when accessing entries in the cache.
112
+ *
113
+ * @return the configured {@link ConversionService} used to convert {@link Object cache keys} to a {@link String}
114
+ * when accessing entries in the cache.
115
+ * @see RedisCacheConfiguration#getConversionService()
116
+ * @see #getCacheConfiguration()
117
+ */
103
118
protected ConversionService getConversionService () {
104
119
return getCacheConfiguration ().getConversionService ();
105
120
}
@@ -111,12 +126,13 @@ public String getName() {
111
126
112
127
@ Override
113
128
public RedisCacheWriter getNativeCache () {
114
- return this . cacheWriter ;
129
+ return getCacheWriter () ;
115
130
}
116
131
117
132
/**
118
- * Return the {@link CacheStatistics} snapshot for this cache instance. Statistics are accumulated per cache instance
119
- * and not from the backing Redis data store.
133
+ * Return the {@link CacheStatistics} snapshot for this cache instance.
134
+ * <p>
135
+ * Statistics are accumulated per cache instance and not from the backing Redis data store.
120
136
*
121
137
* @return statistics object for this {@link RedisCache}.
122
138
* @since 2.4
@@ -134,15 +150,25 @@ public <T> T get(Object key, Callable<T> valueLoader) {
134
150
return result != null ? (T ) result .get () : getSynchronized (key , valueLoader );
135
151
}
136
152
137
- @ SuppressWarnings ("unchecked" )
138
153
@ Nullable
154
+ @ SuppressWarnings ("unchecked" )
139
155
private synchronized <T > T getSynchronized (Object key , Callable <T > valueLoader ) {
140
156
141
157
ValueWrapper result = get (key );
142
158
143
159
return result != null ? (T ) result .get () : loadCacheValue (key , valueLoader );
144
160
}
145
161
162
+ /**
163
+ * Loads the {@link Object} using the given {@link Callable valueLoader} and {@link #put(Object, Object) puts}
164
+ * the {@link Object loaded value} in the cache.
165
+ *
166
+ * @param <T> {@link Class type} of the loaded {@link Object cache value}.
167
+ * @param key {@link Object key} mapped to the loaded {@link Object cache value}.
168
+ * @param valueLoader {@link Callable} object used to load the {@link Object value}
169
+ * for the given {@link Object key}.
170
+ * @return the loaded {@link Object value}.
171
+ */
146
172
protected <T > T loadCacheValue (Object key , Callable <T > valueLoader ) {
147
173
148
174
T value ;
@@ -172,7 +198,7 @@ public void put(Object key, @Nullable Object value) {
172
198
173
199
Object cacheValue = preProcessCacheValue (value );
174
200
175
- if (! isAllowNullValues () && cacheValue == null ) {
201
+ if (nullCacheValueIsNotAllowed ( cacheValue ) ) {
176
202
177
203
String message = String .format ("Cache '%s' does not allow 'null' values; Avoid storing null"
178
204
+ " via '@Cacheable(unless=\" #result == null\" )' or configure RedisCache to allow 'null'"
@@ -182,20 +208,20 @@ public void put(Object key, @Nullable Object value) {
182
208
}
183
209
184
210
getCacheWriter ().put (getName (), createAndConvertCacheKey (key ), serializeCacheValue (cacheValue ),
185
- getCacheConfiguration ().getTtl ( ));
211
+ getCacheConfiguration ().getTtlFunction (). getTimeToLive ( key , value ));
186
212
}
187
213
188
214
@ Override
189
215
public ValueWrapper putIfAbsent (Object key , @ Nullable Object value ) {
190
216
191
217
Object cacheValue = preProcessCacheValue (value );
192
218
193
- if (! isAllowNullValues () && cacheValue == null ) {
219
+ if (nullCacheValueIsNotAllowed ( cacheValue ) ) {
194
220
return get (key );
195
221
}
196
222
197
223
byte [] result = getCacheWriter ().putIfAbsent (getName (), createAndConvertCacheKey (key ),
198
- serializeCacheValue (cacheValue ), getCacheConfiguration ().getTtl ( ));
224
+ serializeCacheValue (cacheValue ), getCacheConfiguration ().getTtlFunction (). getTimeToLive ( key , value ));
199
225
200
226
return result != null ? new SimpleValueWrapper (fromStoreValue (deserializeCacheValue (result ))) : null ;
201
227
}
@@ -206,7 +232,7 @@ public void clear() {
206
232
}
207
233
208
234
/**
209
- * Clear keys that match the provided {@link String keyPattern}.
235
+ * Clear keys that match the given {@link String keyPattern}.
210
236
* <p>
211
237
* Useful when cache keys are formatted in a style where Redis patterns can be used for matching these.
212
238
*
@@ -303,7 +329,7 @@ protected String createCacheKey(Object key) {
303
329
}
304
330
305
331
/**
306
- * Convert {@code key} to a {@link String} representation used for cache key creation.
332
+ * Convert {@code key} to a {@link String} used in cache key creation.
307
333
*
308
334
* @param key will never be {@literal null}.
309
335
* @return never {@literal null}.
@@ -338,10 +364,9 @@ protected String convertKey(Object key) {
338
364
return key .toString ();
339
365
}
340
366
341
- String message = String .format (
342
- "Cannot convert cache key %s to String; Please register a suitable Converter"
367
+ String message = String .format ("Cannot convert cache key %s to String; Please register a suitable Converter"
343
368
+ " via 'RedisCacheConfiguration.configureKeyConverters(...)' or override '%s.toString()'" ,
344
- source , key .getClass ().getName ());
369
+ source , key .getClass ().getName ());
345
370
346
371
throw new IllegalStateException (message );
347
372
}
@@ -403,4 +428,8 @@ private String prefixCacheKey(String key) {
403
428
// allow contextual cache names by computing the key prefix on every call.
404
429
return getCacheConfiguration ().getKeyPrefixFor (getName ()) + key ;
405
430
}
431
+
432
+ private boolean nullCacheValueIsNotAllowed (@ Nullable Object cacheValue ) {
433
+ return cacheValue == null && !isAllowNullValues ();
434
+ }
406
435
}
0 commit comments