Skip to content

Commit 0d458a4

Browse files
committed
Update Redis docs
Issue gh-1711
1 parent 102027a commit 0d458a4

File tree

3 files changed

+90
-4
lines changed

3 files changed

+90
-4
lines changed

spring-session-docs/modules/ROOT/examples/java/docs/IndexDocTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.session.SessionRepository;
3636
import org.springframework.session.data.redis.ReactiveRedisSessionRepository;
3737
import org.springframework.session.data.redis.RedisIndexedSessionRepository;
38+
import org.springframework.session.data.redis.RedisSessionRepository;
3839
import org.springframework.session.jdbc.JdbcIndexedSessionRepository;
3940
import org.springframework.session.web.http.SessionRepositoryFilter;
4041
import org.springframework.transaction.support.TransactionTemplate;
@@ -113,6 +114,18 @@ public void demo() {
113114
}
114115
// end::expire-repository-demo[]
115116

117+
@Test
118+
@SuppressWarnings("unused")
119+
void newRedisSessionRepository() {
120+
// tag::new-redissessionrepository[]
121+
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
122+
123+
// ... configure redisTemplate ...
124+
125+
SessionRepository<? extends Session> repository = new RedisSessionRepository(redisTemplate);
126+
// end::new-redissessionrepository[]
127+
}
128+
116129
@Test
117130
@SuppressWarnings("unused")
118131
void newRedisIndexedSessionRepository() {

spring-session-docs/modules/ROOT/pages/api.adoc

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,78 @@ Note that no infrastructure for session expirations is configured for you.
148148
This is because things such as session expiration are highly implementation-dependent.
149149
This means that, if you require cleaning up expired sessions, you are responsible for cleaning up the expired sessions.
150150

151+
[[api-redissessionrepository]]
152+
== Using `RedisSessionRepository`
153+
154+
`RedisSessionRepository` is a `SessionRepository` that is implemented by using Spring Data's `RedisOperations`.
155+
In a web environment, this is typically used in combination with `SessionRepositoryFilter`.
156+
Note that this implementation does not support publishing of session events.
157+
158+
[[api-redissessionrepository-new]]
159+
=== Instantiating a `RedisSessionRepository`
160+
161+
You can see a typical example of how to create a new instance in the following listing:
162+
163+
====
164+
[source,java,indent=0]
165+
----
166+
include::{indexdoc-tests}[tags=new-redissessionrepository]
167+
----
168+
====
169+
170+
For additional information on how to create a `RedisConnectionFactory`, see the Spring Data Redis Reference.
171+
172+
[[api-redissessionrepository-config]]
173+
=== Using `@EnableRedisHttpSession`
174+
175+
In a web environment, the simplest way to create a new `RedisSessionRepository` is to use `@EnableRedisHttpSession`.
176+
You can find complete example usage in the xref:samples.adoc#samples[Samples and Guides (Start Here)].
177+
You can use the following attributes to customize the configuration:
178+
179+
enableIndexingAndEvents
180+
* *enableIndexingAndEvents*: Whether to use a `RedisIndexedSessionRepository` instead of a `RedisSessionRepository`. The default is `false`.
181+
* *maxInactiveIntervalInSeconds*: The amount of time before the session expires, in seconds.
182+
* *redisNamespace*: Allows configuring an application specific namespace for the sessions. Redis keys and channel IDs start with the prefix of `<redisNamespace>:`.
183+
* *flushMode*: Allows specifying when data is written to Redis. The default is only when `save` is invoked on `SessionRepository`.
184+
A value of `FlushMode.IMMEDIATE` writes to Redis as soon as possible.
185+
186+
==== Custom `RedisSerializer`
187+
188+
You can customize the serialization by creating a bean named `springSessionDefaultRedisSerializer` that implements `RedisSerializer<Object>`.
189+
190+
[[api-redissessionrepository-cli]]
191+
=== Viewing the Session in Redis
192+
193+
After https://redis.io/topics/quickstart[installing redis-cli], you can inspect the values in Redis https://redis.io/commands#hash[using the redis-cli].
194+
For example, you can enter the following command into a terminal window:
195+
196+
====
197+
[source,bash]
198+
----
199+
$ redis-cli
200+
redis 127.0.0.1:6379> keys *
201+
1) "spring:session:sessions:4fc39ce3-63b3-4e17-b1c4-5e1ed96fb021" <1>
202+
----
203+
204+
<1> The suffix of this key is the session identifier of the Spring Session.
205+
====
206+
207+
You can also view the attributes of each session by using the `hkeys` command.
208+
The following example shows how to do so:
209+
210+
====
211+
[source,bash]
212+
----
213+
redis 127.0.0.1:6379> hkeys spring:session:sessions:4fc39ce3-63b3-4e17-b1c4-5e1ed96fb021
214+
1) "lastAccessedTime"
215+
2) "creationTime"
216+
3) "maxInactiveInterval"
217+
4) "sessionAttr:username"
218+
redis 127.0.0.1:6379> hget spring:session:sessions:4fc39ce3-63b3-4e17-b1c4-5e1ed96fb021 sessionAttr:username
219+
"\xac\xed\x00\x05t\x00\x03rob"
220+
----
221+
====
222+
151223
[[api-redisindexedsessionrepository]]
152224
== Using `RedisIndexedSessionRepository`
153225

