DATAREDIS-715 - Allow computation of cache key prefix.
We now allow, in addition to a static key prefix, the computation of prefixes based on the cache name. This change introduces an alternative to the in 2.x removed org.springframework.data.redis.cache.RedisCacheKey, and gives users more control over cache key generation. Original pull request: #313.
This commit is contained in:
committed by
Mark Paluch
parent
67095bca8c
commit
3c0e3050ed
35
src/main/java/org/springframework/data/redis/cache/CacheKeyPrefix.java
vendored
Normal file
35
src/main/java/org/springframework/data/redis/cache/CacheKeyPrefix.java
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.redis.cache;
|
||||
|
||||
/**
|
||||
* {@link CacheKeyPrefix} provides a hook for creating custom prefixes prepended to the actual {@literal key} stored in
|
||||
* Redis.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @since 2.0.4
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface CacheKeyPrefix {
|
||||
|
||||
/**
|
||||
* Compute the prefix for the actual {@literal key} stored in Redis.
|
||||
*
|
||||
* @param cacheName will never be {@literal null}.
|
||||
* @return never {@literal null}.
|
||||
*/
|
||||
String compute(String cacheName);
|
||||
}
|
||||
@@ -300,7 +300,7 @@ public class RedisCache extends AbstractValueAdaptingCache {
|
||||
}
|
||||
|
||||
private String prefixCacheKey(String key) {
|
||||
return cacheConfig.getKeyPrefix().orElseGet(() -> name + "::") + key;
|
||||
return cacheConfig.getKeyPrefixFor(name).orElseGet(() -> name + "::") + key;
|
||||
}
|
||||
|
||||
private static <T> T valueFromLoader(Object key, Callable<T> valueLoader) {
|
||||
|
||||
@@ -18,6 +18,7 @@ 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;
|
||||
@@ -43,7 +44,7 @@ public class RedisCacheConfiguration {
|
||||
|
||||
private final Duration ttl;
|
||||
private final boolean cacheNullValues;
|
||||
private final @Nullable String keyPrefix;
|
||||
private final @Nullable CacheKeyPrefix keyPrefix;
|
||||
private final boolean usePrefix;
|
||||
|
||||
private final SerializationPair<String> keySerializationPair;
|
||||
@@ -52,9 +53,9 @@ public class RedisCacheConfiguration {
|
||||
private final ConversionService conversionService;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private RedisCacheConfiguration(Duration ttl, Boolean cacheNullValues, Boolean usePrefix, @Nullable String keyPrefix,
|
||||
SerializationPair<String> keySerializationPair, SerializationPair<?> valueSerializationPair,
|
||||
ConversionService conversionService) {
|
||||
private RedisCacheConfiguration(Duration ttl, Boolean cacheNullValues, Boolean usePrefix,
|
||||
@Nullable CacheKeyPrefix keyPrefix, SerializationPair<String> keySerializationPair,
|
||||
SerializationPair<?> valueSerializationPair, ConversionService conversionService) {
|
||||
|
||||
this.ttl = ttl;
|
||||
this.cacheNullValues = cacheNullValues;
|
||||
@@ -122,10 +123,39 @@ public class RedisCacheConfiguration {
|
||||
|
||||
Assert.notNull(prefix, "Prefix must not be null!");
|
||||
|
||||
return prefixKeysWith((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
|
||||
* {@literal cache name}.
|
||||
*
|
||||
* @param function must not be {@literal null}.
|
||||
* @return new {@link RedisCacheConfiguration}.
|
||||
* @since 2.0.4
|
||||
*/
|
||||
public RedisCacheConfiguration computePrefixWith(Function<String, String> function) {
|
||||
|
||||
Assert.notNull(function, "Function for computing prefix must not be null!");
|
||||
|
||||
return prefixKeysWith(function::apply);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable caching {@literal null} values. <br />
|
||||
* <strong>NOTE</strong> any {@link org.springframework.cache.Cache#put(Object, Object)} operation involving
|
||||
@@ -196,14 +226,36 @@ public class RedisCacheConfiguration {
|
||||
|
||||
/**
|
||||
* @return never {@literal null}.
|
||||
* @deprecated since 2.0.4. Please use {@link #getKeyPrefixFor(String)}.
|
||||
*/
|
||||
@Deprecated()
|
||||
public Optional<String> getKeyPrefix() {
|
||||
return getCacheKeyPrefix().map(val -> val.compute(""));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the computed {@literal key} prefix for a given {@literal cacheName}.
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
* @since 2.0.4
|
||||
*/
|
||||
public Optional<String> getKeyPrefixFor(String cacheName) {
|
||||
return getCacheKeyPrefix().map(val -> val.compute(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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {@literal true} if cache keys need to be prefixed with the {@link #getKeyPrefix()} if present or the
|
||||
* default which resolves to {@link Cache#getName()}.
|
||||
* @return {@literal true} if cache keys need to be prefixed with the {@link #getKeyPrefixFor(String)} if present or
|
||||
* the default which resolves to {@link Cache#getName()}.
|
||||
*/
|
||||
public boolean usePrefix() {
|
||||
return usePrefix;
|
||||
|
||||
Reference in New Issue
Block a user