From 9659f1f571e5a740f0a92bf8db3e2158e00d149b Mon Sep 17 00:00:00 2001 From: zhaokai <506683059@qq.com> Date: Wed, 22 Sep 2021 16:19:17 +0800 Subject: [PATCH] Modify to support negative numbers --- .../redis/ReactiveRedisSessionRepository.java | 13 ++++++-- .../web/server/EnableRedisWebSession.java | 5 ++-- .../ReactiveRedisSessionRepositoryTests.java | 30 ++++++++++++++++++- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/ReactiveRedisSessionRepository.java b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/ReactiveRedisSessionRepository.java index 81e80824..a987359e 100644 --- a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/ReactiveRedisSessionRepository.java +++ b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/ReactiveRedisSessionRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,6 +37,7 @@ import org.springframework.util.Assert; * {@link ReactiveRedisOperations}. * * @author Vedran Pavic + * @author Kai Zhao * @since 2.2.0 */ public class ReactiveRedisSessionRepository @@ -274,8 +275,14 @@ public class ReactiveRedisSessionRepository String sessionKey = getSessionKey(getId()); Mono update = ReactiveRedisSessionRepository.this.sessionRedisOperations.opsForHash() .putAll(sessionKey, new HashMap<>(this.delta)); - Mono setTtl = ReactiveRedisSessionRepository.this.sessionRedisOperations.expire(sessionKey, - getMaxInactiveInterval()); + Mono setTtl; + if (getMaxInactiveInterval().getSeconds() >= 0) { + setTtl = ReactiveRedisSessionRepository.this.sessionRedisOperations.expire(sessionKey, + getMaxInactiveInterval()); + } + else { + setTtl = ReactiveRedisSessionRepository.this.sessionRedisOperations.persist(sessionKey); + } return update.and(setTtl).and((s) -> { this.delta.clear(); diff --git a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/config/annotation/web/server/EnableRedisWebSession.java b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/config/annotation/web/server/EnableRedisWebSession.java index 87623b83..d5add08a 100644 --- a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/config/annotation/web/server/EnableRedisWebSession.java +++ b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/config/annotation/web/server/EnableRedisWebSession.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -56,6 +56,7 @@ import org.springframework.web.server.session.WebSessionManager; * More advanced configurations can extend {@link RedisWebSessionConfiguration} instead. * * @author Vedran Pavic + * @author Kai Zhao * @since 2.0.0 * @see EnableSpringWebSession */ @@ -68,7 +69,7 @@ public @interface EnableRedisWebSession { /** * The session timeout in seconds. By default, it is set to 1800 seconds (30 minutes). - * This should be a non-negative integer. + * A negative number means permanently valid. * @return the seconds a session can be inactive before expiring */ int maxInactiveIntervalInSeconds() default MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS; diff --git a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/ReactiveRedisSessionRepositoryTests.java b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/ReactiveRedisSessionRepositoryTests.java index b860479a..efdc81f9 100644 --- a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/ReactiveRedisSessionRepositoryTests.java +++ b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/ReactiveRedisSessionRepositoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -49,6 +49,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; * Tests for {@link ReactiveRedisSessionRepository}. * * @author Vedran Pavic + * @author Kai Zhao */ class ReactiveRedisSessionRepositoryTests { @@ -150,6 +151,33 @@ class ReactiveRedisSessionRepositoryTests { .isEqualTo(newSession.getLastAccessedTime().toEpochMilli()); } + @Test + void saveCustomNegativeMaxInactiveIntervalNewSession() { + given(this.redisOperations.opsForHash()).willReturn(this.hashOperations); + given(this.hashOperations.putAll(anyString(), any())).willReturn(Mono.just(true)); + given(this.redisOperations.persist(anyString())).willReturn(Mono.just(true)); + + MapSession mapSession = new MapSession(); + mapSession.setMaxInactiveInterval(Duration.ofSeconds(-1)); + RedisSession newSession = this.repository.new RedisSession(mapSession, true); + StepVerifier.create(this.repository.save(newSession)).verifyComplete(); + + verify(this.redisOperations).opsForHash(); + verify(this.hashOperations).putAll(anyString(), this.delta.capture()); + verify(this.redisOperations).persist(anyString()); + verifyNoMoreInteractions(this.redisOperations); + verifyNoMoreInteractions(this.hashOperations); + + Map delta = this.delta.getAllValues().get(0); + assertThat(delta.size()).isEqualTo(3); + assertThat(delta.get(RedisSessionMapper.CREATION_TIME_KEY)) + .isEqualTo(newSession.getCreationTime().toEpochMilli()); + assertThat(delta.get(RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY)) + .isEqualTo((int) newSession.getMaxInactiveInterval().getSeconds()); + assertThat(delta.get(RedisSessionMapper.LAST_ACCESSED_TIME_KEY)) + .isEqualTo(newSession.getLastAccessedTime().toEpochMilli()); + } + @Test void saveSessionNothingChanged() { given(this.redisOperations.hasKey(anyString())).willReturn(Mono.just(true));