Use proper classloader for value serialization

Closes gh-11822
This commit is contained in:
Stephane Nicoll 2018-01-29 13:55:05 +01:00
parent 9edcd25c25
commit c2a2999799
1 changed files with 10 additions and 3 deletions

View File

@ -29,9 +29,12 @@ import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheManager.RedisCacheManagerBuilder; import org.springframework.data.redis.cache.RedisCacheManager.RedisCacheManagerBuilder;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair;
/** /**
* Redis cache configuration. * Redis cache configuration.
@ -63,9 +66,10 @@ class RedisCacheConfiguration {
} }
@Bean @Bean
public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory,
ResourceLoader resourceLoader) {
RedisCacheManagerBuilder builder = RedisCacheManager RedisCacheManagerBuilder builder = RedisCacheManager
.builder(redisConnectionFactory).cacheDefaults(determineConfiguration()); .builder(redisConnectionFactory).cacheDefaults(determineConfiguration(resourceLoader.getClassLoader()));
List<String> cacheNames = this.cacheProperties.getCacheNames(); List<String> cacheNames = this.cacheProperties.getCacheNames();
if (!cacheNames.isEmpty()) { if (!cacheNames.isEmpty()) {
builder.initialCacheNames(new LinkedHashSet<>(cacheNames)); builder.initialCacheNames(new LinkedHashSet<>(cacheNames));
@ -73,13 +77,16 @@ class RedisCacheConfiguration {
return this.customizerInvoker.customize(builder.build()); return this.customizerInvoker.customize(builder.build());
} }
private org.springframework.data.redis.cache.RedisCacheConfiguration determineConfiguration() { private org.springframework.data.redis.cache.RedisCacheConfiguration determineConfiguration(
ClassLoader classLoader) {
if (this.redisCacheConfiguration != null) { if (this.redisCacheConfiguration != null) {
return this.redisCacheConfiguration; return this.redisCacheConfiguration;
} }
Redis redisProperties = this.cacheProperties.getRedis(); Redis redisProperties = this.cacheProperties.getRedis();
org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration
.defaultCacheConfig(); .defaultCacheConfig();
config = config.serializeValuesWith(SerializationPair.fromSerializer(
new JdkSerializationRedisSerializer(classLoader)));
if (redisProperties.getTimeToLive() != null) { if (redisProperties.getTimeToLive() != null) {
config = config.entryTtl(redisProperties.getTimeToLive()); config = config.entryTtl(redisProperties.getTimeToLive());
} }