Class RedisSessionStore
- All Implemented Interfaces:
io.lettuce.core.pubsub.RedisPubSubListener<String,
,String> io.micronaut.session.SessionStore<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>
,AutoCloseable
An implementation of the SessionStore
interface for Redis. Partially inspired by Spring Session.
Session serialization
Sessions are stored within Redis hashes. The values contained within the sessions are serialized by the ObjectSerializer
configured by RedisHttpSessionConfiguration.getValueSerializer()
which by default uses Java serialization. The Jackson Micronaut module includes the ability the configure JSON serialization as an alternative.
Storage Details
Sessions are stored within Redis hashes by default prefixed with micronaut:session:sessions:[SESSION_ID]
. The expiry of the hash is set to 5 minutes after the actual expiry and
expired sessions are simply not returned by findSession(String)
More exact session expiry entries are stored with keys micronaut:session:expiry:[SESSION_ID]
and current active sessions are tracked within sorted set at the key micronaut:session:active-sessions
.
The entries within the set are sorted by expiry time and a scheduled job that runs every minute periodically touches the keys within the set that match the last minute thus ensuring Redis propagates expiry events in a timely manner.
Redis Pub/Sub
This implementation requires the Redis instance to have keyspace event notification enabled with notify-keyspace-events Egx
. The implementation will attempt to enable this programmatically. This behaviour can be disabled with RedisHttpSessionConfiguration.isEnableKeyspaceEvents()
- Since:
- 1.0
- Author:
- Graeme Rocher
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionRedisSessionStore
(io.micronaut.session.SessionIdGenerator sessionIdGenerator, RedisHttpSessionConfiguration sessionConfiguration, io.micronaut.context.BeanLocator beanLocator, io.micronaut.core.serialize.ObjectSerializer defaultSerializer, io.micronaut.core.convert.ConversionService conversionService, ExecutorService scheduledExecutorService, io.micronaut.context.event.ApplicationEventPublisher eventPublisher) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
deleteSession
(String id) CompletableFuture<Optional<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>>
findSession
(String id) io.micronaut.core.serialize.ObjectSerializer
Getter.void
void
io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession
CompletableFuture<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>
save
(io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession session) Methods inherited from class io.lettuce.core.pubsub.RedisPubSubAdapter
psubscribed, punsubscribed, subscribed, unsubscribed
-
Field Details
-
REDIS_SESSION_ENABLED
- See Also:
-
-
Constructor Details
-
RedisSessionStore
public RedisSessionStore(io.micronaut.session.SessionIdGenerator sessionIdGenerator, RedisHttpSessionConfiguration sessionConfiguration, io.micronaut.context.BeanLocator beanLocator, io.micronaut.core.serialize.ObjectSerializer defaultSerializer, io.micronaut.core.convert.ConversionService conversionService, @Named("scheduled") ExecutorService scheduledExecutorService, io.micronaut.context.event.ApplicationEventPublisher eventPublisher) Constructor.- Parameters:
sessionIdGenerator
- sessionIdGeneratorsessionConfiguration
- sessionConfigurationbeanLocator
- beanLocatordefaultSerializer
- The default value serializerconversionService
- The conversion servicescheduledExecutorService
- scheduledExecutorServiceeventPublisher
- eventPublisher- Since:
- 6.0.0
-
-
Method Details
-
getValueSerializer
public io.micronaut.core.serialize.ObjectSerializer getValueSerializer()Getter.- Returns:
- ObjectSerializer
-
message
-
message
-
newSession
public io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession newSession()- Specified by:
newSession
in interfaceio.micronaut.session.SessionStore<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>
-
findSession
public CompletableFuture<Optional<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>> findSession(String id) - Specified by:
findSession
in interfaceio.micronaut.session.SessionStore<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>
-
deleteSession
- Specified by:
deleteSession
in interfaceio.micronaut.session.SessionStore<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>
-
save
public CompletableFuture<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession> save(io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession session) - Specified by:
save
in interfaceio.micronaut.session.SessionStore<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>
-
close
@PreDestroy public void close()- Specified by:
close
in interfaceAutoCloseable
-