Include Time Unit for ExpiringSession maxinactiveInterval
Fixes gh-82
This commit is contained in:
@@ -76,7 +76,7 @@ public class IndexDocTests {
|
||||
public void demo() {
|
||||
S toSave = repository.createSession(); // <2>
|
||||
// ...
|
||||
toSave.setMaxInactiveInterval(30); // <3>
|
||||
toSave.setMaxInactiveIntervalInSeconds(30); // <3>
|
||||
|
||||
repository.save(toSave); // <4>
|
||||
|
||||
|
||||
@@ -27,14 +27,14 @@ public interface ExpiringSession extends Session {
|
||||
*
|
||||
* @param interval the number of seconds that the {@link Session} should be kept alive between client requests.
|
||||
*/
|
||||
void setMaxInactiveInterval(int interval);
|
||||
void setMaxInactiveIntervalInSeconds(int interval);
|
||||
|
||||
/**
|
||||
* Gets the maximum inactive interval in seconds between requests before this session will be invalidated. A negative time indicates that the session will never timeout.
|
||||
*
|
||||
* @return the maximum inactive interval in seconds between requests before this session will be invalidated. A negative time indicates that the session will never timeout.
|
||||
*/
|
||||
int getMaxInactiveInterval();
|
||||
int getMaxInactiveIntervalInSeconds();
|
||||
|
||||
/**
|
||||
* Returns true if the session is expired.
|
||||
|
||||
@@ -42,7 +42,7 @@ import java.util.concurrent.TimeUnit;
|
||||
*/
|
||||
public final class MapSession implements ExpiringSession, Serializable {
|
||||
/**
|
||||
* Default {@link #setMaxInactiveInterval(int)} (30 minutes)
|
||||
* Default {@link #setMaxInactiveIntervalInSeconds(int)} (30 minutes)
|
||||
*/
|
||||
public static final int DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS = 1800;
|
||||
|
||||
@@ -79,7 +79,7 @@ public final class MapSession implements ExpiringSession, Serializable {
|
||||
}
|
||||
this.lastAccessedTime = session.getLastAccessedTime();
|
||||
this.creationTime = session.getCreationTime();
|
||||
this.maxInactiveInterval = session.getMaxInactiveInterval();
|
||||
this.maxInactiveInterval = session.getMaxInactiveIntervalInSeconds();
|
||||
}
|
||||
|
||||
public void setLastAccessedTime(long lastAccessedTime) {
|
||||
@@ -98,11 +98,11 @@ public final class MapSession implements ExpiringSession, Serializable {
|
||||
return lastAccessedTime;
|
||||
}
|
||||
|
||||
public void setMaxInactiveInterval(int interval) {
|
||||
public void setMaxInactiveIntervalInSeconds(int interval) {
|
||||
this.maxInactiveInterval = interval;
|
||||
}
|
||||
|
||||
public int getMaxInactiveInterval() {
|
||||
public int getMaxInactiveIntervalInSeconds() {
|
||||
return maxInactiveInterval;
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
*/
|
||||
public class MapSessionRepository implements SessionRepository<ExpiringSession> {
|
||||
/**
|
||||
* If non-null, this value is used to override {@link ExpiringSession#setMaxInactiveInterval(int)}.
|
||||
* If non-null, this value is used to override {@link ExpiringSession#setMaxInactiveIntervalInSeconds(int)}.
|
||||
*/
|
||||
private Integer defaultMaxInactiveInterval;
|
||||
|
||||
@@ -60,7 +60,7 @@ public class MapSessionRepository implements SessionRepository<ExpiringSession>
|
||||
}
|
||||
|
||||
/**
|
||||
* If non-null, this value is used to override {@link ExpiringSession#setMaxInactiveInterval(int)}.
|
||||
* If non-null, this value is used to override {@link ExpiringSession#setMaxInactiveIntervalInSeconds(int)}.
|
||||
* @param defaultMaxInactiveInterval the number of seconds that the {@link Session} should be kept alive between client requests.
|
||||
*/
|
||||
public void setDefaultMaxInactiveInterval(int defaultMaxInactiveInterval) {
|
||||
@@ -92,7 +92,7 @@ public class MapSessionRepository implements SessionRepository<ExpiringSession>
|
||||
public ExpiringSession createSession() {
|
||||
ExpiringSession result = new MapSession();
|
||||
if(defaultMaxInactiveInterval != null) {
|
||||
result.setMaxInactiveInterval(defaultMaxInactiveInterval);
|
||||
result.setMaxInactiveIntervalInSeconds(defaultMaxInactiveInterval);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ import org.springframework.util.Assert;
|
||||
* <p>
|
||||
* An expiration is associated to each session using the <a
|
||||
* href="http://redis.io/commands/expire">EXPIRE command</a> based upon the
|
||||
* {@link org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession#getMaxInactiveInterval()}
|
||||
* {@link org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession#getMaxInactiveIntervalInSeconds()}
|
||||
* . For example:
|
||||
* </p>
|
||||
*
|
||||
@@ -123,7 +123,7 @@ public class RedisOperationsSessionRepository implements SessionRepository<Redis
|
||||
static final String CREATION_TIME_ATTR = "creationTime";
|
||||
|
||||
/**
|
||||
* The key in the Hash representing {@link org.springframework.session.ExpiringSession#getMaxInactiveInterval()}
|
||||
* The key in the Hash representing {@link org.springframework.session.ExpiringSession#getMaxInactiveIntervalInSeconds()}
|
||||
*/
|
||||
static final String MAX_INACTIVE_ATTR = "maxInactiveInterval";
|
||||
|
||||
@@ -144,7 +144,7 @@ public class RedisOperationsSessionRepository implements SessionRepository<Redis
|
||||
private final RedisSessionExpirationPolicy expirationPolicy;
|
||||
|
||||
/**
|
||||
* If non-null, this value is used to override the default value for {@link RedisSession#setMaxInactiveInterval(int)}.
|
||||
* If non-null, this value is used to override the default value for {@link RedisSession#setMaxInactiveIntervalInSeconds(int)}.
|
||||
*/
|
||||
private Integer defaultMaxInactiveInterval;
|
||||
|
||||
@@ -216,7 +216,7 @@ public class RedisOperationsSessionRepository implements SessionRepository<Redis
|
||||
if(CREATION_TIME_ATTR.equals(key)) {
|
||||
loaded.setCreationTime((Long) entry.getValue());
|
||||
} else if(MAX_INACTIVE_ATTR.equals(key)) {
|
||||
loaded.setMaxInactiveInterval((Integer) entry.getValue());
|
||||
loaded.setMaxInactiveIntervalInSeconds((Integer) entry.getValue());
|
||||
} else if(LAST_ACCESSED_ATTR.equals(key)) {
|
||||
loaded.setLastAccessedTime((Long) entry.getValue());
|
||||
} else if(key.startsWith(SESSION_ATTR_PREFIX)) {
|
||||
@@ -227,7 +227,7 @@ public class RedisOperationsSessionRepository implements SessionRepository<Redis
|
||||
return null;
|
||||
}
|
||||
RedisSession result = new RedisSession(loaded);
|
||||
result.originalLastAccessTime = loaded.getLastAccessedTime() + TimeUnit.SECONDS.toMillis(loaded.getMaxInactiveInterval());
|
||||
result.originalLastAccessTime = loaded.getLastAccessedTime() + TimeUnit.SECONDS.toMillis(loaded.getMaxInactiveIntervalInSeconds());
|
||||
result.setLastAccessedTime(System.currentTimeMillis());
|
||||
return result;
|
||||
}
|
||||
@@ -248,7 +248,7 @@ public class RedisOperationsSessionRepository implements SessionRepository<Redis
|
||||
public RedisSession createSession() {
|
||||
RedisSession redisSession = new RedisSession();
|
||||
if(defaultMaxInactiveInterval != null) {
|
||||
redisSession.setMaxInactiveInterval(defaultMaxInactiveInterval);
|
||||
redisSession.setMaxInactiveIntervalInSeconds(defaultMaxInactiveInterval);
|
||||
}
|
||||
return redisSession;
|
||||
}
|
||||
@@ -314,7 +314,7 @@ public class RedisOperationsSessionRepository implements SessionRepository<Redis
|
||||
RedisSession() {
|
||||
this(new MapSession());
|
||||
delta.put(CREATION_TIME_ATTR, getCreationTime());
|
||||
delta.put(MAX_INACTIVE_ATTR, getMaxInactiveInterval());
|
||||
delta.put(MAX_INACTIVE_ATTR, getMaxInactiveIntervalInSeconds());
|
||||
delta.put(LAST_ACCESSED_ATTR, getLastAccessedTime());
|
||||
}
|
||||
|
||||
@@ -349,13 +349,13 @@ public class RedisOperationsSessionRepository implements SessionRepository<Redis
|
||||
return cached.getLastAccessedTime();
|
||||
}
|
||||
|
||||
public void setMaxInactiveInterval(int interval) {
|
||||
cached.setMaxInactiveInterval(interval);
|
||||
delta.put(MAX_INACTIVE_ATTR, getMaxInactiveInterval());
|
||||
public void setMaxInactiveIntervalInSeconds(int interval) {
|
||||
cached.setMaxInactiveIntervalInSeconds(interval);
|
||||
delta.put(MAX_INACTIVE_ATTR, getMaxInactiveIntervalInSeconds());
|
||||
}
|
||||
|
||||
public int getMaxInactiveInterval() {
|
||||
return cached.getMaxInactiveInterval();
|
||||
public int getMaxInactiveIntervalInSeconds() {
|
||||
return cached.getMaxInactiveIntervalInSeconds();
|
||||
}
|
||||
|
||||
public Object getAttribute(String attributeName) {
|
||||
|
||||
@@ -67,7 +67,7 @@ final class RedisSessionExpirationPolicy {
|
||||
|
||||
public void onDelete(ExpiringSession session) {
|
||||
long lastAccessedTime = session.getLastAccessedTime();
|
||||
int maxInactiveInterval = session.getMaxInactiveInterval();
|
||||
int maxInactiveInterval = session.getMaxInactiveIntervalInSeconds();
|
||||
|
||||
long toExpire = roundUpToNextMinute(lastAccessedTime, maxInactiveInterval);
|
||||
String expireKey = getExpirationKey(toExpire);
|
||||
@@ -80,12 +80,12 @@ final class RedisSessionExpirationPolicy {
|
||||
expirationRedisOperations.boundSetOps(expireKey).remove(session.getId());
|
||||
}
|
||||
|
||||
long toExpire = roundUpToNextMinute(session.getLastAccessedTime(), session.getMaxInactiveInterval());
|
||||
long toExpire = roundUpToNextMinute(session.getLastAccessedTime(), session.getMaxInactiveIntervalInSeconds());
|
||||
|
||||
String expireKey = getExpirationKey(toExpire);
|
||||
expirationRedisOperations.boundSetOps(expireKey).add(session.getId());
|
||||
|
||||
long redisExpirationInSeconds = session.getMaxInactiveInterval();
|
||||
long redisExpirationInSeconds = session.getMaxInactiveIntervalInSeconds();
|
||||
String sessionKey = getSessionKey(session.getId());
|
||||
expirationRedisOperations.boundSetOps(expireKey).expire(redisExpirationInSeconds, TimeUnit.SECONDS);
|
||||
sessionRedisOperations.boundHashOps(sessionKey).expire(redisExpirationInSeconds, TimeUnit.SECONDS);
|
||||
|
||||
@@ -249,11 +249,11 @@ public class SessionRepositoryFilter<S extends ExpiringSession> extends OncePerR
|
||||
}
|
||||
|
||||
public void setMaxInactiveInterval(int interval) {
|
||||
session.setMaxInactiveInterval(interval);
|
||||
session.setMaxInactiveIntervalInSeconds(interval);
|
||||
}
|
||||
|
||||
public int getMaxInactiveInterval() {
|
||||
return session.getMaxInactiveInterval();
|
||||
return session.getMaxInactiveIntervalInSeconds();
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
|
||||
@@ -35,7 +35,7 @@ public class MapSessionRepositoryTests {
|
||||
|
||||
@Test
|
||||
public void getSessionExpired() {
|
||||
session.setMaxInactiveInterval(1);
|
||||
session.setMaxInactiveIntervalInSeconds(1);
|
||||
session.setLastAccessedTime(System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(5));
|
||||
repository.save(session);
|
||||
|
||||
@@ -47,16 +47,16 @@ public class MapSessionRepositoryTests {
|
||||
ExpiringSession session = repository.createSession();
|
||||
|
||||
assertThat(session).isInstanceOf(MapSession.class);
|
||||
assertThat(session.getMaxInactiveInterval()).isEqualTo(new MapSession().getMaxInactiveInterval());
|
||||
assertThat(session.getMaxInactiveIntervalInSeconds()).isEqualTo(new MapSession().getMaxInactiveIntervalInSeconds());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createSessionCustomDefaultExpiration() {
|
||||
final int expectedMaxInterval = new MapSession().getMaxInactiveInterval() + 10;
|
||||
final int expectedMaxInterval = new MapSession().getMaxInactiveIntervalInSeconds() + 10;
|
||||
repository.setDefaultMaxInactiveInterval(expectedMaxInterval);
|
||||
|
||||
ExpiringSession session = repository.createSession();
|
||||
|
||||
assertThat(session.getMaxInactiveInterval()).isEqualTo(expectedMaxInterval);
|
||||
assertThat(session.getMaxInactiveIntervalInSeconds()).isEqualTo(expectedMaxInterval);
|
||||
}
|
||||
}
|
||||
@@ -83,11 +83,11 @@ public class MapSessionTests {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void setMaxInactiveInterval(int interval) {
|
||||
public void setMaxInactiveIntervalInSeconds(int interval) {
|
||||
|
||||
}
|
||||
|
||||
public int getMaxInactiveInterval() {
|
||||
public int getMaxInactiveIntervalInSeconds() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ public class RedisOperationsSessionRepositoryTests {
|
||||
@Test
|
||||
public void createSessionDefaultMaxInactiveInterval() throws Exception {
|
||||
ExpiringSession session = redisRepository.createSession();
|
||||
assertThat(session.getMaxInactiveInterval()).isEqualTo(new MapSession().getMaxInactiveInterval());
|
||||
assertThat(session.getMaxInactiveIntervalInSeconds()).isEqualTo(new MapSession().getMaxInactiveIntervalInSeconds());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -86,7 +86,7 @@ public class RedisOperationsSessionRepositoryTests {
|
||||
int interval = 1;
|
||||
redisRepository.setDefaultMaxInactiveInterval(interval);
|
||||
ExpiringSession session = redisRepository.createSession();
|
||||
assertThat(session.getMaxInactiveInterval()).isEqualTo(interval);
|
||||
assertThat(session.getMaxInactiveIntervalInSeconds()).isEqualTo(interval);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -164,7 +164,7 @@ public class RedisOperationsSessionRepositoryTests {
|
||||
Map map = map(
|
||||
getSessionAttrNameKey(attrName), expected.getAttribute(attrName),
|
||||
CREATION_TIME_ATTR, expected.getCreationTime(),
|
||||
MAX_INACTIVE_ATTR, expected.getMaxInactiveInterval(),
|
||||
MAX_INACTIVE_ATTR, expected.getMaxInactiveIntervalInSeconds(),
|
||||
LAST_ACCESSED_ATTR, expected.getLastAccessedTime());
|
||||
when(boundHashOperations.entries()).thenReturn(map);
|
||||
when(expirationRedisOperations.boundSetOps(anyString())).thenReturn(boundSetOperations);
|
||||
@@ -206,7 +206,7 @@ public class RedisOperationsSessionRepositoryTests {
|
||||
Map map = map(
|
||||
getSessionAttrNameKey(attrName), expected.getAttribute(attrName),
|
||||
CREATION_TIME_ATTR, expected.getCreationTime(),
|
||||
MAX_INACTIVE_ATTR, expected.getMaxInactiveInterval(),
|
||||
MAX_INACTIVE_ATTR, expected.getMaxInactiveIntervalInSeconds(),
|
||||
LAST_ACCESSED_ATTR, expected.getLastAccessedTime());
|
||||
when(boundHashOperations.entries()).thenReturn(map);
|
||||
|
||||
@@ -216,7 +216,7 @@ public class RedisOperationsSessionRepositoryTests {
|
||||
assertThat(session.getAttributeNames()).isEqualTo(expected.getAttributeNames());
|
||||
assertThat(session.getAttribute(attrName)).isEqualTo(expected.getAttribute(attrName));
|
||||
assertThat(session.getCreationTime()).isEqualTo(expected.getCreationTime());
|
||||
assertThat(session.getMaxInactiveInterval()).isEqualTo(expected.getMaxInactiveInterval());
|
||||
assertThat(session.getMaxInactiveIntervalInSeconds()).isEqualTo(expected.getMaxInactiveIntervalInSeconds());
|
||||
assertThat(session.getLastAccessedTime()).isGreaterThanOrEqualTo(now);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user