DATAREDIS-715 - Polishing.
Rename prefixKeysWith(CacheKeyPrefix) to computePrefixWith(…) and drop computePrefixWith(Function) in favor of CacheKeyPrefix. Refactor nullable keyPrefix to non-nullable. Extract default prefixing scheme to CacheKeyPrefix.simple(). Original pull request: #313.
This commit is contained in:
@@ -20,6 +20,7 @@ package org.springframework.data.redis.cache;
|
||||
* Redis.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
* @since 2.0.4
|
||||
*/
|
||||
@FunctionalInterface
|
||||
@@ -32,4 +33,14 @@ public interface CacheKeyPrefix {
|
||||
* @return never {@literal null}.
|
||||
*/
|
||||
String compute(String cacheName);
|
||||
|
||||
/**
|
||||
* Creates a default {@link CacheKeyPrefix} scheme that prefixes cache keys with {@code cacheName} followed by double
|
||||
* colons. A cache named {@code myCache} will prefix all cache keys with {@code myCache::}.
|
||||
*
|
||||
* @return the default {@link CacheKeyPrefix} scheme.
|
||||
*/
|
||||
static CacheKeyPrefix simple() {
|
||||
return name -> name + "::";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -300,7 +300,9 @@ public class RedisCache extends AbstractValueAdaptingCache {
|
||||
}
|
||||
|
||||
private String prefixCacheKey(String key) {
|
||||
return cacheConfig.getKeyPrefixFor(name).orElseGet(() -> name + "::") + key;
|
||||
|
||||
// allow contextual cache names by computing the key prefix on every call.
|
||||
return cacheConfig.getKeyPrefixFor(name) + key;
|
||||
}
|
||||
|
||||
private static <T> T valueFromLoader(Object key, Callable<T> valueLoader) {
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.data.redis.cache;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.interceptor.SimpleKey;
|
||||
@@ -27,7 +26,6 @@ import org.springframework.core.convert.converter.ConverterRegistry;
|
||||
import org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.format.support.DefaultFormattingConversionService;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -44,7 +42,7 @@ public class RedisCacheConfiguration {
|
||||
|
||||
private final Duration ttl;
|
||||
private final boolean cacheNullValues;
|
||||
private final @Nullable CacheKeyPrefix keyPrefix;
|
||||
private final CacheKeyPrefix keyPrefix;
|
||||
private final boolean usePrefix;
|
||||
|
||||
private final SerializationPair<String> keySerializationPair;
|
||||
@@ -54,7 +52,7 @@ public class RedisCacheConfiguration {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private RedisCacheConfiguration(Duration ttl, Boolean cacheNullValues, Boolean usePrefix,
|
||||
@Nullable CacheKeyPrefix keyPrefix, SerializationPair<String> keySerializationPair,
|
||||
CacheKeyPrefix keyPrefix, SerializationPair<String> keySerializationPair,
|
||||
SerializationPair<?> valueSerializationPair, ConversionService conversionService) {
|
||||
|
||||
this.ttl = ttl;
|
||||
@@ -94,7 +92,7 @@ public class RedisCacheConfiguration {
|
||||
|
||||
registerDefaultConverters(conversionService);
|
||||
|
||||
return new RedisCacheConfiguration(Duration.ZERO, true, true, null,
|
||||
return new RedisCacheConfiguration(Duration.ZERO, true, true, CacheKeyPrefix.simple(),
|
||||
SerializationPair.fromSerializer(RedisSerializer.string()),
|
||||
SerializationPair.fromSerializer(RedisSerializer.java()), conversionService);
|
||||
}
|
||||
@@ -123,37 +121,23 @@ public class RedisCacheConfiguration {
|
||||
|
||||
Assert.notNull(prefix, "Prefix must not be null!");
|
||||
|
||||
return prefixKeysWith((cacheName) -> prefix);
|
||||
return computePrefixWith((cacheName) -> prefix);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the given {@link CacheKeyPrefix} computing the prefix based on the {@literal cache name}.
|
||||
*
|
||||
* @param prefix must not be {@literal null}.
|
||||
* @return new {@link RedisCacheConfiguration}.
|
||||
* @since 2.0.4
|
||||
*/
|
||||
public RedisCacheConfiguration prefixKeysWith(CacheKeyPrefix prefix) {
|
||||
|
||||
Assert.notNull(prefix, "Prefix must not be null!");
|
||||
|
||||
return new RedisCacheConfiguration(ttl, cacheNullValues, true, prefix, keySerializationPair, valueSerializationPair,
|
||||
conversionService);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the given {@link Function} to compute the prefix for the actual Redis {@literal key} on the
|
||||
* Use the given {@link CacheKeyPrefix} to compute the prefix for the actual Redis {@literal key} on the
|
||||
* {@literal cache name}.
|
||||
*
|
||||
* @param function must not be {@literal null}.
|
||||
* @param cacheKeyPrefix must not be {@literal null}.
|
||||
* @return new {@link RedisCacheConfiguration}.
|
||||
* @since 2.0.4
|
||||
*/
|
||||
public RedisCacheConfiguration computePrefixWith(Function<String, String> function) {
|
||||
public RedisCacheConfiguration computePrefixWith(CacheKeyPrefix cacheKeyPrefix) {
|
||||
|
||||
Assert.notNull(function, "Function for computing prefix must not be null!");
|
||||
Assert.notNull(cacheKeyPrefix, "Function for computing prefix must not be null!");
|
||||
|
||||
return prefixKeysWith(function::apply);
|
||||
return new RedisCacheConfiguration(ttl, cacheNullValues, true, cacheKeyPrefix, keySerializationPair,
|
||||
valueSerializationPair, conversionService);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -228,9 +212,9 @@ public class RedisCacheConfiguration {
|
||||
* @return never {@literal null}.
|
||||
* @deprecated since 2.0.4. Please use {@link #getKeyPrefixFor(String)}.
|
||||
*/
|
||||
@Deprecated()
|
||||
@Deprecated
|
||||
public Optional<String> getKeyPrefix() {
|
||||
return getCacheKeyPrefix().map(val -> val.compute(""));
|
||||
return usePrefix() ? Optional.of(keyPrefix.compute("")) : Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -239,18 +223,11 @@ public class RedisCacheConfiguration {
|
||||
* @return never {@literal null}.
|
||||
* @since 2.0.4
|
||||
*/
|
||||
public Optional<String> getKeyPrefixFor(String cacheName) {
|
||||
return getCacheKeyPrefix().map(val -> val.compute(cacheName));
|
||||
}
|
||||
public String getKeyPrefixFor(String cacheName) {
|
||||
|
||||
/**
|
||||
* Obtain the {@link CacheKeyPrefix} used to compute the actual {@literal key} prefix.
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
* @since 2.0.4
|
||||
*/
|
||||
public Optional<CacheKeyPrefix> getCacheKeyPrefix() {
|
||||
return Optional.ofNullable(keyPrefix);
|
||||
Assert.notNull(cacheName, "Cache name must not be null!");
|
||||
|
||||
return keyPrefix.compute(cacheName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user