Skip to content

Commit 090a10f

Browse files
vpaviceleftherias
authored andcommitted
Improve RedisSessionRepository-based sample configuration
1 parent 2358014 commit 090a10f

File tree

1 file changed

+27
-4
lines changed
  • spring-session-samples/spring-session-sample-boot-redis-simple/src/main/java/sample/config

1 file changed

+27
-4
lines changed

spring-session-samples/spring-session-sample-boot-redis-simple/src/main/java/sample/config/SessionConfig.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,22 +16,37 @@
1616

1717
package sample.config;
1818

19+
import java.time.Duration;
20+
1921
import org.springframework.beans.factory.ObjectProvider;
22+
import org.springframework.boot.autoconfigure.session.RedisSessionProperties;
23+
import org.springframework.boot.autoconfigure.session.SessionProperties;
24+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2025
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
2127
import org.springframework.data.redis.connection.RedisConnectionFactory;
2228
import org.springframework.data.redis.core.RedisOperations;
2329
import org.springframework.data.redis.core.RedisTemplate;
2430
import org.springframework.data.redis.serializer.StringRedisSerializer;
2531
import org.springframework.session.config.annotation.web.http.EnableSpringHttpSession;
2632
import org.springframework.session.data.redis.RedisSessionRepository;
2733

34+
@Configuration(proxyBeanMethods = false)
35+
@EnableConfigurationProperties(RedisSessionProperties.class)
2836
@EnableSpringHttpSession
2937
public class SessionConfig {
3038

39+
private final SessionProperties sessionProperties;
40+
41+
private final RedisSessionProperties redisSessionProperties;
42+
3143
private final RedisConnectionFactory redisConnectionFactory;
3244

33-
public SessionConfig(ObjectProvider<RedisConnectionFactory> redisConnectionFactory) {
34-
this.redisConnectionFactory = redisConnectionFactory.getIfAvailable();
45+
public SessionConfig(SessionProperties sessionProperties, RedisSessionProperties redisSessionProperties,
46+
ObjectProvider<RedisConnectionFactory> redisConnectionFactory) {
47+
this.sessionProperties = sessionProperties;
48+
this.redisSessionProperties = redisSessionProperties;
49+
this.redisConnectionFactory = redisConnectionFactory.getObject();
3550
}
3651

3752
@Bean
@@ -45,7 +60,15 @@ public RedisOperations<String, Object> sessionRedisOperations() {
4560

4661
@Bean
4762
public RedisSessionRepository sessionRepository(RedisOperations<String, Object> sessionRedisOperations) {
48-
return new RedisSessionRepository(sessionRedisOperations);
63+
RedisSessionRepository sessionRepository = new RedisSessionRepository(sessionRedisOperations);
64+
Duration timeout = this.sessionProperties.getTimeout();
65+
if (timeout != null) {
66+
sessionRepository.setDefaultMaxInactiveInterval(timeout);
67+
}
68+
sessionRepository.setKeyNamespace(this.redisSessionProperties.getNamespace());
69+
sessionRepository.setFlushMode(this.redisSessionProperties.getFlushMode());
70+
sessionRepository.setSaveMode(this.redisSessionProperties.getSaveMode());
71+
return sessionRepository;
4972
}
5073

5174
}

0 commit comments

Comments
 (0)