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: *