DATAREDIS-1041 - Introduce RedisCache configuration option prefixCacheNameWith.

prefixCacheNameWith allows to prefix the cache name. The default CacheKeyPrefix.simple() format is still in place.

Original pull request: #507.
This commit is contained in:
Christoph Strobl
2020-01-10 12:59:05 +01:00
committed by Mark Paluch
parent f823062ef0
commit 94699ce79c
3 changed files with 61 additions and 2 deletions

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.data.redis.cache;
import org.springframework.util.Assert;
/**
* {@link CacheKeyPrefix} provides a hook for creating custom prefixes prepended to the actual {@literal key} stored in
* Redis.
@@ -26,6 +28,13 @@ package org.springframework.data.redis.cache;
@FunctionalInterface
public interface CacheKeyPrefix {
/**
* Default separator.
*
* @since 2.3
*/
String SEPARATOR = "::";
/**
* Compute the prefix for the actual {@literal key} stored in Redis.
*
@@ -41,6 +50,21 @@ public interface CacheKeyPrefix {
* @return the default {@link CacheKeyPrefix} scheme.
*/
static CacheKeyPrefix simple() {
return name -> name + "::";
return name -> name + SEPARATOR;
}
/**
* Creates a {@link CacheKeyPrefix} scheme that prefixes cache keys with the given {@code prefix} prepended to the
* {@code cacheName} followed by double colons. A cache named {@code myCache} with prefix {@code redis-} will result
* in {@code redis-myCache::}.
*
* @param prefix must not be {@literal null}.
* @return the default {@link CacheKeyPrefix} scheme.
* @since 2.3
*/
static CacheKeyPrefix prefixed(String prefix) {
Assert.notNull(prefix, "Prefix must not be null!");
return name -> prefix + name + SEPARATOR;
}
}

View File

@@ -144,11 +144,16 @@ public class RedisCacheConfiguration {
}
/**
* Use the given prefix instead of the default one.
* Use the given prefix instead of the default one. <br />
* This option overrides the default cache name and is not recommended. {@link #prefixCacheNameWith(String)} or
* {@link #computePrefixWith(CacheKeyPrefix)}. <br />
* The generated cache key will be: {@code prefix + cache entry key}.
*
* @param prefix must not be {@literal null}.
* @return new {@link RedisCacheConfiguration}.
* @deprecated since 2.3. Use {@link #prefixCacheNameWith(String)} or {@link #computePrefixWith(CacheKeyPrefix)} instead.
*/
@Deprecated
public RedisCacheConfiguration prefixKeysWith(String prefix) {
Assert.notNull(prefix, "Prefix must not be null!");
@@ -156,6 +161,20 @@ public class RedisCacheConfiguration {
return computePrefixWith((cacheName) -> prefix);
}
/**
* Prefix the {@link RedisCache#getName() cache name} with the given value. <br />
* The generated cache key will be: {@code prefix + cache name + "::" + cache entry key}.
*
* @param prefix the prefix to prepend to the cache name.
* @return this.
* @see #computePrefixWith(CacheKeyPrefix)
* @see CacheKeyPrefix#prefixed(String)
* @since 2.3
*/
public RedisCacheConfiguration prefixCacheNameWith(String prefix) {
return computePrefixWith(CacheKeyPrefix.prefixed(prefix));
}
/**
* Use the given {@link CacheKeyPrefix} to compute the prefix for the actual Redis {@literal key} on the
* {@literal cache name}.