From e4630f951d20ab25bc084c576da81a02df77b180 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 3 Sep 2019 13:04:57 +0200 Subject: [PATCH] DATAREDIS-1032 - Improve cache key converter registration. Original pull request: #475. --- .../data/redis/cache/RedisCache.java | 2 +- .../redis/cache/RedisCacheConfiguration.java | 33 +++++++++++++++++++ .../RedisCacheConfigurationUnitTests.java | 24 ++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) 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 607b032ec..9331e8a3d 100644 --- a/src/main/java/org/springframework/data/redis/cache/RedisCache.java +++ b/src/main/java/org/springframework/data/redis/cache/RedisCache.java @@ -309,7 +309,7 @@ public class RedisCache extends AbstractValueAdaptingCache { } throw new IllegalStateException(String.format( - "Cannot convert cache key %s to String. Please provide a suitable Converter via 'RedisCacheConfiguration.withConversionService(...)' or override '%s.toString()'.", + "Cannot convert cache key %s to String. Please register a suitable Converter via 'RedisCacheConfiguration.configureKeyConverters(...)' or override '%s.toString()'.", source, key != null ? key.getClass().getSimpleName() : "Object")); } diff --git a/src/main/java/org/springframework/data/redis/cache/RedisCacheConfiguration.java b/src/main/java/org/springframework/data/redis/cache/RedisCacheConfiguration.java index 3e7ba074d..5ee530116 100644 --- a/src/main/java/org/springframework/data/redis/cache/RedisCacheConfiguration.java +++ b/src/main/java/org/springframework/data/redis/cache/RedisCacheConfiguration.java @@ -18,10 +18,12 @@ package org.springframework.data.redis.cache; import java.nio.charset.StandardCharsets; import java.time.Duration; import java.util.Optional; +import java.util.function.Consumer; import org.springframework.cache.Cache; import org.springframework.cache.interceptor.SimpleKey; import org.springframework.core.convert.ConversionService; +import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.ConverterRegistry; import org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair; import org.springframework.data.redis.serializer.RedisSerializer; @@ -303,6 +305,37 @@ public class RedisCacheConfiguration { return conversionService; } + /** + * Add a {@link Converter} for extracting the {@link String} representation of a cache key if no suitable + * {@link Object#toString()} method is present. + * + * @param cacheKeyConverter + * @throws IllegalStateException if {@link #getConversionService()} does not allow converter registration. + * @since 2.2 + */ + public void addCacheKeyConverter(Converter cacheKeyConverter) { + configureKeyConverters(it -> it.addConverter(cacheKeyConverter)); + } + + /** + * Configure the underlying conversion system used to extract the cache key. + * + * @param registryConsumer never {@literal null}. + * @throws IllegalStateException if {@link #getConversionService()} does not allow converter registration. + * @since 2.2 + */ + public void configureKeyConverters(Consumer registryConsumer) { + + if (!(getConversionService() instanceof ConverterRegistry)) { + throw new IllegalStateException(String.format( + "'%s' returned by getConversionService() does not allow converter registration." // + + " Please make sure to provide a ConversionService that implements ConverterRegistry.", + getConversionService().getClass().getSimpleName())); + } + + registryConsumer.accept((ConverterRegistry) getConversionService()); + } + /** * Registers default cache key converters. The following converters get registered: *