Add Default Expiration Option to MapSessionRepository

Fixes gh-73
This commit is contained in:
Rob Winch
2014-12-11 21:00:03 -06:00
parent 646682b056
commit 977d1f474b
3 changed files with 37 additions and 3 deletions

View File

@@ -33,6 +33,11 @@ import java.util.concurrent.ConcurrentHashMap;
* @since 1.0
*/
public class MapSessionRepository implements SessionRepository<ExpiringSession> {
/**
* If non-null, this value is used to override {@link ExpiringSession#setMaxInactiveInterval(int)}.
*/
private Integer defaultMaxInactiveInterval;
private final Map<String,ExpiringSession> sessions;
/**
@@ -54,6 +59,14 @@ public class MapSessionRepository implements SessionRepository<ExpiringSession>
this.sessions = sessions;
}
/**
* If non-null, this value is used to override {@link ExpiringSession#setMaxInactiveInterval(int)}.
* @param defaultMaxInactiveInterval
*/
public void setDefaultMaxInactiveInterval(int defaultMaxInactiveInterval) {
this.defaultMaxInactiveInterval = Integer.valueOf(defaultMaxInactiveInterval);
}
public void save(ExpiringSession session) {
sessions.put(session.getId(), new MapSession(session));
}
@@ -77,6 +90,10 @@ public class MapSessionRepository implements SessionRepository<ExpiringSession>
}
public ExpiringSession createSession() {
return new MapSession();
ExpiringSession result = new MapSession();
if(defaultMaxInactiveInterval != null) {
result.setMaxInactiveInterval(defaultMaxInactiveInterval);
}
return result;
}
}

View File

@@ -148,7 +148,7 @@ public class RedisOperationsSessionRepository implements SessionRepository<Redis
private final RedisSessionExpirationPolicy expirationPolicy;
/**
* If non-null, this value is used to override {@link RedisSession#setDefaultMaxInactiveInterval(int)}.
* If non-null, this value is used to override the default value for {@link RedisSession#setMaxInactiveInterval(int)}.
*/
private Integer defaultMaxInactiveInterval;

View File

@@ -42,4 +42,21 @@ public class MapSessionRepositoryTests {
assertThat(repository.getSession(session.getId())).isNull();
}
}
@Test
public void createSessionDefaultExpiration() {
ExpiringSession session = repository.createSession();
assertThat(session).isInstanceOf(MapSession.class);
assertThat(session.getMaxInactiveInterval()).isEqualTo(new MapSession().getMaxInactiveInterval());
}
@Test
public void createSessionCustomDefaultExpiration() {
final int expectedMaxInterval = new MapSession().getMaxInactiveInterval() + 10;
repository.setDefaultMaxInactiveInterval(expectedMaxInterval);
ExpiringSession session = repository.createSession();
assertThat(session.getMaxInactiveInterval()).isEqualTo(expectedMaxInterval);
}
}