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:
Christoph Strobl
2018-02-15 13:27:05 +01:00
committed by Mark Paluch
parent 67095bca8c
commit 3c0e3050ed
5 changed files with 137 additions and 7 deletions

View File

@@ -269,6 +269,37 @@ public class RedisCacheTests {
});
}
@Test // DATAREDIS-715
public void computePrefixCreatesCacheKeyCorrectly() {
RedisCache cacheWithCustomPrefix = new RedisCache("cache", new DefaultRedisCacheWriter(connectionFactory),
RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(SerializationPair.fromSerializer(serializer))
.computePrefixWith(cacheName -> "_" + cacheName + "_"));
cacheWithCustomPrefix.put("key-1", sample);
doWithConnection(connection -> {
assertThat(connection.stringCommands().get("_cache_key-1".getBytes(Charset.forName("UTF-8"))))
.isEqualTo(binarySample);
});
}
@Test // DATAREDIS-715
public void fetchKeyWithComputedPrefixReturnsExpectedResult() {
doWithConnection(connection -> connection.set("_cache_key-1".getBytes(Charset.forName("UTF-8")), binarySample));
RedisCache cacheWithCustomPrefix = new RedisCache("cache", new DefaultRedisCacheWriter(connectionFactory),
RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(SerializationPair.fromSerializer(serializer))
.computePrefixWith(cacheName -> "_" + cacheName + "_"));
ValueWrapper result = cacheWithCustomPrefix.get(key);
assertThat(result).isNotNull();
assertThat(result.get()).isEqualTo(sample);
}
void doWithConnection(Consumer<RedisConnection> callback) {
RedisConnection connection = connectionFactory.getConnection();
try {