From 44a6da43fa0f212d7d28947bf26a240460b7eaf8 Mon Sep 17 00:00:00 2001 From: Stephen Kinser Date: Mon, 8 Apr 2024 17:05:58 -0600 Subject: [PATCH] Redis reads session once not twice during cleanup I noticed that the session gets read twice at https://github.com/spring-projects/spring-session/blob/4dc81ec10d0e2870bb6f208df924b2ab933a835e/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/ReactiveRedisIndexedSessionRepository.java#L441-L442. This alters the code to only read it once. --- .../redis/ReactiveRedisIndexedSessionRepository.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/ReactiveRedisIndexedSessionRepository.java b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/ReactiveRedisIndexedSessionRepository.java index 4d4c4c23..0b3bdbb0 100644 --- a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/ReactiveRedisIndexedSessionRepository.java +++ b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/ReactiveRedisIndexedSessionRepository.java @@ -385,13 +385,17 @@ public class ReactiveRedisIndexedSessionRepository @Override public Mono deleteById(String id) { + return internalDeleteById(id).then(); + } + + public Mono internalDeleteById(String id) { // @formatter:off return getSession(id, true) .flatMap((session) -> this.sessionRedisOperations.delete(getExpiredKey(session.getId())) .thenReturn(session)) .flatMap((session) -> this.sessionRedisOperations.delete(getSessionKey(session.getId())).thenReturn(session)) .flatMap((session) -> this.indexer.delete(session.getId()).thenReturn(session)) - .flatMap((session) -> this.expirationStore.remove(session.getId())); + .flatMap((session) -> this.expirationStore.remove(session.getId()).thenReturn(session)); // @formatter:on } @@ -438,8 +442,7 @@ public class ReactiveRedisIndexedSessionRepository int sessionIdBeginIndex = key.lastIndexOf(":") + 1; return key.substring(sessionIdBeginIndex); }) - .flatMap((sessionId) -> getSession(sessionId, true)) - .flatMap((session) -> this.deleteById(session.getId()).thenReturn(session)) + .flatMap(this::internalDeleteById) .map((session) -> { if (message.getChannel().equals(this.sessionDeletedChannel)) { return new SessionDeletedEvent(this, session);