From d209a105140d3e571ac3e1a1c83102304bd0d571 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Mon, 15 Feb 2016 08:32:31 -0600 Subject: [PATCH] Clean up principal index immediately on delete Previously index was cleaned up only in the Redis Keyspace Notification. This meant there was a delay in removing the index. This does not cause a bug since we verify sessions exist and are not expired when we look up sessions by index. However, it could be improved. This commit ensures that the index is cleaned up immediately on session deletion. Fixes gh-367 --- .../redis/RedisOperationsSessionRepository.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/spring-session/src/main/java/org/springframework/session/data/redis/RedisOperationsSessionRepository.java b/spring-session/src/main/java/org/springframework/session/data/redis/RedisOperationsSessionRepository.java index e1b1b26..0f3ad24 100644 --- a/spring-session/src/main/java/org/springframework/session/data/redis/RedisOperationsSessionRepository.java +++ b/spring-session/src/main/java/org/springframework/session/data/redis/RedisOperationsSessionRepository.java @@ -450,6 +450,7 @@ public class RedisOperationsSessionRepository implements FindByIndexNameSessionR return; } + cleanupPrincipalIndex(session); expirationPolicy.onDelete(session); String expireKey = getExpiredKey(session.getId()); @@ -502,10 +503,7 @@ public class RedisOperationsSessionRepository implements FindByIndexNameSessionR logger.debug("Publishing SessionDestroyedEvent for session " + sessionId); } - String principal = PRINCIPAL_NAME_RESOLVER.resolvePrincipal(session); - if(principal != null) { - sessionRedisOperations.boundSetOps(getPrincipalKey(principal)).remove(sessionId); - } + cleanupPrincipalIndex(session); if(isDeleted) { handleDeleted(sessionId, session); @@ -517,6 +515,17 @@ public class RedisOperationsSessionRepository implements FindByIndexNameSessionR } } + private void cleanupPrincipalIndex(RedisSession session) { + if(session == null) { + return; + } + String sessionId = session.getId(); + String principal = PRINCIPAL_NAME_RESOLVER.resolvePrincipal(session); + if(principal != null) { + sessionRedisOperations.boundSetOps(getPrincipalKey(principal)).remove(sessionId); + } + } + public void handleCreated(Map loaded, String channel) { String id = channel.substring(channel.lastIndexOf(":") + 1); ExpiringSession session = loadSession(id, loaded);