@@ -170,12 +242,13 @@ include::{indexdoc-tests}[tags=new-redisindexedsessionrepository]
170242
For additional information on how to create a `RedisConnectionFactory`, see the Spring Data Redis Reference.
171243

172244
[[api-redisindexedsessionrepository-config]]
173-
=== Using `@EnableRedisHttpSession`
245+
=== Using `@EnableRedisHttpSession(enableIndexingAndEvents = true)`
174246

175-
In a web environment, the simplest way to create a new `RedisIndexedSessionRepository` is to use `@EnableRedisHttpSession`.
247+
In a web environment, the simplest way to create a new `RedisIndexedSessionRepository` is to use `@EnableRedisHttpSession(enableIndexingAndEvents = true)`.
176248
You can find complete example usage in the xref:samples.adoc#samples[Samples and Guides (Start Here)].
177249
You can use the following attributes to customize the configuration:
178250

251+
* *enableIndexingAndEvents*: Whether to use a `RedisIndexedSessionRepository` instead of a `RedisSessionRepository`. The default is `false`.
179252
* *maxInactiveIntervalInSeconds*: The amount of time before the session expires, in seconds.
180253
* *redisNamespace*: Allows configuring an application specific namespace for the sessions. Redis keys and channel IDs start with the prefix of `<redisNamespace>:`.
181254
* *flushMode*: Allows specifying when data is written to Redis. The default is only when `save` is invoked on `SessionRepository`.
@@ -335,7 +408,7 @@ redis-cli config set notify-keyspace-events Egx
335408
----
336409
====
337410

338-
If you use `@EnableRedisHttpSession`, managing the `SessionMessageListener` and enabling the necessary Redis Keyspace events is done automatically.
411+
If you use `@EnableRedisHttpSession(enableIndexingAndEvents = true)`, managing the `SessionMessageListener` and enabling the necessary Redis Keyspace events is done automatically.
339412
However, in a secured Redis enviornment, the config command is disabled.
340413
This means that Spring Session cannot configure Redis Keyspace events for you.
341414
To disable the automatic configuration, add `ConfigureRedisAction.NO_OP` as a bean.

spring-session-docs/modules/ROOT/pages/http-session.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ To use this support, you need to:
235235
* Configure `SessionEventHttpSessionListenerAdapter` as a Spring bean.
236236
* Inject every `HttpSessionListener` into the `SessionEventHttpSessionListenerAdapter`
237237

238-
If you use the configuration support documented in <<httpsession-redis,`HttpSession` with Redis>>, all you need to do is register every `HttpSessionListener` as a bean.
238+
If you use the Redis support with `enableIndexingAndEvents` set to `true`, `@EnableRedisHttpSession(enableIndexingAndEvents = true)`, all you need to do is register every `HttpSessionListener` as a bean.
239239
For example, assume you want to support Spring Security's concurrency control and need to use `HttpSessionEventPublisher`. In that case, you can add `HttpSessionEventPublisher` as a bean.
240240
In Java configuration, this might look like the following:
241241

0 commit comments

Comments
 (0)