Commit c2a29997 authored by Stephane Nicoll's avatar Stephane Nicoll

Use proper classloader for value serialization

Closes gh-11822
parent 9edcd25c
...@@ -29,9 +29,12 @@ import org.springframework.cache.CacheManager; ...@@ -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 { ...@@ -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 { ...@@ -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());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment