Skip to content

Commit 55502f3

Browse files
vpaviceleftherias
authored andcommitted
Harmonize Redis key namespace configurations
At present, the RedisSessionRepository#setKeyNamespace expects users to provide the trailing colon (:) character that is used as separator between namespace segments. This is unlike RedisIndexedSessionRepository and ReactiveRedisSessionRepository that apply the separator implicitly in their respective #setRedisKeyNamespace methods. This commit harmonizes the Redis key namespaces configurations across all Redis-backed session repository implementations.
1 parent 0e83e3f commit 55502f3

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisSessionRepository.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,13 +42,13 @@
4242
*/
4343
public class RedisSessionRepository implements SessionRepository<RedisSessionRepository.RedisSession> {
4444

45-
private static final String DEFAULT_KEY_NAMESPACE = "spring:session:";
45+
private static final String DEFAULT_KEY_NAMESPACE = "spring:session";
4646

4747
private final RedisOperations<String, Object> sessionRedisOperations;
4848

4949
private Duration defaultMaxInactiveInterval = Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
5050

51-
private String keyNamespace = DEFAULT_KEY_NAMESPACE;
51+
private String keyNamespace = DEFAULT_KEY_NAMESPACE + ":";
5252

5353
private FlushMode flushMode = FlushMode.ON_SAVE;
5454

@@ -76,12 +76,23 @@ public void setDefaultMaxInactiveInterval(Duration defaultMaxInactiveInterval) {
7676
/**
7777
* Set the key namespace.
7878
* @param keyNamespace the key namespace
79+
* @deprecated since 2.4.0 in favor of {@link #setRedisKeyNamespace(String)}
7980
*/
81+
@Deprecated
8082
public void setKeyNamespace(String keyNamespace) {
8183
Assert.hasText(keyNamespace, "keyNamespace must not be empty");
8284
this.keyNamespace = keyNamespace;
8385
}
8486

87+
/**
88+
* Set the Redis key namespace.
89+
* @param namespace the Redis key namespace
90+
*/
91+
public void setRedisKeyNamespace(String namespace) {
92+
Assert.hasText(namespace, "namespace must not be empty");
93+
this.keyNamespace = namespace.trim() + ":";
94+
}
95+
8596
/**
8697
* Set the flush mode.
8798
* @param flushMode the flush mode

spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisSessionRepositoryTests.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -102,18 +102,36 @@ void setKeyNamespace_ValidNamespace_ShouldSetNamespace() {
102102
assertThat(ReflectionTestUtils.getField(this.sessionRepository, "keyNamespace")).isEqualTo("test:");
103103
}
104104

105+
@Test
106+
void setRedisKeyNamespace_ValidNamespace_ShouldSetNamespace() {
107+
this.sessionRepository.setRedisKeyNamespace("test");
108+
assertThat(ReflectionTestUtils.getField(this.sessionRepository, "keyNamespace")).isEqualTo("test:");
109+
}
110+
105111
@Test
106112
void setKeyNamespace_NullNamespace_ShouldThrowException() {
107113
assertThatIllegalArgumentException().isThrownBy(() -> this.sessionRepository.setKeyNamespace(null))
108114
.withMessage("keyNamespace must not be empty");
109115
}
110116

117+
@Test
118+
void setRedisKeyNamespace_NullNamespace_ShouldThrowException() {
119+
assertThatIllegalArgumentException().isThrownBy(() -> this.sessionRepository.setRedisKeyNamespace(null))
120+
.withMessage("namespace must not be empty");
121+
}
122+
111123
@Test
112124
void setKeyNamespace_EmptyNamespace_ShouldThrowException() {
113125
assertThatIllegalArgumentException().isThrownBy(() -> this.sessionRepository.setKeyNamespace(" "))
114126
.withMessage("keyNamespace must not be empty");
115127
}
116128

129+
@Test
130+
void setRedisKeyNamespace_EmptyNamespace_ShouldThrowException() {
131+
assertThatIllegalArgumentException().isThrownBy(() -> this.sessionRepository.setRedisKeyNamespace(" "))
132+
.withMessage("namespace must not be empty");
133+
}
134+
117135
@Test
118136
void setFlushMode_ValidFlushMode_ShouldSetFlushMode() {
119137
this.sessionRepository.setFlushMode(FlushMode.IMMEDIATE);
@@ -185,7 +203,7 @@ void save_NewSession_ShouldSaveSession() {
185203

186204
@Test
187205
void save_NewSessionAndCustomKeyNamespace_ShouldSaveSession() {
188-
this.sessionRepository.setKeyNamespace("custom:");
206+
this.sessionRepository.setRedisKeyNamespace("custom");
189207
RedisSession session = this.sessionRepository.createSession();
190208
this.sessionRepository.save(session);
191209
String key = "custom:sessions:" + session.getId();

0 commit comments

Comments
 (0)