Remove attribute key and value from Redis

Closes gh-1331
This commit is contained in:
Eleftheria Stein-Kousathana
2020-07-24 12:55:26 +02:00
parent b219806d8e
commit 0cd0bfb32f
2 changed files with 24 additions and 1 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.session.data.redis;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
@@ -134,6 +135,22 @@ class RedisIndexedSessionRepositoryITests extends AbstractRedisITests {
.isEqualTo(expectedAttributeValue);
}
@Test
void removeAttributeRemovedAttributeKey() {
RedisSession toSave = this.repository.createSession();
toSave.setAttribute("a", "b");
this.repository.save(toSave);
toSave.removeAttribute("a");
this.repository.save(toSave);
String id = toSave.getId();
String key = "RedisIndexedSessionRepositoryITests:sessions:" + id;
Set<Map.Entry<Object, Object>> entries = this.redis.boundHashOps(key).entries().entrySet();
assertThat(entries).extracting(Map.Entry::getKey).doesNotContain("sessionAttr:a");
}
@Test
void putAllOnSingleAttrDoesNotRemoveOld() {
RedisSession toSave = this.repository.createSession();

View File

@@ -792,7 +792,8 @@ public class RedisIndexedSessionRepository
return;
}
String sessionId = getId();
getSessionBoundHashOperations(sessionId).putAll(this.delta);
BoundHashOperations<Object, Object, Object> boundHashOperations = getSessionBoundHashOperations(sessionId);
boundHashOperations.putAll(this.delta);
String principalSessionKey = getSessionAttrNameKey(
FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME);
String securityPrincipalSessionKey = getSessionAttrNameKey(SPRING_SECURITY_CONTEXT);
@@ -811,6 +812,11 @@ public class RedisIndexedSessionRepository
.add(sessionId);
}
}
for (final Map.Entry<String, Object> attribute : this.delta.entrySet()) {
if (attribute.getValue() == null) {
boundHashOperations.delete(attribute.getKey());
}
}
this.delta = new HashMap<>(this.delta.size());