Skip to content

Commit 9659f1f

Browse files
zk1991-githubeleftherias
authored andcommitted
Modify to support negative numbers
1 parent 919a2a5 commit 9659f1f

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

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

Lines changed: 10 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-2021 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.
@@ -37,6 +37,7 @@
3737
* {@link ReactiveRedisOperations}.
3838
*
3939
* @author Vedran Pavic
40+
* @author Kai Zhao
4041
* @since 2.2.0
4142
*/
4243
public class ReactiveRedisSessionRepository
@@ -274,8 +275,14 @@ private Mono<Void> saveDelta() {
274275
String sessionKey = getSessionKey(getId());
275276
Mono<Boolean> update = ReactiveRedisSessionRepository.this.sessionRedisOperations.opsForHash()
276277
.putAll(sessionKey, new HashMap<>(this.delta));
277-
Mono<Boolean> setTtl = ReactiveRedisSessionRepository.this.sessionRedisOperations.expire(sessionKey,
278-
getMaxInactiveInterval());
278+
Mono<Boolean> setTtl;
279+
if (getMaxInactiveInterval().getSeconds() >= 0) {
280+
setTtl = ReactiveRedisSessionRepository.this.sessionRedisOperations.expire(sessionKey,
281+
getMaxInactiveInterval());
282+
}
283+
else {
284+
setTtl = ReactiveRedisSessionRepository.this.sessionRedisOperations.persist(sessionKey);
285+
}
279286

280287
return update.and(setTtl).and((s) -> {
281288
this.delta.clear();

spring-session-data-redis/src/main/java/org/springframework/session/data/redis/config/annotation/web/server/EnableRedisWebSession.java

Lines changed: 3 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-2021 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.
@@ -56,6 +56,7 @@
5656
* More advanced configurations can extend {@link RedisWebSessionConfiguration} instead.
5757
*
5858
* @author Vedran Pavic
59+
* @author Kai Zhao
5960
* @since 2.0.0
6061
* @see EnableSpringWebSession
6162
*/
@@ -68,7 +69,7 @@
6869

6970
/**
7071
* The session timeout in seconds. By default, it is set to 1800 seconds (30 minutes).
71-
* This should be a non-negative integer.
72+
* A negative number means permanently valid.
7273
* @return the seconds a session can be inactive before expiring
7374
*/
7475
int maxInactiveIntervalInSeconds() default MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS;

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

Lines changed: 29 additions & 1 deletion
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-2021 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.
@@ -49,6 +49,7 @@
4949
* Tests for {@link ReactiveRedisSessionRepository}.
5050
*
5151
* @author Vedran Pavic
52+
* @author Kai Zhao
5253
*/
5354
class ReactiveRedisSessionRepositoryTests {
5455

@@ -150,6 +151,33 @@ void saveNewSession() {
150151
.isEqualTo(newSession.getLastAccessedTime().toEpochMilli());
151152
}
152153

154+
@Test
155+
void saveCustomNegativeMaxInactiveIntervalNewSession() {
156+
given(this.redisOperations.opsForHash()).willReturn(this.hashOperations);
157+
given(this.hashOperations.putAll(anyString(), any())).willReturn(Mono.just(true));
158+
given(this.redisOperations.persist(anyString())).willReturn(Mono.just(true));
159+
160+
MapSession mapSession = new MapSession();
161+
mapSession.setMaxInactiveInterval(Duration.ofSeconds(-1));
162+
RedisSession newSession = this.repository.new RedisSession(mapSession, true);
163+
StepVerifier.create(this.repository.save(newSession)).verifyComplete();
164+
165+
verify(this.redisOperations).opsForHash();
166+
verify(this.hashOperations).putAll(anyString(), this.delta.capture());
167+
verify(this.redisOperations).persist(anyString());
168+
verifyNoMoreInteractions(this.redisOperations);
169+
verifyNoMoreInteractions(this.hashOperations);
170+
171+
Map<String, Object> delta = this.delta.getAllValues().get(0);
172+
assertThat(delta.size()).isEqualTo(3);
173+
assertThat(delta.get(RedisSessionMapper.CREATION_TIME_KEY))
174+
.isEqualTo(newSession.getCreationTime().toEpochMilli());
175+
assertThat(delta.get(RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY))
176+
.isEqualTo((int) newSession.getMaxInactiveInterval().getSeconds());
177+
assertThat(delta.get(RedisSessionMapper.LAST_ACCESSED_TIME_KEY))
178+
.isEqualTo(newSession.getLastAccessedTime().toEpochMilli());
179+
}
180+
153181
@Test
154182
void saveSessionNothingChanged() {
155183
given(this.redisOperations.hasKey(anyString())).willReturn(Mono.just(true));

0 commit comments

Comments
 (0)