@@ -62,6 +62,9 @@ public class RedisCache extends AbstractValueAdaptingCache {
62
62
63
63
static final byte [] BINARY_NULL_VALUE = RedisSerializer .java ().serialize (NullValue .INSTANCE );
64
64
65
+ static final String CACHE_RETRIEVAL_UNSUPPORTED_OPERATION_EXCEPTION_MESSAGE =
66
+ "The Redis driver configured with RedisCache through RedisCacheWriter does not support CompletableFuture-based retrieval" ;
67
+
65
68
private final Lock lock = new ReentrantLock ();
66
69
67
70
private final RedisCacheConfiguration cacheConfiguration ;
@@ -71,16 +74,16 @@ public class RedisCache extends AbstractValueAdaptingCache {
71
74
private final String name ;
72
75
73
76
/**
74
- * Create a new {@link RedisCache} with the given {@link String name} and {@link RedisCacheConfiguration}, using the
75
- * {@link RedisCacheWriter} to execute Redis commands supporting the cache operations.
77
+ * Create a new {@link RedisCache} with the given {@link String name} and {@link RedisCacheConfiguration},
78
+ * using the {@link RedisCacheWriter} to execute Redis commands supporting the cache operations.
76
79
*
77
80
* @param name {@link String name} for this {@link Cache}; must not be {@literal null}.
78
- * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations by executing the
79
- * necessary Redis commands; must not be {@literal null}.
80
- * @param cacheConfiguration {@link RedisCacheConfiguration} applied to this {@link RedisCache} on creation; must not
81
- * be {@literal null}.
81
+ * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations
82
+ * by executing the necessary Redis commands; must not be {@literal null}.
83
+ * @param cacheConfiguration {@link RedisCacheConfiguration} applied to this {@link RedisCache} on creation;
84
+ * must not be {@literal null}.
82
85
* @throws IllegalArgumentException if either the given {@link RedisCacheWriter} or {@link RedisCacheConfiguration}
83
- * are {@literal null} or the given {@link String} name for this {@link RedisCache} is {@literal null}.
86
+ * are {@literal null} or the given {@link String} name for this {@link RedisCache} is {@literal null}.
84
87
*/
85
88
protected RedisCache (String name , RedisCacheWriter cacheWriter , RedisCacheConfiguration cacheConfiguration ) {
86
89
@@ -98,28 +101,27 @@ protected RedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfig
98
101
/**
99
102
* Get the {@link RedisCacheConfiguration} used to configure this {@link RedisCache} on initialization.
100
103
*
101
- * @return an immutable {@link RedisCacheConfiguration} used to configure this {@link RedisCache} on initialization;
102
- * never {@literal null}.
104
+ * @return an immutable {@link RedisCacheConfiguration} used to configure this {@link RedisCache} on initialization.
103
105
*/
104
106
public RedisCacheConfiguration getCacheConfiguration () {
105
107
return this .cacheConfiguration ;
106
108
}
107
109
108
110
/**
109
- * Gets the configured {@link RedisCacheWriter} used to modify Redis for cache operations.
111
+ * Gets the configured {@link RedisCacheWriter} used to adapt Redis for cache operations.
110
112
*
111
- * @return the configured {@link RedisCacheWriter} used to modify Redis for cache operations.
113
+ * @return the configured {@link RedisCacheWriter} used to adapt Redis for cache operations.
112
114
*/
113
115
protected RedisCacheWriter getCacheWriter () {
114
116
return this .cacheWriter ;
115
117
}
116
118
117
119
/**
118
- * Gets the configured {@link ConversionService} used to convert {@link Object cache keys} to a {@link String} when
119
- * accessing entries in the cache.
120
+ * Gets the configured {@link ConversionService} used to convert {@link Object cache keys} to a {@link String}
121
+ * when accessing entries in the cache.
120
122
*
121
- * @return the configured {@link ConversionService} used to convert {@link Object cache keys} to a {@link String} when
122
- * accessing entries in the cache.
123
+ * @return the configured {@link ConversionService} used to convert {@link Object cache keys} to a {@link String}
124
+ * when accessing entries in the cache.
123
125
* @see RedisCacheConfiguration#getConversionService()
124
126
* @see #getCacheConfiguration()
125
127
*/
@@ -142,7 +144,7 @@ public RedisCacheWriter getNativeCache() {
142
144
* <p>
143
145
* Statistics are accumulated per cache instance and not from the backing Redis data store.
144
146
*
145
- * @return statistics object for this {@link RedisCache}.
147
+ * @return {@link CacheStatistics} object for this {@link RedisCache}.
146
148
* @since 2.4
147
149
*/
148
150
public CacheStatistics getStatistics () {
@@ -173,8 +175,8 @@ private <T> T getSynchronized(Object key, Callable<T> valueLoader) {
173
175
}
174
176
175
177
/**
176
- * Loads the {@link Object} using the given {@link Callable valueLoader} and {@link #put(Object, Object) puts} the
177
- * {@link Object loaded value} in the cache.
178
+ * Loads the {@link Object} using the given {@link Callable valueLoader} and {@link #put(Object, Object) puts}
179
+ * the {@link Object loaded value} in the cache.
178
180
*
179
181
* @param <T> {@link Class type} of the loaded {@link Object cache value}.
180
182
* @param key {@link Object key} mapped to the loaded {@link Object cache value}.
@@ -199,11 +201,13 @@ protected <T> T loadCacheValue(Object key, Callable<T> valueLoader) {
199
201
@ Override
200
202
protected Object lookup (Object key ) {
201
203
202
- byte [] value = getCacheConfiguration ().isTimeToIdleEnabled ()
203
- ? getCacheWriter ().get (getName (), createAndConvertCacheKey (key ), getTimeToLive (key ))
204
- : getCacheWriter ().get (getName (), createAndConvertCacheKey (key ));
204
+ byte [] binaryKey = createAndConvertCacheKey (key );
205
+
206
+ byte [] binaryValue = getCacheConfiguration ().isTimeToIdleEnabled ()
207
+ ? getCacheWriter ().get (getName (), binaryKey , getTimeToLive (key ))
208
+ : getCacheWriter ().get (getName (), binaryKey );
205
209
206
- return value != null ? deserializeCacheValue (value ) : null ;
210
+ return binaryValue != null ? deserializeCacheValue (binaryValue ) : null ;
207
211
}
208
212
209
213
private Duration getTimeToLive (Object key ) {
@@ -219,8 +223,12 @@ public void put(Object key, @Nullable Object value) {
219
223
220
224
Object cacheValue = processAndCheckValue (value );
221
225
222
- getCacheWriter ().put (getName (), createAndConvertCacheKey (key ), serializeCacheValue (cacheValue ),
223
- getTimeToLive (key , value ));
226
+ byte [] binaryKey = createAndConvertCacheKey (key );
227
+ byte [] binaryValue = serializeCacheValue (cacheValue );
228
+
229
+ Duration timeToLive = getTimeToLive (key , value );
230
+
231
+ getCacheWriter ().put (getName (), binaryKey , binaryValue , timeToLive );
224
232
}
225
233
226
234
@ Override
@@ -232,8 +240,11 @@ public ValueWrapper putIfAbsent(Object key, @Nullable Object value) {
232
240
return get (key );
233
241
}
234
242
235
- byte [] result = getCacheWriter ().putIfAbsent (getName (), createAndConvertCacheKey (key ),
236
- serializeCacheValue (cacheValue ), getTimeToLive (key , value ));
243
+ Duration timeToLive = getTimeToLive (key , value );
244
+
245
+ byte [] binaryKey = createAndConvertCacheKey (key );
246
+ byte [] binaryValue = serializeCacheValue (cacheValue );
247
+ byte [] result = getCacheWriter ().putIfAbsent (getName (), binaryKey , binaryValue , timeToLive );
237
248
238
249
return result != null ? new SimpleValueWrapper (fromStoreValue (deserializeCacheValue (result ))) : null ;
239
250
}
@@ -273,8 +284,7 @@ public void evict(Object key) {
273
284
public CompletableFuture <?> retrieve (Object key ) {
274
285
275
286
if (!getCacheWriter ().supportsAsyncRetrieve ()) {
276
- throw new UnsupportedOperationException (
277
- "The Redis driver configured with RedisCache through RedisCacheWriter does not support CompletableFuture-based retrieval" );
287
+ throw new UnsupportedOperationException (CACHE_RETRIEVAL_UNSUPPORTED_OPERATION_EXCEPTION_MESSAGE );
278
288
}
279
289
280
290
return retrieveValue (key ).thenApply (this ::nullSafeDeserializedStoreValue );
@@ -285,27 +295,28 @@ public CompletableFuture<?> retrieve(Object key) {
285
295
public <T > CompletableFuture <T > retrieve (Object key , Supplier <CompletableFuture <T >> valueLoader ) {
286
296
287
297
if (!getCacheWriter ().supportsAsyncRetrieve ()) {
288
- throw new UnsupportedOperationException (
289
- "The Redis driver configured with RedisCache through RedisCacheWriter does not support CompletableFuture-based retrieval" );
298
+ throw new UnsupportedOperationException (CACHE_RETRIEVAL_UNSUPPORTED_OPERATION_EXCEPTION_MESSAGE );
290
299
}
291
300
292
- return retrieveValue (key ) //
293
- .thenCompose (bytes -> {
301
+ return retrieveValue (key ).thenCompose (bytes -> {
302
+
303
+ if (bytes != null ) {
304
+ return CompletableFuture .completedFuture ((T ) nullSafeDeserializedStoreValue (bytes ));
305
+ }
306
+
307
+ return valueLoader .get ().thenCompose (value -> {
294
308
295
- if (bytes != null ) {
296
- return CompletableFuture .completedFuture ((T ) nullSafeDeserializedStoreValue (bytes ));
297
- }
309
+ Object cacheValue = processAndCheckValue (value );
298
310
299
- return valueLoader .get ().thenCompose (value -> {
311
+ byte [] binaryKey = createAndConvertCacheKey (key );
312
+ byte [] binaryValue = serializeCacheValue (cacheValue );
300
313
301
- Object cacheValue = processAndCheckValue ( value );
314
+ Duration timeToLive = getTimeToLive ( key , cacheValue );
302
315
303
- return getCacheWriter ()
304
- .store (getName (), createAndConvertCacheKey (key ), serializeCacheValue (cacheValue ),
305
- getTimeToLive (key , cacheValue )) //
306
- .thenApply (v -> value );
307
- });
308
- });
316
+ return getCacheWriter ().store (getName (), binaryKey , binaryValue , timeToLive )
317
+ .thenApply (v -> value );
318
+ });
319
+ });
309
320
}
310
321
311
322
private Object processAndCheckValue (@ Nullable Object value ) {
@@ -403,8 +414,8 @@ protected String createCacheKey(Object key) {
403
414
*/
404
415
protected String convertKey (Object key ) {
405
416
406
- if (key instanceof String ) {
407
- return ( String ) key ;
417
+ if (key instanceof String stringKey ) {
418
+ return stringKey ;
408
419
}
409
420
410
421
TypeDescriptor source = TypeDescriptor .forObject (key );
@@ -429,9 +440,8 @@ protected String convertKey(Object key) {
429
440
return key .toString ();
430
441
}
431
442
432
- String message = String .format (
433
- "Cannot convert cache key %s to String; Please register a suitable Converter"
434
- + " via 'RedisCacheConfiguration.configureKeyConverters(...)' or override '%s.toString()'" ,
443
+ String message = String .format ("Cannot convert cache key %s to String; Please register a suitable Converter"
444
+ + " via 'RedisCacheConfiguration.configureKeyConverters(...)' or override '%s.toString()'" ,
435
445
source , key .getClass ().getName ());
436
446
437
447
throw new IllegalStateException (message );
0 commit comments