Make Redis namespace fully configurable
See gh-919
This commit is contained in:
committed by
Vedran Pavic
parent
4c9fbd5b6b
commit
5f23a41674
@@ -89,7 +89,7 @@ public class RedisOperationsSessionRepositoryITests extends AbstractRedisITests
|
||||
public void saves() throws InterruptedException {
|
||||
String username = "saves-" + System.currentTimeMillis();
|
||||
|
||||
String usernameSessionKey = "spring:session:RedisOperationsSessionRepositoryITests:index:"
|
||||
String usernameSessionKey = "RedisOperationsSessionRepositoryITests:index:"
|
||||
+ INDEX_NAME + ":" + username;
|
||||
|
||||
RedisSession toSave = this.repository.createSession();
|
||||
@@ -188,7 +188,7 @@ public class RedisOperationsSessionRepositoryITests extends AbstractRedisITests
|
||||
|
||||
this.repository.save(toSave);
|
||||
|
||||
String body = "spring:session:RedisOperationsSessionRepositoryITests:sessions:expires:"
|
||||
String body = "RedisOperationsSessionRepositoryITests:sessions:expires:"
|
||||
+ toSave.getId();
|
||||
String channel = ":expired";
|
||||
DefaultMessage message = new DefaultMessage(channel.getBytes("UTF-8"),
|
||||
@@ -356,7 +356,7 @@ public class RedisOperationsSessionRepositoryITests extends AbstractRedisITests
|
||||
|
||||
this.repository.save(toSave);
|
||||
|
||||
String body = "spring:session:RedisOperationsSessionRepositoryITests:sessions:expires:"
|
||||
String body = "RedisOperationsSessionRepositoryITests:sessions:expires:"
|
||||
+ toSave.getId();
|
||||
String channel = ":expired";
|
||||
DefaultMessage message = new DefaultMessage(channel.getBytes("UTF-8"),
|
||||
|
||||
@@ -93,7 +93,7 @@ public class ReactiveRedisOperationsSessionRepository implements
|
||||
|
||||
public void setRedisKeyNamespace(String namespace) {
|
||||
Assert.hasText(namespace, "namespace cannot be null or empty");
|
||||
this.keyPrefix = DEFAULT_SPRING_SESSION_REDIS_PREFIX + namespace.trim() + ":";
|
||||
this.keyPrefix = namespace.trim() + ":";
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -588,7 +588,8 @@ public class RedisOperationsSessionRepository implements
|
||||
}
|
||||
|
||||
public void setRedisKeyNamespace(String namespace) {
|
||||
this.keyPrefix = DEFAULT_SPRING_SESSION_REDIS_PREFIX + namespace + ":";
|
||||
Assert.hasText(namespace, "namespace cannot be null or empty");
|
||||
this.keyPrefix = namespace.trim() + ":";
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -85,9 +85,7 @@ public class ReactiveRedisOperationsSessionRepositoryTests {
|
||||
public void customRedisKeyNamespace() {
|
||||
this.repository.setRedisKeyNamespace("test");
|
||||
|
||||
assertThat(ReflectionTestUtils.getField(this.repository, "keyPrefix")).isEqualTo(
|
||||
ReactiveRedisOperationsSessionRepository.DEFAULT_SPRING_SESSION_REDIS_PREFIX
|
||||
+ "test:");
|
||||
assertThat(ReflectionTestUtils.getField(this.repository, "keyPrefix")).isEqualTo("test:");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.session.data.redis;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
@@ -709,8 +710,62 @@ public class RedisOperationsSessionRepositoryTests {
|
||||
this.redisRepository.setRedisFlushMode(null);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDefaultRedisNamespace() {
|
||||
RedisSession session = this.redisRepository.new RedisSession(new MapSession());
|
||||
session.setMaxInactiveInterval(Duration.ZERO);
|
||||
given(this.redisOperations.boundHashOps(anyString()))
|
||||
.willReturn(this.boundHashOperations);
|
||||
given(this.redisOperations.boundSetOps(anyString()))
|
||||
.willReturn(this.boundSetOperations);
|
||||
|
||||
this.redisRepository.save(session);
|
||||
|
||||
String id = session.getId();
|
||||
verify(this.redisOperations, atLeastOnce())
|
||||
.delete(getKey("expires:" + id));
|
||||
verify(this.redisOperations, never()).boundValueOps(getKey("expires:" + id));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRedisNamespaceChange() {
|
||||
String namespace = "foo:bar";
|
||||
this.redisRepository.setRedisKeyNamespace(namespace);
|
||||
RedisSession session = this.redisRepository.new RedisSession(new MapSession());
|
||||
session.setMaxInactiveInterval(Duration.ZERO);
|
||||
given(this.redisOperations.boundHashOps(anyString()))
|
||||
.willReturn(this.boundHashOperations);
|
||||
given(this.redisOperations.boundSetOps(anyString()))
|
||||
.willReturn(this.boundSetOperations);
|
||||
|
||||
this.redisRepository.save(session);
|
||||
|
||||
String id = session.getId();
|
||||
verify(this.redisOperations, atLeastOnce())
|
||||
.delete(getKeyWithinNamespace(namespace, "expires:" + id));
|
||||
verify(this.redisOperations, never()).boundValueOps(getKeyWithinNamespace(namespace, "expires:" + id));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testLaunchExceptionOnNullNamespace() {
|
||||
this.redisRepository.setRedisKeyNamespace(null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testLaunchExceptionOnEmptyNamespace() {
|
||||
this.redisRepository.setRedisKeyNamespace("");
|
||||
}
|
||||
|
||||
private String getKey(String id) {
|
||||
return "spring:session:sessions:" + id;
|
||||
return getKeyWithinNamespace("spring:session", id);
|
||||
}
|
||||
|
||||
private String getKeyWithinNamespace(String prefix, String id) {
|
||||
if (prefix.endsWith(":")) {
|
||||
prefix = prefix.substring(0, prefix.length());
|
||||
}
|
||||
return MessageFormat.format("{0}:sessions:{1}", prefix, id);
|
||||
}
|
||||
|
||||
private Map map(Object... objects) {
|
||||
|
||||
@@ -82,7 +82,7 @@ public class RedisWebSessionConfigurationTests {
|
||||
.getBean(ReactiveRedisOperationsSessionRepository.class);
|
||||
assertThat(repository).isNotNull();
|
||||
assertThat(ReflectionTestUtils.getField(repository, "keyPrefix"))
|
||||
.isEqualTo("spring:session:" + REDIS_NAMESPACE + ":");
|
||||
.isEqualTo(REDIS_NAMESPACE + ":");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user