Make Redis namespace fully configurable

See gh-919
This commit is contained in:
Luís Duarte
2017-11-03 14:22:33 +00:00
committed by Vedran Pavic
parent 4c9fbd5b6b
commit 5f23a41674
6 changed files with 64 additions and 10 deletions

View File

@@ -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

View File

@@ -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) {

View File

@@ -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