Prevent expiration on RedisSession interval < 0

Since a negative maxInactiveInterval is supposed to disable
expiration, if it is negative, use persist on the session's
spring:session:session and spring:session:expires keys to
prevent the expiration of the RedisSession.

Issue gh-629
This commit is contained in:
Joe Atkins
2016-09-14 22:19:10 -05:00
committed by Rob Winch
parent 17e397212d
commit 1df1a76069
2 changed files with 42 additions and 2 deletions

View File

@@ -80,15 +80,23 @@ final class RedisSessionExpirationPolicy {
}
}
long sessionExpireInSeconds = session.getMaxInactiveIntervalInSeconds();
String sessionKey = getSessionKey(keyToExpire);
if (sessionExpireInSeconds < 0) {
this.redis.boundValueOps(sessionKey).append("");
this.redis.boundValueOps(sessionKey).persist();
this.redis.boundHashOps(getSessionKey(session.getId())).persist();
return;
}
String expireKey = getExpirationKey(toExpire);
BoundSetOperations<Object, Object> expireOperations = this.redis
.boundSetOps(expireKey);
expireOperations.add(keyToExpire);
long sessionExpireInSeconds = session.getMaxInactiveIntervalInSeconds();
long fiveMinutesAfterExpires = sessionExpireInSeconds
+ TimeUnit.MINUTES.toSeconds(5);
String sessionKey = getSessionKey(keyToExpire);
expireOperations.expire(fiveMinutesAfterExpires, TimeUnit.SECONDS);
if (sessionExpireInSeconds == 0) {

View File

@@ -137,4 +137,36 @@ public class RedisSessionExpirationPolicyTests {
verify(this.hashOperations).expire(this.session.getMaxInactiveIntervalInSeconds()
+ TimeUnit.MINUTES.toSeconds(5), TimeUnit.SECONDS);
}
@Test
public void onExpirationUpdatedDeleteOnZero() throws Exception {
String sessionKey = this.policy.getSessionKey("expires:" + this.session.getId());
long originalExpirationTimeInMs = ONE_MINUTE_AGO;
this.session.setMaxInactiveIntervalInSeconds(0);
this.policy.onExpirationUpdated(originalExpirationTimeInMs, this.session);
// verify the original is removed
verify(this.setOperations).remove("expires:" + this.session.getId());
verify(this.setOperations).add("expires:" + this.session.getId());
verify(this.sessionRedisOperations).delete(sessionKey);
verify(this.setOperations).expire(this.session.getMaxInactiveIntervalInSeconds()
+ TimeUnit.MINUTES.toSeconds(5), TimeUnit.SECONDS);
}
@Test
public void onExpirationUpdatedPersistOnNegativeExpiration() throws Exception {
long originalExpirationTimeInMs = ONE_MINUTE_AGO;
this.session.setMaxInactiveIntervalInSeconds(-1);
this.policy.onExpirationUpdated(originalExpirationTimeInMs, this.session);
verify(this.setOperations).remove("expires:" + this.session.getId());
verify(this.valueOperations).append("");
verify(this.valueOperations).persist();
verify(this.hashOperations).persist();
}
}