@@ -62,6 +62,9 @@ public class RedisCache extends AbstractValueAdaptingCache {
6262
6363 static final byte [] BINARY_NULL_VALUE = RedisSerializer .java ().serialize (NullValue .INSTANCE );
6464
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+ 6568 private final Lock lock = new ReentrantLock ();
6669
6770 private final RedisCacheConfiguration cacheConfiguration ;
@@ -71,16 +74,16 @@ public class RedisCache extends AbstractValueAdaptingCache {
7174 private final String name ;
7275
7376 /**
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.
7679 *
7780 * @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}.
8285 * @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}.
8487 */
8588 protected RedisCache (String name , RedisCacheWriter cacheWriter , RedisCacheConfiguration cacheConfiguration ) {
8689
@@ -98,28 +101,27 @@ protected RedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfig
98101 /**
99102 * Get the {@link RedisCacheConfiguration} used to configure this {@link RedisCache} on initialization.
100103 *
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.
103105 */
104106 public RedisCacheConfiguration getCacheConfiguration () {
105107 return this .cacheConfiguration ;
106108 }
107109
108110 /**
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.
110112 *
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.
112114 */
113115 protected RedisCacheWriter getCacheWriter () {
114116 return this .cacheWriter ;
115117 }
116118
117119 /**
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.
120122 *
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.
123125 * @see RedisCacheConfiguration#getConversionService()
124126 * @see #getCacheConfiguration()
125127 */
@@ -142,7 +144,7 @@ public RedisCacheWriter getNativeCache() {
142144 * <p>
143145 * Statistics are accumulated per cache instance and not from the backing Redis data store.
144146 *
145- * @return statistics object for this {@link RedisCache}.
147+ * @return {@link CacheStatistics} object for this {@link RedisCache}.
146148 * @since 2.4
147149 */
148150 public CacheStatistics getStatistics () {
@@ -173,8 +175,8 @@ private <T> T getSynchronized(Object key, Callable<T> valueLoader) {
173175 }
174176
175177 /**
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.
178180 *
179181 * @param <T> {@link Class type} of the loaded {@link Object cache value}.
180182 * @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) {
199201 @ Override
200202 protected Object lookup (Object key ) {
201203
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 );
205209
206- return value != null ? deserializeCacheValue (value ) : null ;
210+ return binaryValue != null ? deserializeCacheValue (binaryValue ) : null ;
207211 }
208212
209213 private Duration getTimeToLive (Object key ) {
@@ -219,8 +223,12 @@ public void put(Object key, @Nullable Object value) {
219223
220224 Object cacheValue = processAndCheckValue (value );
221225
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 );
224232 }
225233
226234 @ Override
@@ -232,8 +240,11 @@ public ValueWrapper putIfAbsent(Object key, @Nullable Object value) {
232240 return get (key );
233241 }
234242
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 );
237248
238249 return result != null ? new SimpleValueWrapper (fromStoreValue (deserializeCacheValue (result ))) : null ;
239250 }
@@ -273,8 +284,7 @@ public void evict(Object key) {
273284 public CompletableFuture <?> retrieve (Object key ) {
274285
275286 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 );
278288 }
279289
280290 return retrieveValue (key ).thenApply (this ::nullSafeDeserializedStoreValue );
@@ -285,27 +295,28 @@ public CompletableFuture<?> retrieve(Object key) {
285295 public <T > CompletableFuture <T > retrieve (Object key , Supplier <CompletableFuture <T >> valueLoader ) {
286296
287297 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 );
290299 }
291300
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 -> {
294308
295- if (bytes != null ) {
296- return CompletableFuture .completedFuture ((T ) nullSafeDeserializedStoreValue (bytes ));
297- }
309+ Object cacheValue = processAndCheckValue (value );
298310
299- return valueLoader .get ().thenCompose (value -> {
311+ byte [] binaryKey = createAndConvertCacheKey (key );
312+ byte [] binaryValue = serializeCacheValue (cacheValue );
300313
301- Object cacheValue = processAndCheckValue ( value );
314+ Duration timeToLive = getTimeToLive ( key , cacheValue );
302315
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+ });
309320 }
310321
311322 private Object processAndCheckValue (@ Nullable Object value ) {
@@ -403,8 +414,8 @@ protected String createCacheKey(Object key) {
403414 */
404415 protected String convertKey (Object key ) {
405416
406- if (key instanceof String ) {
407- return ( String ) key ;
417+ if (key instanceof String stringKey ) {
418+ return stringKey ;
408419 }
409420
410421 TypeDescriptor source = TypeDescriptor .forObject (key );
@@ -429,9 +440,8 @@ protected String convertKey(Object key) {
429440 return key .toString ();
430441 }
431442
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()'" ,
435445 source , key .getClass ().getName ());
436446
437447 throw new IllegalStateException (message );
0 commit comments