diff --git a/src/main/java/org/springframework/data/redis/cache/RedisCache.java b/src/main/java/org/springframework/data/redis/cache/RedisCache.java index 22e1cb9912..2df721c0a4 100644 --- a/src/main/java/org/springframework/data/redis/cache/RedisCache.java +++ b/src/main/java/org/springframework/data/redis/cache/RedisCache.java @@ -62,6 +62,9 @@ public class RedisCache extends AbstractValueAdaptingCache { static final byte[] BINARY_NULL_VALUE = RedisSerializer.java().serialize(NullValue.INSTANCE); + static final String CACHE_RETRIEVAL_UNSUPPORTED_OPERATION_EXCEPTION_MESSAGE = + "The Redis driver configured with RedisCache through RedisCacheWriter does not support CompletableFuture-based retrieval"; + private final Lock lock = new ReentrantLock(); private final RedisCacheConfiguration cacheConfiguration; @@ -71,16 +74,16 @@ public class RedisCache extends AbstractValueAdaptingCache { private final String name; /** - * Create a new {@link RedisCache} with the given {@link String name} and {@link RedisCacheConfiguration}, using the - * {@link RedisCacheWriter} to execute Redis commands supporting the cache operations. + * Create a new {@link RedisCache} with the given {@link String name} and {@link RedisCacheConfiguration}, + * using the {@link RedisCacheWriter} to execute Redis commands supporting the cache operations. * * @param name {@link String name} for this {@link Cache}; must not be {@literal null}. - * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations by executing the - * necessary Redis commands; must not be {@literal null}. - * @param cacheConfiguration {@link RedisCacheConfiguration} applied to this {@link RedisCache} on creation; must not - * be {@literal null}. + * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations + * by executing the necessary Redis commands; must not be {@literal null}. + * @param cacheConfiguration {@link RedisCacheConfiguration} applied to this {@link RedisCache} on creation; + * must not be {@literal null}. * @throws IllegalArgumentException if either the given {@link RedisCacheWriter} or {@link RedisCacheConfiguration} - * are {@literal null} or the given {@link String} name for this {@link RedisCache} is {@literal null}. + * are {@literal null} or the given {@link String} name for this {@link RedisCache} is {@literal null}. */ protected RedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfiguration) { @@ -98,28 +101,27 @@ protected RedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfig /** * Get the {@link RedisCacheConfiguration} used to configure this {@link RedisCache} on initialization. * - * @return an immutable {@link RedisCacheConfiguration} used to configure this {@link RedisCache} on initialization; - * never {@literal null}. + * @return an immutable {@link RedisCacheConfiguration} used to configure this {@link RedisCache} on initialization. */ public RedisCacheConfiguration getCacheConfiguration() { return this.cacheConfiguration; } /** - * Gets the configured {@link RedisCacheWriter} used to modify Redis for cache operations. + * Gets the configured {@link RedisCacheWriter} used to adapt Redis for cache operations. * - * @return the configured {@link RedisCacheWriter} used to modify Redis for cache operations. + * @return the configured {@link RedisCacheWriter} used to adapt Redis for cache operations. */ protected RedisCacheWriter getCacheWriter() { return this.cacheWriter; } /** - * Gets the configured {@link ConversionService} used to convert {@link Object cache keys} to a {@link String} when - * accessing entries in the cache. + * Gets the configured {@link ConversionService} used to convert {@link Object cache keys} to a {@link String} + * when accessing entries in the cache. * - * @return the configured {@link ConversionService} used to convert {@link Object cache keys} to a {@link String} when - * accessing entries in the cache. + * @return the configured {@link ConversionService} used to convert {@link Object cache keys} to a {@link String} + * when accessing entries in the cache. * @see RedisCacheConfiguration#getConversionService() * @see #getCacheConfiguration() */ @@ -142,7 +144,7 @@ public RedisCacheWriter getNativeCache() { *

* Statistics are accumulated per cache instance and not from the backing Redis data store. * - * @return statistics object for this {@link RedisCache}. + * @return {@link CacheStatistics} object for this {@link RedisCache}. * @since 2.4 */ public CacheStatistics getStatistics() { @@ -173,8 +175,8 @@ private T getSynchronized(Object key, Callable valueLoader) { } /** - * Loads the {@link Object} using the given {@link Callable valueLoader} and {@link #put(Object, Object) puts} the - * {@link Object loaded value} in the cache. + * Loads the {@link Object} using the given {@link Callable valueLoader} and {@link #put(Object, Object) puts} + * the {@link Object loaded value} in the cache. * * @param {@link Class type} of the loaded {@link Object cache value}. * @param key {@link Object key} mapped to the loaded {@link Object cache value}. @@ -199,11 +201,13 @@ protected T loadCacheValue(Object key, Callable valueLoader) { @Override protected Object lookup(Object key) { - byte[] value = getCacheConfiguration().isTimeToIdleEnabled() - ? getCacheWriter().get(getName(), createAndConvertCacheKey(key), getTimeToLive(key)) - : getCacheWriter().get(getName(), createAndConvertCacheKey(key)); + byte[] binaryKey = createAndConvertCacheKey(key); + + byte[] binaryValue = getCacheConfiguration().isTimeToIdleEnabled() + ? getCacheWriter().get(getName(), binaryKey, getTimeToLive(key)) + : getCacheWriter().get(getName(), binaryKey); - return value != null ? deserializeCacheValue(value) : null; + return binaryValue != null ? deserializeCacheValue(binaryValue) : null; } private Duration getTimeToLive(Object key) { @@ -219,8 +223,12 @@ public void put(Object key, @Nullable Object value) { Object cacheValue = processAndCheckValue(value); - getCacheWriter().put(getName(), createAndConvertCacheKey(key), serializeCacheValue(cacheValue), - getTimeToLive(key, value)); + byte[] binaryKey = createAndConvertCacheKey(key); + byte[] binaryValue = serializeCacheValue(cacheValue); + + Duration timeToLive = getTimeToLive(key, value); + + getCacheWriter().put(getName(), binaryKey, binaryValue, timeToLive); } @Override @@ -232,8 +240,11 @@ public ValueWrapper putIfAbsent(Object key, @Nullable Object value) { return get(key); } - byte[] result = getCacheWriter().putIfAbsent(getName(), createAndConvertCacheKey(key), - serializeCacheValue(cacheValue), getTimeToLive(key, value)); + Duration timeToLive = getTimeToLive(key, value); + + byte[] binaryKey = createAndConvertCacheKey(key); + byte[] binaryValue = serializeCacheValue(cacheValue); + byte[] result = getCacheWriter().putIfAbsent(getName(), binaryKey, binaryValue, timeToLive); return result != null ? new SimpleValueWrapper(fromStoreValue(deserializeCacheValue(result))) : null; } @@ -273,8 +284,7 @@ public void evict(Object key) { public CompletableFuture retrieve(Object key) { if (!getCacheWriter().supportsAsyncRetrieve()) { - throw new UnsupportedOperationException( - "The Redis driver configured with RedisCache through RedisCacheWriter does not support CompletableFuture-based retrieval"); + throw new UnsupportedOperationException(CACHE_RETRIEVAL_UNSUPPORTED_OPERATION_EXCEPTION_MESSAGE); } return retrieveValue(key).thenApply(this::nullSafeDeserializedStoreValue); @@ -285,27 +295,28 @@ public CompletableFuture retrieve(Object key) { public CompletableFuture retrieve(Object key, Supplier> valueLoader) { if (!getCacheWriter().supportsAsyncRetrieve()) { - throw new UnsupportedOperationException( - "The Redis driver configured with RedisCache through RedisCacheWriter does not support CompletableFuture-based retrieval"); + throw new UnsupportedOperationException(CACHE_RETRIEVAL_UNSUPPORTED_OPERATION_EXCEPTION_MESSAGE); } - return retrieveValue(key) // - .thenCompose(bytes -> { + return retrieveValue(key).thenCompose(bytes -> { + + if (bytes != null) { + return CompletableFuture.completedFuture((T) nullSafeDeserializedStoreValue(bytes)); + } + + return valueLoader.get().thenCompose(value -> { - if (bytes != null) { - return CompletableFuture.completedFuture((T) nullSafeDeserializedStoreValue(bytes)); - } + Object cacheValue = processAndCheckValue(value); - return valueLoader.get().thenCompose(value -> { + byte[] binaryKey = createAndConvertCacheKey(key); + byte[] binaryValue = serializeCacheValue(cacheValue); - Object cacheValue = processAndCheckValue(value); + Duration timeToLive = getTimeToLive(key, cacheValue); - return getCacheWriter() - .store(getName(), createAndConvertCacheKey(key), serializeCacheValue(cacheValue), - getTimeToLive(key, cacheValue)) // - .thenApply(v -> value); - }); - }); + return getCacheWriter().store(getName(), binaryKey, binaryValue, timeToLive) + .thenApply(v -> value); + }); + }); } private Object processAndCheckValue(@Nullable Object value) { @@ -403,8 +414,8 @@ protected String createCacheKey(Object key) { */ protected String convertKey(Object key) { - if (key instanceof String) { - return (String) key; + if (key instanceof String stringKey) { + return stringKey; } TypeDescriptor source = TypeDescriptor.forObject(key); @@ -429,9 +440,8 @@ protected String convertKey(Object key) { return key.toString(); } - String message = String.format( - "Cannot convert cache key %s to String; Please register a suitable Converter" - + " via 'RedisCacheConfiguration.configureKeyConverters(...)' or override '%s.toString()'", + String message = String.format("Cannot convert cache key %s to String; Please register a suitable Converter" + + " via 'RedisCacheConfiguration.configureKeyConverters(...)' or override '%s.toString()'", source, key.getClass().getName()); throw new IllegalStateException(message); diff --git a/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java b/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java index 83eefb8006..ffd4dafa1e 100644 --- a/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java +++ b/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java @@ -34,8 +34,8 @@ /** * {@link CacheManager} implementation for Redis backed by {@link RedisCache}. *

- * This {@link CacheManager} creates {@link Cache caches} on first write, by default. Empty {@link Cache caches} are not - * visible in Redis due to how Redis represents empty data structures. + * This {@link CacheManager} creates {@link Cache caches} on first write, by default. Empty {@link Cache caches} + * are not visible in Redis due to how Redis represents empty data structures. *

* {@link Cache Caches} requiring a different {@link RedisCacheConfiguration cache configuration} than the * {@link RedisCacheConfiguration#defaultCacheConfig() default cache configuration} can be specified via @@ -46,11 +46,11 @@ * @author Mark Paluch * @author Yanming Zhou * @author John Blum - * @see RedisCache - * @see RedisCacheConfiguration - * @see RedisCacheWriter - * @see org.springframework.cache.CacheManager + * @see org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager * @see org.springframework.data.redis.connection.RedisConnectionFactory + * @see org.springframework.data.redis.cache.RedisCacheConfiguration + * @see org.springframework.data.redis.cache.RedisCacheWriter + * @see org.springframework.data.redis.cache.RedisCache * @since 2.0 */ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager { @@ -66,17 +66,17 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager private final Map initialCacheConfiguration; /** - * Creates a new {@link RedisCacheManager} initialized with the given {@link RedisCacheWriter} and a default + * Creates a new {@link RedisCacheManager} initialized with the given {@link RedisCacheWriter} and default * {@link RedisCacheConfiguration}. *

* Allows {@link RedisCache cache} creation at runtime. * - * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations by executing appropriate - * Redis commands; must not be {@literal null}. - * @param defaultCacheConfiguration {@link RedisCacheConfiguration} applied to new {@link RedisCache Redis caches} by - * default when no cache-specific {@link RedisCacheConfiguration} is provided; must not be {@literal null}. + * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations + * by executing appropriate Redis commands; must not be {@literal null}. + * @param defaultCacheConfiguration {@link RedisCacheConfiguration} applied to new {@link RedisCache Redis caches} + * by default when no cache-specific {@link RedisCacheConfiguration} is provided; must not be {@literal null}. * @throws IllegalArgumentException if either the given {@link RedisCacheWriter} or {@link RedisCacheConfiguration} - * are {@literal null}. + * are {@literal null}. * @see org.springframework.data.redis.cache.RedisCacheConfiguration * @see org.springframework.data.redis.cache.RedisCacheWriter */ @@ -85,17 +85,17 @@ public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration d } /** - * Creates a new {@link RedisCacheManager} initialized with the given {@link RedisCacheWriter} and default - * {@link RedisCacheConfiguration}, and whether to allow cache creation at runtime. + * Creates a new {@link RedisCacheManager} initialized with the given {@link RedisCacheWriter} + * and default {@link RedisCacheConfiguration} along with whether to allow cache creation at runtime. * - * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations by executing appropriate - * Redis commands; must not be {@literal null}. - * @param defaultCacheConfiguration {@link RedisCacheConfiguration} applied to new {@link RedisCache Redis caches} by - * default when no cache-specific {@link RedisCacheConfiguration} is provided; must not be {@literal null}. + * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations + * by executing appropriate Redis commands; must not be {@literal null}. + * @param defaultCacheConfiguration {@link RedisCacheConfiguration} applied to new {@link RedisCache Redis caches} + * by default when no cache-specific {@link RedisCacheConfiguration} is provided; must not be {@literal null}. * @param allowRuntimeCacheCreation boolean specifying whether to allow creation of undeclared caches at runtime; - * {@literal true} by default. Maybe just use {@link RedisCacheConfiguration#defaultCacheConfig()}. + * {@literal true} by default. Maybe just use {@link RedisCacheConfiguration#defaultCacheConfig()}. * @throws IllegalArgumentException if either the given {@link RedisCacheWriter} or {@link RedisCacheConfiguration} - * are {@literal null}. + * are {@literal null}. * @see org.springframework.data.redis.cache.RedisCacheConfiguration * @see org.springframework.data.redis.cache.RedisCacheWriter * @since 2.0.4 @@ -113,19 +113,19 @@ private RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration /** * Creates a new {@link RedisCacheManager} initialized with the given {@link RedisCacheWriter} and a default - * {@link RedisCacheConfiguration}, along with an optional, initial set of {@link String cache names} used to create - * {@link RedisCache Redis caches} on startup. + * {@link RedisCacheConfiguration} along with an optional, initial set of {@link String cache names} + * used to create {@link RedisCache Redis caches} on startup. *

* Allows {@link RedisCache cache} creation at runtime. * - * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations by executing appropriate - * Redis commands; must not be {@literal null}. - * @param defaultCacheConfiguration {@link RedisCacheConfiguration} applied to new {@link RedisCache Redis caches} by - * default when no cache-specific {@link RedisCacheConfiguration} is provided; must not be {@literal null}. + * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations + * by executing appropriate Redis commands; must not be {@literal null}. + * @param defaultCacheConfiguration {@link RedisCacheConfiguration} applied to new {@link RedisCache Redis caches} + * by default when no cache-specific {@link RedisCacheConfiguration} is provided; must not be {@literal null}. * @param initialCacheNames optional set of {@link String cache names} used to create {@link RedisCache Redis caches} - * on startup. The default {@link RedisCacheConfiguration} will be applied to each cache. + * on startup. The default {@link RedisCacheConfiguration} will be applied to each cache. * @throws IllegalArgumentException if either the given {@link RedisCacheWriter} or {@link RedisCacheConfiguration} - * are {@literal null}. + * are {@literal null}. * @see org.springframework.data.redis.cache.RedisCacheConfiguration * @see org.springframework.data.redis.cache.RedisCacheWriter */ @@ -137,21 +137,21 @@ public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration d /** * Creates a new {@link RedisCacheManager} initialized with the given {@link RedisCacheWriter} and default - * {@link RedisCacheConfiguration}, and whether to allow cache creation at runtime. + * {@link RedisCacheConfiguration} along with whether to allow cache creation at runtime. *

- * Additionally, the optional, initial set of {@link String cache names} will be used to create {@link RedisCache - * Redis caches} on startup. + * Additionally, the optional, initial set of {@link String cache names} will be used to + * create {@link RedisCache Redis caches} on startup. * - * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations by executing appropriate - * Redis commands; must not be {@literal null}. - * @param defaultCacheConfiguration {@link RedisCacheConfiguration} applied to new {@link RedisCache Redis caches} by - * default when no cache-specific {@link RedisCacheConfiguration} is provided; must not be {@literal null}. + * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations + * by executing appropriate Redis commands; must not be {@literal null}. + * @param defaultCacheConfiguration {@link RedisCacheConfiguration} applied to new {@link RedisCache Redis caches} + * by default when no cache-specific {@link RedisCacheConfiguration} is provided; must not be {@literal null}. * @param allowRuntimeCacheCreation boolean specifying whether to allow creation of undeclared caches at runtime; - * {@literal true} by default. Maybe just use {@link RedisCacheConfiguration#defaultCacheConfig()}. + * {@literal true} by default. Maybe just use {@link RedisCacheConfiguration#defaultCacheConfig()}. * @param initialCacheNames optional set of {@link String cache names} used to create {@link RedisCache Redis caches} - * on startup. The default {@link RedisCacheConfiguration} will be applied to each cache. + * on startup. The default {@link RedisCacheConfiguration} will be applied to each cache. * @throws IllegalArgumentException if either the given {@link RedisCacheWriter} or {@link RedisCacheConfiguration} - * are {@literal null}. + * are {@literal null}. * @see org.springframework.data.redis.cache.RedisCacheConfiguration * @see org.springframework.data.redis.cache.RedisCacheWriter * @since 2.0.4 @@ -175,15 +175,15 @@ public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration d *

* Allows {@link RedisCache cache} creation at runtime. * - * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations by executing appropriate - * Redis commands; must not be {@literal null}. - * @param defaultCacheConfiguration {@link RedisCacheConfiguration} applied to new {@link RedisCache Redis caches} by - * default when no cache-specific {@link RedisCacheConfiguration} is provided; must not be {@literal null}. + * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations + * by executing appropriate Redis commands; must not be {@literal null}. + * @param defaultCacheConfiguration {@link RedisCacheConfiguration} applied to new {@link RedisCache Redis caches} + * by default when no cache-specific {@link RedisCacheConfiguration} is provided; must not be {@literal null}. * @param initialCacheConfigurations {@link Map} of declared, known {@link String cache names} along with associated - * {@link RedisCacheConfiguration} used to create and configure {@link RedisCache Reds caches} on startup; - * must not be {@literal null}. + * {@link RedisCacheConfiguration} used to create and configure {@link RedisCache Reds caches} on startup; + * must not be {@literal null}. * @throws IllegalArgumentException if either the given {@link RedisCacheWriter} or {@link RedisCacheConfiguration} - * are {@literal null}. + * are {@literal null}. * @see org.springframework.data.redis.cache.RedisCacheConfiguration * @see org.springframework.data.redis.cache.RedisCacheWriter */ @@ -200,17 +200,17 @@ public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration d * Additionally, an initial {@link RedisCache} will be created and configured using the associated * {@link RedisCacheConfiguration} for each {@link String named} {@link RedisCache} in the given {@link Map}. * - * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations by executing appropriate - * Redis commands; must not be {@literal null}. - * @param defaultCacheConfiguration {@link RedisCacheConfiguration} applied to new {@link RedisCache Redis caches} by - * default when no cache-specific {@link RedisCacheConfiguration} is provided; must not be {@literal null}. + * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations + * by executing appropriate Redis commands; must not be {@literal null}. + * @param defaultCacheConfiguration {@link RedisCacheConfiguration} applied to new {@link RedisCache Redis caches} + * by default when no cache-specific {@link RedisCacheConfiguration} is provided; must not be {@literal null}. * @param allowRuntimeCacheCreation boolean specifying whether to allow creation of undeclared caches at runtime; - * {@literal true} by default. Maybe just use {@link RedisCacheConfiguration#defaultCacheConfig()}. + * {@literal true} by default. Maybe just use {@link RedisCacheConfiguration#defaultCacheConfig()}. * @param initialCacheConfigurations {@link Map} of declared, known {@link String cache names} along with the - * associated {@link RedisCacheConfiguration} used to create and configure {@link RedisCache Redis caches} on - * startup; must not be {@literal null}. + * associated {@link RedisCacheConfiguration} used to create and configure {@link RedisCache Redis caches} + * on startup; must not be {@literal null}. * @throws IllegalArgumentException if either the given {@link RedisCacheWriter} or {@link RedisCacheConfiguration} - * are {@literal null}. + * are {@literal null}. * @see org.springframework.data.redis.cache.RedisCacheConfiguration * @see org.springframework.data.redis.cache.RedisCacheWriter * @since 2.0.4 @@ -226,13 +226,12 @@ public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration d } /** - * @deprecated since 3.2. Use - * {@link RedisCacheManager#RedisCacheManager(RedisCacheWriter, RedisCacheConfiguration, boolean, Map)} - * instead. + * @deprecated since 3.2. Use {@link RedisCacheManager#RedisCacheManager(RedisCacheWriter, RedisCacheConfiguration, boolean, Map)} instead. */ @Deprecated(since = "3.2") public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, Map initialCacheConfigurations, boolean allowRuntimeCacheCreation) { + this(cacheWriter, defaultCacheConfiguration, allowRuntimeCacheCreation, initialCacheConfigurations); } @@ -250,8 +249,8 @@ public static RedisCacheManagerBuilder builder() { * Factory method returning a {@literal Builder} used to construct and configure a {@link RedisCacheManager} * initialized with the given {@link RedisCacheWriter}. * - * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations by executing appropriate - * Redis commands; must not be {@literal null}. + * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations + * by executing appropriate Redis commands; must not be {@literal null}. * @return new {@link RedisCacheManagerBuilder}. * @throws IllegalArgumentException if the given {@link RedisCacheWriter} is {@literal null}. * @see org.springframework.data.redis.cache.RedisCacheWriter @@ -267,8 +266,8 @@ public static RedisCacheManagerBuilder builder(RedisCacheWriter cacheWriter) { * Factory method returning a {@literal Builder} used to construct and configure a {@link RedisCacheManager} * initialized with the given {@link RedisConnectionFactory}. * - * @param connectionFactory {@link RedisConnectionFactory} used by the {@link RedisCacheManager} to acquire - * connections to Redis when performing {@link RedisCache} operations; must not be {@literal null}. + * @param connectionFactory {@link RedisConnectionFactory} used by the {@link RedisCacheManager} + * to acquire connections to Redis when performing {@link RedisCache} operations; must not be {@literal null}. * @return new {@link RedisCacheManagerBuilder}. * @throws IllegalArgumentException if the given {@link RedisConnectionFactory} is {@literal null}. * @see org.springframework.data.redis.connection.RedisConnectionFactory @@ -298,8 +297,8 @@ public static RedisCacheManagerBuilder builder(RedisConnectionFactory connection *

enabled
* * - * @param connectionFactory {@link RedisConnectionFactory} used by the {@link RedisCacheManager} to acquire - * connections to Redis when performing {@link RedisCache} operations; must not be {@literal null}. + * @param connectionFactory {@link RedisConnectionFactory} used by the {@link RedisCacheManager} + * to acquire connections to Redis when performing {@link RedisCache} operations; must not be {@literal null}. * @return new {@link RedisCacheManager}. * @throws IllegalArgumentException if the given {@link RedisConnectionFactory} is {@literal null}. * @see org.springframework.data.redis.connection.RedisConnectionFactory @@ -308,9 +307,10 @@ public static RedisCacheManager create(RedisConnectionFactory connectionFactory) Assert.notNull(connectionFactory, "ConnectionFactory must not be null"); - return new RedisCacheManager( - org.springframework.data.redis.cache.RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory), - RedisCacheConfiguration.defaultCacheConfig()); + RedisCacheWriter cacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory); + RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig(); + + return new RedisCacheManager(cacheWriter, cacheConfiguration); } /** @@ -326,8 +326,8 @@ public boolean isAllowRuntimeCacheCreation() { * Return an {@link Collections#unmodifiableMap(Map) unmodifiable Map} containing {@link String caches name} mapped to * the {@link RedisCache} {@link RedisCacheConfiguration configuration}. * - * @return unmodifiable {@link Map} containing {@link String cache name} / {@link RedisCacheConfiguration - * configuration} pairs. + * @return unmodifiable {@link Map} containing {@link String cache name} + * / {@link RedisCacheConfiguration configuration} pairs. */ public Map getCacheConfigurations() { @@ -353,8 +353,8 @@ protected RedisCacheConfiguration getDefaultCacheConfiguration() { } /** - * Gets a {@link Map} of {@link String cache names} to {@link RedisCacheConfiguration} objects as the initial set of - * {@link RedisCache Redis caches} to create on startup. + * Gets a {@link Map} of {@link String cache names} to {@link RedisCacheConfiguration} objects as the initial set + * of {@link RedisCache Redis caches} to create on startup. * * @return a {@link Map} of {@link String cache names} to {@link RedisCacheConfiguration} objects. */ @@ -363,8 +363,8 @@ protected Map getInitialCacheConfiguration() { } /** - * Returns a reference to the configured {@link RedisCacheWriter} used to perform {@link RedisCache} operations, such - * as reading from and writing to the cache. + * Returns a reference to the configured {@link RedisCacheWriter} used to perform {@link RedisCache} operations, + * such as reading from and writing to the cache. * * @return a reference to the configured {@link RedisCacheWriter}. * @see org.springframework.data.redis.cache.RedisCacheWriter @@ -382,8 +382,8 @@ protected RedisCache getMissingCache(String name) { * Creates a new {@link RedisCache} with given {@link String name} and {@link RedisCacheConfiguration}. * * @param name {@link String name} for the {@link RedisCache}; must not be {@literal null}. - * @param cacheConfiguration {@link RedisCacheConfiguration} used to configure the {@link RedisCache}; resolves to the - * {@link #getDefaultCacheConfiguration()} if {@literal null}. + * @param cacheConfiguration {@link RedisCacheConfiguration} used to configure the {@link RedisCache}; + * resolves to the {@link #getDefaultCacheConfiguration()} if {@literal null}. * @return a new {@link RedisCache} instance; never {@literal null}. */ protected RedisCache createRedisCache(String name, @Nullable RedisCacheConfiguration cacheConfiguration) { @@ -416,8 +416,8 @@ public static class RedisCacheManagerBuilder { * Factory method returning a new {@literal Builder} used to create and configure a {@link RedisCacheManager} using * the given {@link RedisCacheWriter}. * - * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations by executing - * appropriate Redis commands; must not be {@literal null}. + * @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations + * by executing appropriate Redis commands; must not be {@literal null}. * @return new {@link RedisCacheManagerBuilder}. * @throws IllegalArgumentException if the given {@link RedisCacheWriter} is {@literal null}. * @see org.springframework.data.redis.cache.RedisCacheWriter @@ -430,16 +430,17 @@ public static RedisCacheManagerBuilder fromCacheWriter(RedisCacheWriter cacheWri * Factory method returning a new {@literal Builder} used to create and configure a {@link RedisCacheManager} using * the given {@link RedisConnectionFactory}. * - * @param connectionFactory {@link RedisConnectionFactory} used by the {@link RedisCacheManager} to acquire - * connections to Redis when performing {@link RedisCache} operations; must not be {@literal null}. + * @param connectionFactory {@link RedisConnectionFactory} used by the {@link RedisCacheManager} + * to acquire connections to Redis when performing {@link RedisCache} operations; must not be {@literal null}. * @return new {@link RedisCacheManagerBuilder}. * @throws IllegalArgumentException if the given {@link RedisConnectionFactory} is {@literal null}. * @see org.springframework.data.redis.connection.RedisConnectionFactory */ public static RedisCacheManagerBuilder fromConnectionFactory(RedisConnectionFactory connectionFactory) { - RedisCacheWriter cacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter( - RedisAssertions.requireNonNull(connectionFactory, "ConnectionFactory must not be null")); + Assert.notNull(connectionFactory, "ConnectionFactory must not be null"); + + RedisCacheWriter cacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory); return new RedisCacheManagerBuilder(cacheWriter); } @@ -464,8 +465,8 @@ private RedisCacheManagerBuilder(RedisCacheWriter cacheWriter) { /** * Configure whether to allow cache creation at runtime. * - * @param allowRuntimeCacheCreation boolean to allow creation of undeclared caches at runtime; {@literal true} by - * default. + * @param allowRuntimeCacheCreation boolean to allow creation of undeclared caches at runtime; + * {@literal true} by default. * @return this {@link RedisCacheManagerBuilder}. */ public RedisCacheManagerBuilder allowCreateOnMissingCache(boolean allowRuntimeCacheCreation) { @@ -476,9 +477,9 @@ public RedisCacheManagerBuilder allowCreateOnMissingCache(boolean allowRuntimeCa /** * Disable {@link RedisCache} creation at runtime for non-configured, undeclared caches. *

- * {@link RedisCacheManager#getMissingCache(String)} returns {@literal null} for any non-configured, undeclared - * {@link Cache} instead of a new {@link RedisCache} instance. This allows the - * {@link org.springframework.cache.support.CompositeCacheManager} to participate. + * {@link RedisCacheManager#getMissingCache(String)} returns {@literal null} for any non-configured, + * undeclared {@link Cache} instead of a new {@link RedisCache} instance. + * This allows the {@link org.springframework.cache.support.CompositeCacheManager} to participate. * * @return this {@link RedisCacheManagerBuilder}. * @see #allowCreateOnMissingCache(boolean) @@ -518,8 +519,9 @@ public RedisCacheConfiguration cacheDefaults() { */ public RedisCacheManagerBuilder cacheDefaults(RedisCacheConfiguration defaultCacheConfiguration) { - this.defaultCacheConfiguration = RedisAssertions.requireNonNull(defaultCacheConfiguration, - "DefaultCacheConfiguration must not be null"); + Assert.notNull(defaultCacheConfiguration, "DefaultCacheConfiguration must not be null"); + + this.defaultCacheConfiguration = defaultCacheConfiguration; return this; } @@ -573,8 +575,8 @@ public RedisCacheManagerBuilder transactionAware() { } /** - * Registers the given {@link String cache name} and {@link RedisCacheConfiguration} used to create and configure a - * {@link RedisCache} on startup. + * Registers the given {@link String cache name} and {@link RedisCacheConfiguration} used to create + * and configure a {@link RedisCache} on startup. * * @param cacheName {@link String name} of the cache to register for creation on startup. * @param cacheConfiguration {@link RedisCacheConfiguration} used to configure the new cache on startup. @@ -603,7 +605,7 @@ public RedisCacheManagerBuilder withInitialCacheConfigurations( RedisAssertions.requireNonNull(cacheConfigurations, "CacheConfigurations must not be null") .forEach((cacheName, cacheConfiguration) -> RedisAssertions.requireNonNull(cacheConfiguration, - "RedisCacheConfiguration for cache [%s] must not be null", cacheName)); + "RedisCacheConfiguration for cache [%s] must not be null", cacheName)); this.initialCaches.putAll(cacheConfigurations); @@ -624,8 +626,8 @@ public Optional getCacheConfigurationFor(String cacheNa /** * Get the {@link Set} of cache names for which the builder holds {@link RedisCacheConfiguration configuration}. * - * @return an unmodifiable {@link Set} holding the name of caches for which a {@link RedisCacheConfiguration - * configuration} has been set. + * @return an unmodifiable {@link Set} holding the name of caches + * for which a {@link RedisCacheConfiguration configuration} has been set. * @since 2.2 */ public Set getConfiguredCaches() { diff --git a/src/main/java/org/springframework/data/redis/cache/RedisCacheWriter.java b/src/main/java/org/springframework/data/redis/cache/RedisCacheWriter.java index f6da33eaf7..f9dc937fa9 100644 --- a/src/main/java/org/springframework/data/redis/cache/RedisCacheWriter.java +++ b/src/main/java/org/springframework/data/redis/cache/RedisCacheWriter.java @@ -24,14 +24,14 @@ import org.springframework.util.Assert; /** - * {@link RedisCacheWriter} provides low-level access to Redis commands ({@code SET, SETNX, GET, EXPIRE,...}) used for - * caching. + * {@link RedisCacheWriter} provides low-level access to Redis commands ({@code SET, SETNX, GET, EXPIRE,...}) + * used for caching. *

* The {@link RedisCacheWriter} may be shared by multiple cache implementations and is responsible for reading/writing * binary data from/to Redis. The implementation honors potential cache lock flags that might be set. *

- * The default {@link RedisCacheWriter} implementation can be customized with {@link BatchStrategy} to tune performance - * behavior. + * The default {@link RedisCacheWriter} implementation can be customized with {@link BatchStrategy} + * to tune performance behavior. * * @author Christoph Strobl * @author Mark Paluch @@ -96,8 +96,9 @@ static RedisCacheWriter lockingRedisCacheWriter(RedisConnectionFactory connectio * * @param connectionFactory must not be {@literal null}. * @param sleepTime sleep time between lock access attempts, must not be {@literal null}. - * @param lockTtlFunction TTL function to compute the Lock TTL. The function is called with contextual keys and values - * (such as the cache name on cleanup or the actual key/value on put requests). Must not be {@literal null}. + * @param lockTtlFunction TTL function to compute the Lock TTL. The function is called with contextual keys + * and values (such as the cache name on cleanup or the actual key/value on put requests); + * must not be {@literal null}. * @param batchStrategy must not be {@literal null}. * @return new instance of {@link DefaultRedisCacheWriter}. * @since 3.2 @@ -123,8 +124,8 @@ static RedisCacheWriter lockingRedisCacheWriter(RedisConnectionFactory connectio byte[] get(String name, byte[] key); /** - * Get the binary value representation from Redis stored for the given key and set the given {@link Duration TTL - * expiration} for the cache entry. + * Get the binary value representation from Redis stored for the given key and set + * the given {@link Duration TTL expiration} for the cache entry. * * @param name must not be {@literal null}. * @param key must not be {@literal null}. @@ -137,14 +138,14 @@ default byte[] get(String name, byte[] key, @Nullable Duration ttl) { } /** - * Determines whether the asynchronous {@link #retrieve(String, byte[])} and - * {@link #retrieve(String, byte[], Duration)} cache operations are supported by the implementation. + * Determines whether the asynchronous {@link #retrieve(String, byte[])} + * and {@link #retrieve(String, byte[], Duration)} cache operations are supported by the implementation. *

- * The main factor for whether the {@literal retrieve} operation can be supported will primarily be determined by the - * Redis driver in use at runtime. + * The main factor for whether the {@literal retrieve} operation can be supported will primarily be determined + * by the Redis driver in use at runtime. *

- * Returns {@literal false} by default. This will have an effect of {@link RedisCache#retrieve(Object)} and - * {@link RedisCache#retrieve(Object, Supplier)} throwing an {@link UnsupportedOperationException}. + * Returns {@literal false} by default. This will have an effect of {@link RedisCache#retrieve(Object)} + * and {@link RedisCache#retrieve(Object, Supplier)} throwing an {@link UnsupportedOperationException}. * * @return {@literal true} if asynchronous {@literal retrieve} operations are supported by the implementation. * @since 3.2 @@ -154,7 +155,8 @@ default boolean supportsAsyncRetrieve() { } /** - * Returns the {@link CompletableFuture value} to which the {@link RedisCache} maps the given {@link byte[] key}. + * Asynchronously retrieves the {@link CompletableFuture value} to which the {@link RedisCache} + * maps the given {@link byte[] key}. *

* This operation is non-blocking. * @@ -169,8 +171,8 @@ default CompletableFuture retrieve(String name, byte[] key) { } /** - * Returns the {@link CompletableFuture value} to which the {@link RedisCache} maps the given {@link byte[] key} - * setting the {@link Duration TTL expiration} for the cache entry. + * Asynchronously retrieves the {@link CompletableFuture value} to which the {@link RedisCache} maps + * the given {@link byte[] key} setting the {@link Duration TTL expiration} for the cache entry. *

* This operation is non-blocking. * @@ -260,10 +262,10 @@ interface TtlFunction { Duration NO_EXPIRATION = Duration.ZERO; /** - * Creates a singleton {@link TtlFunction} using the given {@link Duration}. + * Creates a {@literal Singleton} {@link TtlFunction} using the given {@link Duration}. * - * @param duration the time to live. Can be {@link Duration#ZERO} for persistent values (i.e. cache entry does not - * expire). + * @param duration the time to live. Can be {@link Duration#ZERO} for persistent values (i.e. cache entry + * does not expire). * @return a singleton {@link TtlFunction} using {@link Duration}. */ static TtlFunction just(Duration duration) {