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

Delay allocating Strings for Redis keyspace notifications #1708

Merged
Merged
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 @@ -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 @@ -501,25 +518,24 @@ public RedisSession createSession() {
@Override
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);
handleCreated(loaded, new String(messageChannel));
return;
}

String body = new String(messageBody);
if (!body.startsWith(getExpiredKeyPrefix())) {
byte[] messageBody = message.getBody();

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 +627,7 @@ private String getSessionCreatedChannel(String sessionId) {
}

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

/**
Expand Down