Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--wip-- [skip ci] #18

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -44,7 +45,8 @@
public class RedisCacheResource extends CacheResource<RedisCacheResourceConfiguration> {

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
Expand All @@ -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();
Expand All @@ -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()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
}

Expand All @@ -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() {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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
Expand Down