DATAREDIS-1032 - Improve cache key converter registration.
Original pull request: #475.
This commit is contained in:
committed by
Mark Paluch
parent
17f05c40ad
commit
79ba1ccc9b
@@ -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"));
|
||||
}
|
||||
|
||||
|
||||
@@ -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<?, String> 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<ConverterRegistry> 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:
|
||||
* <ul>
|
||||
|
||||
@@ -19,7 +19,9 @@ import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.instrument.classloading.ShadowingClassLoader;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link RedisCacheConfiguration}.
|
||||
@@ -43,4 +45,26 @@ public class RedisCacheConfigurationUnitTests {
|
||||
|
||||
assertThat(usedClassLoader).isSameAs(classLoader);
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-1032
|
||||
public void shouldAllowConverterRegistration() {
|
||||
|
||||
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
|
||||
config.configureKeyConverters(registry -> registry.addConverter(new DomainTypeConverter()));
|
||||
|
||||
assertThat(config.getConversionService().canConvert(DomainType.class, String.class)).isTrue();
|
||||
}
|
||||
|
||||
private static class DomainType {
|
||||
|
||||
}
|
||||
|
||||
static class DomainTypeConverter implements Converter<DomainType, String> {
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String convert(DomainType source) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user