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
- * 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
* 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
- * {@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
* 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
* 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) {