|
18 | 18 |
|
19 | 19 | import java.time.Duration;
|
20 | 20 | import java.time.Instant;
|
| 21 | +import java.util.Arrays; |
21 | 22 | import java.util.Collections;
|
22 | 23 | import java.util.HashMap;
|
23 | 24 | import java.util.Map;
|
|
36 | 37 | import org.springframework.data.redis.core.RedisOperations;
|
37 | 38 | import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
|
38 | 39 | import org.springframework.data.redis.serializer.RedisSerializer;
|
| 40 | +import org.springframework.data.redis.util.ByteUtils; |
39 | 41 | import org.springframework.session.DelegatingIndexResolver;
|
40 | 42 | import org.springframework.session.FindByIndexNameSessionRepository;
|
41 | 43 | import org.springframework.session.FlushMode;
|
@@ -272,10 +274,20 @@ public class RedisIndexedSessionRepository
|
272 | 274 |
|
273 | 275 | private String sessionCreatedChannelPrefix;
|
274 | 276 |
|
| 277 | + private byte[] sessionCreatedChannelPrefixBytes; |
| 278 | + |
275 | 279 | private String sessionDeletedChannel;
|
276 | 280 |
|
| 281 | + private byte[] sessionDeletedChannelBytes; |
| 282 | + |
277 | 283 | private String sessionExpiredChannel;
|
278 | 284 |
|
| 285 | + private byte[] sessionExpiredChannelBytes; |
| 286 | + |
| 287 | + private String expiredKeyPrefix; |
| 288 | + |
| 289 | + private byte[] expiredKeyPrefixBytes; |
| 290 | + |
279 | 291 | private final RedisOperations<Object, Object> sessionRedisOperations;
|
280 | 292 |
|
281 | 293 | private final RedisSessionExpirationPolicy expirationPolicy;
|
@@ -381,8 +393,13 @@ public void setDatabase(int database) {
|
381 | 393 |
|
382 | 394 | private void configureSessionChannels() {
|
383 | 395 | this.sessionCreatedChannelPrefix = this.namespace + "event:" + this.database + ":created:";
|
| 396 | + this.sessionCreatedChannelPrefixBytes = this.sessionCreatedChannelPrefix.getBytes(); |
384 | 397 | this.sessionDeletedChannel = "__keyevent@" + this.database + "__:del";
|
| 398 | + this.sessionDeletedChannelBytes = this.sessionDeletedChannel.getBytes(); |
385 | 399 | this.sessionExpiredChannel = "__keyevent@" + this.database + "__:expired";
|
| 400 | + this.sessionExpiredChannelBytes = this.sessionExpiredChannel.getBytes(); |
| 401 | + this.expiredKeyPrefix = this.namespace + "sessions:expires:"; |
| 402 | + this.expiredKeyPrefixBytes = this.expiredKeyPrefix.getBytes(); |
386 | 403 | }
|
387 | 404 |
|
388 | 405 | /**
|
@@ -501,25 +518,24 @@ public RedisSession createSession() {
|
501 | 518 | @Override
|
502 | 519 | public void onMessage(Message message, byte[] pattern) {
|
503 | 520 | byte[] messageChannel = message.getChannel();
|
504 |
| - byte[] messageBody = message.getBody(); |
505 |
| - |
506 |
| - String channel = new String(messageChannel); |
507 | 521 |
|
508 |
| - if (channel.startsWith(this.sessionCreatedChannelPrefix)) { |
| 522 | + if (ByteUtils.startsWith(messageChannel, this.sessionCreatedChannelPrefixBytes)) { |
509 | 523 | // TODO: is this thread safe?
|
510 | 524 | @SuppressWarnings("unchecked")
|
511 | 525 | Map<Object, Object> loaded = (Map<Object, Object>) this.defaultSerializer.deserialize(message.getBody());
|
512 |
| - handleCreated(loaded, channel); |
| 526 | + handleCreated(loaded, new String(messageChannel)); |
513 | 527 | return;
|
514 | 528 | }
|
515 | 529 |
|
516 |
| - String body = new String(messageBody); |
517 |
| - if (!body.startsWith(getExpiredKeyPrefix())) { |
| 530 | + byte[] messageBody = message.getBody(); |
| 531 | + |
| 532 | + if (!ByteUtils.startsWith(messageBody, this.expiredKeyPrefixBytes)) { |
518 | 533 | return;
|
519 | 534 | }
|
520 | 535 |
|
521 |
| - boolean isDeleted = channel.equals(this.sessionDeletedChannel); |
522 |
| - if (isDeleted || channel.equals(this.sessionExpiredChannel)) { |
| 536 | + boolean isDeleted = Arrays.equals(messageChannel, this.sessionDeletedChannelBytes); |
| 537 | + if (isDeleted || Arrays.equals(messageChannel, this.sessionExpiredChannelBytes)) { |
| 538 | + String body = new String(messageBody); |
523 | 539 | int beginIndex = body.lastIndexOf(":") + 1;
|
524 | 540 | int endIndex = body.length();
|
525 | 541 | String sessionId = body.substring(beginIndex, endIndex);
|
@@ -611,7 +627,7 @@ private String getSessionCreatedChannel(String sessionId) {
|
611 | 627 | }
|
612 | 628 |
|
613 | 629 | private String getExpiredKeyPrefix() {
|
614 |
| - return this.namespace + "sessions:expires:"; |
| 630 | + return this.expiredKeyPrefix; |
615 | 631 | }
|
616 | 632 |
|
617 | 633 | /**
|
|
0 commit comments