Add defaultMaxInactiveInterval to REdisOperationsSessionRepository

This commit is contained in:
Rob Winch
2014-06-26 16:52:19 -05:00
parent 2edeb9e498
commit 6fb7e3dfda
2 changed files with 59 additions and 2 deletions

View File

@@ -39,11 +39,23 @@ public class RedisOperationsSessionRepository implements SessionRepository<Redis
private final RedisOperations<String,Session> redisTemplate;
private Integer defaultMaxInactiveInterval;
public RedisOperationsSessionRepository(RedisOperations<String, Session> redisTemplate) {
this.redisTemplate = redisTemplate;
}
@Override
/**
* Sets the maximum inactive interval in seconds between requests before newly created sessions will be
* invalidated. A negative time indicates that the session will never timeout.
*
* @param defaultMaxInactiveInterval the number of seconds that the {@link Session} should be kept alive between client requests.
*/
public void setDefaultMaxInactiveInterval(int defaultMaxInactiveInterval) {
this.defaultMaxInactiveInterval = defaultMaxInactiveInterval;
}
@Override
public void save(RedisSession session) {
session.saveDelta();
}
@@ -79,7 +91,11 @@ public class RedisOperationsSessionRepository implements SessionRepository<Redis
@Override
public RedisSession createSession() {
return new RedisSession();
RedisSession redisSession = new RedisSession();
if(defaultMaxInactiveInterval != null) {
redisSession.setMaxInactiveInterval(defaultMaxInactiveInterval);
}
return redisSession;
}
private String getKey(String sessionId) {

View File

@@ -0,0 +1,41 @@
package org.springframework.session.redis;
import static org.fest.assertions.Assertions.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.session.MapSession;
import org.springframework.session.Session;
import static org.junit.Assert.*;
@RunWith(MockitoJUnitRunner.class)
public class RedisOperationsSessionRepositoryTests {
@Mock
RedisOperations redisOperations;
private RedisOperationsSessionRepository redisRepository;
@Before
public void setup() {
this.redisRepository = new RedisOperationsSessionRepository(redisOperations);
}
@Test
public void createSessionDefaultMaxInactiveInterval() throws Exception {
Session session = redisRepository.createSession();
assertThat(session.getMaxInactiveInterval()).isEqualTo(new MapSession().getMaxInactiveInterval());
}
@Test
public void createSessionCustomMaxInactiveInterval() throws Exception {
int interval = 1;
redisRepository.setDefaultMaxInactiveInterval(interval);
Session session = redisRepository.createSession();
assertThat(session.getMaxInactiveInterval()).isEqualTo(interval);
}
}