Refactor Redis configuration

Closes gh-941
This commit is contained in:
Vedran Pavic
2017-11-27 20:28:41 +01:00
parent 41de1b087a
commit 6a370b1ef8
3 changed files with 12 additions and 47 deletions

View File

@@ -30,13 +30,10 @@ import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.session.FindByIndexNameSessionRepository;
@@ -63,9 +60,12 @@ import org.springframework.util.Assert;
* A typical example of how to create a new instance can be seen below:
*
* <pre>
* LettuceConnectionFactory factory = new LettuceConnectionFactory();
* RedisTemplate&lt;Object, Object&gt; redisTemplate = new RedisTemplate();
*
* RedisOperationsSessionRepository redisSessionRepository = new RedisOperationsSessionRepository(factory);
* // ... configure redisTemplate ...
*
* RedisOperationsSessionRepository redisSessionRepository =
* new RedisOperationsSessionRepository(redisTemplate);
* </pre>
*
* <p>
@@ -313,17 +313,6 @@ public class RedisOperationsSessionRepository implements
private RedisFlushMode redisFlushMode = RedisFlushMode.ON_SAVE;
/**
* Allows creating an instance and uses a default {@link RedisOperations} for both
* managing the session and the expirations.
*
* @param redisConnectionFactory the {@link RedisConnectionFactory} to use.
*/
public RedisOperationsSessionRepository(
RedisConnectionFactory redisConnectionFactory) {
this(createDefaultTemplate(redisConnectionFactory));
}
/**
* Creates a new instance. For an example, refer to the class level javadoc.
*
@@ -648,17 +637,6 @@ public class RedisOperationsSessionRepository implements
return SESSION_ATTR_PREFIX + attributeName;
}
private static RedisTemplate<Object, Object> createDefaultTemplate(
RedisConnectionFactory connectionFactory) {
Assert.notNull(connectionFactory, "connectionFactory cannot be null");
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(connectionFactory);
template.afterPropertiesSet();
return template;
}
/**
* A custom implementation of {@link Session} that uses a {@link MapSession} as the
* basis for its mapping. It keeps track of any attributes that have changed. When

View File

@@ -110,27 +110,11 @@ public class RedisOperationsSessionRepositoryTests {
this.cached.setLastAccessedTime(Instant.ofEpochMilli(1404360000000L));
}
@Test(expected = IllegalArgumentException.class)
public void constructorNullConnectionFactory() {
new RedisOperationsSessionRepository((RedisConnectionFactory) null);
}
@Test(expected = IllegalArgumentException.class)
public void setApplicationEventPublisherNull() {
this.redisRepository.setApplicationEventPublisher(null);
}
// gh-61
@Test
public void constructorConnectionFactory() {
this.redisRepository = new RedisOperationsSessionRepository(this.factory);
RedisSession session = this.redisRepository.createSession();
given(this.factory.getConnection()).willReturn(this.connection);
this.redisRepository.save(session);
}
@Test
public void changeSessionId() {
RedisSession createSession = this.redisRepository.createSession();