Skip to content

Commit

Permalink
Delay allocating Strings for message channel and body
Browse files Browse the repository at this point in the history
  • Loading branch information
theigl committed Sep 26, 2020
1 parent 2358014 commit 084d141
Showing 1 changed file with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -36,6 +37,7 @@
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.util.ByteUtils;
import org.springframework.session.DelegatingIndexResolver;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.session.FlushMode;
Expand Down Expand Up @@ -272,10 +274,20 @@ public class RedisIndexedSessionRepository

private String sessionCreatedChannelPrefix;

private byte[] sessionCreatedChannelPrefixBytes;

private String sessionDeletedChannel;

private byte[] sessionDeletedChannelBytes;

private String sessionExpiredChannel;

private byte[] sessionExpiredChannelBytes;

private String expiredKeyPrefix;

private byte[] expiredKeyPrefixBytes;

private final RedisOperations<Object, Object> sessionRedisOperations;

private final RedisSessionExpirationPolicy expirationPolicy;
Expand Down Expand Up @@ -381,8 +393,13 @@ public void setDatabase(int database) {

private void configureSessionChannels() {
this.sessionCreatedChannelPrefix = this.namespace + "event:" + this.database + ":created:";
this.sessionCreatedChannelPrefixBytes = this.sessionCreatedChannelPrefix.getBytes();
this.sessionDeletedChannel = "__keyevent@" + this.database + "__:del";
this.sessionDeletedChannelBytes = this.sessionDeletedChannel.getBytes();
this.sessionExpiredChannel = "__keyevent@" + this.database + "__:expired";
this.sessionExpiredChannelBytes = this.sessionExpiredChannel.getBytes();
this.expiredKeyPrefix = this.namespace + "sessions:expires:";
this.expiredKeyPrefixBytes = this.expiredKeyPrefix.getBytes();
}

/**
Expand Down Expand Up @@ -503,23 +520,21 @@ public void onMessage(Message message, byte[] pattern) {
byte[] messageChannel = message.getChannel();
byte[] messageBody = message.getBody();

String channel = new String(messageChannel);

if (channel.startsWith(this.sessionCreatedChannelPrefix)) {
if (ByteUtils.startsWith(messageChannel, this.sessionCreatedChannelPrefixBytes)) {
// TODO: is this thread safe?
@SuppressWarnings("unchecked")
Map<Object, Object> loaded = (Map<Object, Object>) this.defaultSerializer.deserialize(message.getBody());
handleCreated(loaded, channel);
Map<Object, Object> loaded = (Map<Object, Object>) this.defaultSerializer.deserialize(messageBody);
handleCreated(loaded, new String(messageChannel));
return;
}

String body = new String(messageBody);
if (!body.startsWith(getExpiredKeyPrefix())) {
if (!ByteUtils.startsWith(messageBody, this.expiredKeyPrefixBytes)) {
return;
}

boolean isDeleted = channel.equals(this.sessionDeletedChannel);
if (isDeleted || channel.equals(this.sessionExpiredChannel)) {
boolean isDeleted = Arrays.equals(messageChannel, this.sessionDeletedChannelBytes);
if (isDeleted || Arrays.equals(messageChannel, this.sessionExpiredChannelBytes)) {
String body = new String(messageBody);
int beginIndex = body.lastIndexOf(":") + 1;
int endIndex = body.length();
String sessionId = body.substring(beginIndex, endIndex);
Expand Down Expand Up @@ -611,7 +626,7 @@ private String getSessionCreatedChannel(String sessionId) {
}

private String getExpiredKeyPrefix() {
return this.namespace + "sessions:expires:";
return expiredKeyPrefix;
}

/**
Expand Down

0 comments on commit 084d141

Please sign in to comment.