Include Time Unit for ExpiringSession maxinactiveInterval

Fixes gh-82
This commit is contained in:
Rob Winch
2015-01-05 14:10:04 -06:00
parent 755754e173
commit f4b87b7ae7
10 changed files with 38 additions and 38 deletions

View File

@@ -76,7 +76,7 @@ public class IndexDocTests {
public void demo() { public void demo() {
S toSave = repository.createSession(); // <2> S toSave = repository.createSession(); // <2>
// ... // ...
toSave.setMaxInactiveInterval(30); // <3> toSave.setMaxInactiveIntervalInSeconds(30); // <3>
repository.save(toSave); // <4> repository.save(toSave); // <4>

View File

@@ -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. * @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. * 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. * @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. * Returns true if the session is expired.

View File

@@ -42,7 +42,7 @@ import java.util.concurrent.TimeUnit;
*/ */
public final class MapSession implements ExpiringSession, Serializable { 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; 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.lastAccessedTime = session.getLastAccessedTime();
this.creationTime = session.getCreationTime(); this.creationTime = session.getCreationTime();
this.maxInactiveInterval = session.getMaxInactiveInterval(); this.maxInactiveInterval = session.getMaxInactiveIntervalInSeconds();
} }
public void setLastAccessedTime(long lastAccessedTime) { public void setLastAccessedTime(long lastAccessedTime) {
@@ -98,11 +98,11 @@ public final class MapSession implements ExpiringSession, Serializable {
return lastAccessedTime; return lastAccessedTime;
} }
public void setMaxInactiveInterval(int interval) { public void setMaxInactiveIntervalInSeconds(int interval) {
this.maxInactiveInterval = interval; this.maxInactiveInterval = interval;
} }
public int getMaxInactiveInterval() { public int getMaxInactiveIntervalInSeconds() {
return maxInactiveInterval; return maxInactiveInterval;
} }

View File

@@ -34,7 +34,7 @@ import java.util.concurrent.ConcurrentHashMap;
*/ */
public class MapSessionRepository implements SessionRepository<ExpiringSession> { 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; 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. * @param defaultMaxInactiveInterval the number of seconds that the {@link Session} should be kept alive between client requests.
*/ */
public void setDefaultMaxInactiveInterval(int defaultMaxInactiveInterval) { public void setDefaultMaxInactiveInterval(int defaultMaxInactiveInterval) {
@@ -92,7 +92,7 @@ public class MapSessionRepository implements SessionRepository<ExpiringSession>
public ExpiringSession createSession() { public ExpiringSession createSession() {
ExpiringSession result = new MapSession(); ExpiringSession result = new MapSession();
if(defaultMaxInactiveInterval != null) { if(defaultMaxInactiveInterval != null) {
result.setMaxInactiveInterval(defaultMaxInactiveInterval); result.setMaxInactiveIntervalInSeconds(defaultMaxInactiveInterval);
} }
return result; return result;
} }

View File

@@ -76,7 +76,7 @@ import org.springframework.util.Assert;
* <p> * <p>
* An expiration is associated to each session using the <a * An expiration is associated to each session using the <a
* href="http://redis.io/commands/expire">EXPIRE command</a> based upon the * 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: * . For example:
* </p> * </p>
* *
@@ -123,7 +123,7 @@ public class RedisOperationsSessionRepository implements SessionRepository<Redis
static final String CREATION_TIME_ATTR = "creationTime"; 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"; static final String MAX_INACTIVE_ATTR = "maxInactiveInterval";
@@ -144,7 +144,7 @@ public class RedisOperationsSessionRepository implements SessionRepository<Redis
private final RedisSessionExpirationPolicy expirationPolicy; 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; private Integer defaultMaxInactiveInterval;
@@ -216,7 +216,7 @@ public class RedisOperationsSessionRepository implements SessionRepository<Redis
if(CREATION_TIME_ATTR.equals(key)) { if(CREATION_TIME_ATTR.equals(key)) {
loaded.setCreationTime((Long) entry.getValue()); loaded.setCreationTime((Long) entry.getValue());
} else if(MAX_INACTIVE_ATTR.equals(key)) { } else if(MAX_INACTIVE_ATTR.equals(key)) {
loaded.setMaxInactiveInterval((Integer) entry.getValue()); loaded.setMaxInactiveIntervalInSeconds((Integer) entry.getValue());
} else if(LAST_ACCESSED_ATTR.equals(key)) { } else if(LAST_ACCESSED_ATTR.equals(key)) {
loaded.setLastAccessedTime((Long) entry.getValue()); loaded.setLastAccessedTime((Long) entry.getValue());
} else if(key.startsWith(SESSION_ATTR_PREFIX)) { } else if(key.startsWith(SESSION_ATTR_PREFIX)) {
@@ -227,7 +227,7 @@ public class RedisOperationsSessionRepository implements SessionRepository<Redis
return null; return null;
} }
RedisSession result = new RedisSession(loaded); 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()); result.setLastAccessedTime(System.currentTimeMillis());
return result; return result;
} }
@@ -248,7 +248,7 @@ public class RedisOperationsSessionRepository implements SessionRepository<Redis
public RedisSession createSession() { public RedisSession createSession() {
RedisSession redisSession = new RedisSession(); RedisSession redisSession = new RedisSession();
if(defaultMaxInactiveInterval != null) { if(defaultMaxInactiveInterval != null) {
redisSession.setMaxInactiveInterval(defaultMaxInactiveInterval); redisSession.setMaxInactiveIntervalInSeconds(defaultMaxInactiveInterval);
} }
return redisSession; return redisSession;
} }
@@ -314,7 +314,7 @@ public class RedisOperationsSessionRepository implements SessionRepository<Redis
RedisSession() { RedisSession() {
this(new MapSession()); this(new MapSession());
delta.put(CREATION_TIME_ATTR, getCreationTime()); delta.put(CREATION_TIME_ATTR, getCreationTime());
delta.put(MAX_INACTIVE_ATTR, getMaxInactiveInterval()); delta.put(MAX_INACTIVE_ATTR, getMaxInactiveIntervalInSeconds());
delta.put(LAST_ACCESSED_ATTR, getLastAccessedTime()); delta.put(LAST_ACCESSED_ATTR, getLastAccessedTime());
} }
@@ -349,13 +349,13 @@ public class RedisOperationsSessionRepository implements SessionRepository<Redis
return cached.getLastAccessedTime(); return cached.getLastAccessedTime();
} }
public void setMaxInactiveInterval(int interval) { public void setMaxInactiveIntervalInSeconds(int interval) {
cached.setMaxInactiveInterval(interval); cached.setMaxInactiveIntervalInSeconds(interval);
delta.put(MAX_INACTIVE_ATTR, getMaxInactiveInterval()); delta.put(MAX_INACTIVE_ATTR, getMaxInactiveIntervalInSeconds());
} }
public int getMaxInactiveInterval() { public int getMaxInactiveIntervalInSeconds() {
return cached.getMaxInactiveInterval(); return cached.getMaxInactiveIntervalInSeconds();
} }
public Object getAttribute(String attributeName) { public Object getAttribute(String attributeName) {

View File

@@ -67,7 +67,7 @@ final class RedisSessionExpirationPolicy {
public void onDelete(ExpiringSession session) { public void onDelete(ExpiringSession session) {
long lastAccessedTime = session.getLastAccessedTime(); long lastAccessedTime = session.getLastAccessedTime();
int maxInactiveInterval = session.getMaxInactiveInterval(); int maxInactiveInterval = session.getMaxInactiveIntervalInSeconds();
long toExpire = roundUpToNextMinute(lastAccessedTime, maxInactiveInterval); long toExpire = roundUpToNextMinute(lastAccessedTime, maxInactiveInterval);
String expireKey = getExpirationKey(toExpire); String expireKey = getExpirationKey(toExpire);
@@ -80,12 +80,12 @@ final class RedisSessionExpirationPolicy {
expirationRedisOperations.boundSetOps(expireKey).remove(session.getId()); expirationRedisOperations.boundSetOps(expireKey).remove(session.getId());
} }
long toExpire = roundUpToNextMinute(session.getLastAccessedTime(), session.getMaxInactiveInterval()); long toExpire = roundUpToNextMinute(session.getLastAccessedTime(), session.getMaxInactiveIntervalInSeconds());
String expireKey = getExpirationKey(toExpire); String expireKey = getExpirationKey(toExpire);
expirationRedisOperations.boundSetOps(expireKey).add(session.getId()); expirationRedisOperations.boundSetOps(expireKey).add(session.getId());
long redisExpirationInSeconds = session.getMaxInactiveInterval(); long redisExpirationInSeconds = session.getMaxInactiveIntervalInSeconds();
String sessionKey = getSessionKey(session.getId()); String sessionKey = getSessionKey(session.getId());
expirationRedisOperations.boundSetOps(expireKey).expire(redisExpirationInSeconds, TimeUnit.SECONDS); expirationRedisOperations.boundSetOps(expireKey).expire(redisExpirationInSeconds, TimeUnit.SECONDS);
sessionRedisOperations.boundHashOps(sessionKey).expire(redisExpirationInSeconds, TimeUnit.SECONDS); sessionRedisOperations.boundHashOps(sessionKey).expire(redisExpirationInSeconds, TimeUnit.SECONDS);

View File

@@ -249,11 +249,11 @@ public class SessionRepositoryFilter<S extends ExpiringSession> extends OncePerR
} }
public void setMaxInactiveInterval(int interval) { public void setMaxInactiveInterval(int interval) {
session.setMaxInactiveInterval(interval); session.setMaxInactiveIntervalInSeconds(interval);
} }
public int getMaxInactiveInterval() { public int getMaxInactiveInterval() {
return session.getMaxInactiveInterval(); return session.getMaxInactiveIntervalInSeconds();
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")

View File

@@ -35,7 +35,7 @@ public class MapSessionRepositoryTests {
@Test @Test
public void getSessionExpired() { public void getSessionExpired() {
session.setMaxInactiveInterval(1); session.setMaxInactiveIntervalInSeconds(1);
session.setLastAccessedTime(System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(5)); session.setLastAccessedTime(System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(5));
repository.save(session); repository.save(session);
@@ -47,16 +47,16 @@ public class MapSessionRepositoryTests {
ExpiringSession session = repository.createSession(); ExpiringSession session = repository.createSession();
assertThat(session).isInstanceOf(MapSession.class); assertThat(session).isInstanceOf(MapSession.class);
assertThat(session.getMaxInactiveInterval()).isEqualTo(new MapSession().getMaxInactiveInterval()); assertThat(session.getMaxInactiveIntervalInSeconds()).isEqualTo(new MapSession().getMaxInactiveIntervalInSeconds());
} }
@Test @Test
public void createSessionCustomDefaultExpiration() { public void createSessionCustomDefaultExpiration() {
final int expectedMaxInterval = new MapSession().getMaxInactiveInterval() + 10; final int expectedMaxInterval = new MapSession().getMaxInactiveIntervalInSeconds() + 10;
repository.setDefaultMaxInactiveInterval(expectedMaxInterval); repository.setDefaultMaxInactiveInterval(expectedMaxInterval);
ExpiringSession session = repository.createSession(); ExpiringSession session = repository.createSession();
assertThat(session.getMaxInactiveInterval()).isEqualTo(expectedMaxInterval); assertThat(session.getMaxInactiveIntervalInSeconds()).isEqualTo(expectedMaxInterval);
} }
} }

View File

@@ -83,11 +83,11 @@ public class MapSessionTests {
return 0; return 0;
} }
public void setMaxInactiveInterval(int interval) { public void setMaxInactiveIntervalInSeconds(int interval) {
} }
public int getMaxInactiveInterval() { public int getMaxInactiveIntervalInSeconds() {
return 0; return 0;
} }

View File

@@ -78,7 +78,7 @@ public class RedisOperationsSessionRepositoryTests {
@Test @Test
public void createSessionDefaultMaxInactiveInterval() throws Exception { public void createSessionDefaultMaxInactiveInterval() throws Exception {
ExpiringSession session = redisRepository.createSession(); ExpiringSession session = redisRepository.createSession();
assertThat(session.getMaxInactiveInterval()).isEqualTo(new MapSession().getMaxInactiveInterval()); assertThat(session.getMaxInactiveIntervalInSeconds()).isEqualTo(new MapSession().getMaxInactiveIntervalInSeconds());
} }
@Test @Test
@@ -86,7 +86,7 @@ public class RedisOperationsSessionRepositoryTests {
int interval = 1; int interval = 1;
redisRepository.setDefaultMaxInactiveInterval(interval); redisRepository.setDefaultMaxInactiveInterval(interval);
ExpiringSession session = redisRepository.createSession(); ExpiringSession session = redisRepository.createSession();
assertThat(session.getMaxInactiveInterval()).isEqualTo(interval); assertThat(session.getMaxInactiveIntervalInSeconds()).isEqualTo(interval);
} }
@Test @Test
@@ -164,7 +164,7 @@ public class RedisOperationsSessionRepositoryTests {
Map map = map( Map map = map(
getSessionAttrNameKey(attrName), expected.getAttribute(attrName), getSessionAttrNameKey(attrName), expected.getAttribute(attrName),
CREATION_TIME_ATTR, expected.getCreationTime(), CREATION_TIME_ATTR, expected.getCreationTime(),
MAX_INACTIVE_ATTR, expected.getMaxInactiveInterval(), MAX_INACTIVE_ATTR, expected.getMaxInactiveIntervalInSeconds(),
LAST_ACCESSED_ATTR, expected.getLastAccessedTime()); LAST_ACCESSED_ATTR, expected.getLastAccessedTime());
when(boundHashOperations.entries()).thenReturn(map); when(boundHashOperations.entries()).thenReturn(map);
when(expirationRedisOperations.boundSetOps(anyString())).thenReturn(boundSetOperations); when(expirationRedisOperations.boundSetOps(anyString())).thenReturn(boundSetOperations);
@@ -206,7 +206,7 @@ public class RedisOperationsSessionRepositoryTests {
Map map = map( Map map = map(
getSessionAttrNameKey(attrName), expected.getAttribute(attrName), getSessionAttrNameKey(attrName), expected.getAttribute(attrName),
CREATION_TIME_ATTR, expected.getCreationTime(), CREATION_TIME_ATTR, expected.getCreationTime(),
MAX_INACTIVE_ATTR, expected.getMaxInactiveInterval(), MAX_INACTIVE_ATTR, expected.getMaxInactiveIntervalInSeconds(),
LAST_ACCESSED_ATTR, expected.getLastAccessedTime()); LAST_ACCESSED_ATTR, expected.getLastAccessedTime());
when(boundHashOperations.entries()).thenReturn(map); when(boundHashOperations.entries()).thenReturn(map);
@@ -216,7 +216,7 @@ public class RedisOperationsSessionRepositoryTests {
assertThat(session.getAttributeNames()).isEqualTo(expected.getAttributeNames()); assertThat(session.getAttributeNames()).isEqualTo(expected.getAttributeNames());
assertThat(session.getAttribute(attrName)).isEqualTo(expected.getAttribute(attrName)); assertThat(session.getAttribute(attrName)).isEqualTo(expected.getAttribute(attrName));
assertThat(session.getCreationTime()).isEqualTo(expected.getCreationTime()); assertThat(session.getCreationTime()).isEqualTo(expected.getCreationTime());
assertThat(session.getMaxInactiveInterval()).isEqualTo(expected.getMaxInactiveInterval()); assertThat(session.getMaxInactiveIntervalInSeconds()).isEqualTo(expected.getMaxInactiveIntervalInSeconds());
assertThat(session.getLastAccessedTime()).isGreaterThanOrEqualTo(now); assertThat(session.getLastAccessedTime()).isGreaterThanOrEqualTo(now);
} }