diff --git a/src/main/java/io/gravitee/resource/cache/redis/RedisCacheResource.java b/src/main/java/io/gravitee/resource/cache/redis/RedisCacheResource.java index 479cd45..dd42b4c 100644 --- a/src/main/java/io/gravitee/resource/cache/redis/RedisCacheResource.java +++ b/src/main/java/io/gravitee/resource/cache/redis/RedisCacheResource.java @@ -34,7 +34,8 @@ import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration; -import org.springframework.data.redis.serializer.RedisSerializationContext; +import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; +import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; /** @@ -44,7 +45,8 @@ public class RedisCacheResource extends CacheResource { private final Logger logger = LoggerFactory.getLogger(RedisCacheResource.class); - private final StringRedisSerializer stringSerializer = new StringRedisSerializer(); + private final RedisSerializer keySerializer = new StringRedisSerializer(); + private final RedisSerializer valueSerializer = new JdkSerializationRedisSerializer(); private RedisCacheManager redisCacheManager; @Override @@ -55,8 +57,6 @@ protected void doStart() throws Exception { try { RedisCacheConfiguration conf = RedisCacheConfiguration .defaultCacheConfig() - .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringSerializer)) - .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(stringSerializer)) .entryTtl(Duration.ofSeconds(configuration().getTimeToLiveSeconds())); this.redisCacheManager = RedisCacheManager.builder(getConnectionFactory()).cacheDefaults(conf).build(); @@ -75,7 +75,8 @@ public Cache getCache(ExecutionContext executionContext) { return new RedisDelegate( this.redisCacheManager.getCache("gravitee:"), executionContext, - stringSerializer, + keySerializer, + valueSerializer, (int) configuration().getTimeToLiveSeconds(), configuration().isReleaseCache() ); diff --git a/src/main/java/io/gravitee/resource/cache/redis/RedisDelegate.java b/src/main/java/io/gravitee/resource/cache/redis/RedisDelegate.java index 962733b..9c57228 100644 --- a/src/main/java/io/gravitee/resource/cache/redis/RedisDelegate.java +++ b/src/main/java/io/gravitee/resource/cache/redis/RedisDelegate.java @@ -22,6 +22,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.redis.cache.RedisCacheWriter; +import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer; /** @@ -34,21 +35,24 @@ public class RedisDelegate implements Cache { private final org.springframework.cache.Cache cache; private final int timeToLiveSeconds; - private final RedisSerializer serializer; + private final RedisSerializer keySerializer; + private final RedisSerializer valueSerializer; private boolean releaseCache; private ExecutionContext executionContext; public RedisDelegate( org.springframework.cache.Cache cache, ExecutionContext executionContext, - RedisSerializer serializer, + RedisSerializer keySerializer, + RedisSerializer valueSerializer, int timeToLiveSeconds, boolean releaseCache ) { this.cache = cache; this.executionContext = executionContext; this.timeToLiveSeconds = timeToLiveSeconds; - this.serializer = serializer; + this.keySerializer = keySerializer; + this.valueSerializer = valueSerializer; this.releaseCache = releaseCache; } @@ -69,7 +73,7 @@ public Element get(Object key) { RedisCacheWriter redisCacheWriter = (RedisCacheWriter) this.getNativeCache(); byte[] bytes = redisCacheWriter.get(this.getName(), buildKey(key)); if (bytes != null) { - Object value = this.serializer.deserialize(bytes); + Object value = valueSerializer.deserialize(bytes); return new Element() { @Override public Object key() { @@ -100,7 +104,7 @@ public void put(Element element) { } RedisCacheWriter redisCacheWriter = (RedisCacheWriter) this.getNativeCache(); - redisCacheWriter.put(getName(), buildKey(element.key()), this.serializer.serialize(element.value()), Duration.ofSeconds(ttl)); + redisCacheWriter.put(getName(), buildKey(element.key()), valueSerializer.serialize(element.value()), Duration.ofSeconds(ttl)); } catch (Throwable e) { logger.error("Cannot put element in cache", e); } @@ -111,7 +115,7 @@ private byte[] buildKey(Object key) { if (this.releaseCache) { allKey += ":" + this.executionContext.getAttribute(ExecutionContext.ATTR_API_DEPLOYED_AT); } - return this.serializer.serialize(allKey); + return this.keySerializer.serialize(allKey); } @Override