@Singleton
@Primary
@Requires(property="micronaut.session.http.redis.enabled",
value="true")
@Replaces(value=io.micronaut.session.InMemorySessionStore.class)
public class RedisSessionStore
extends io.lettuce.core.pubsub.RedisPubSubAdapter<java.lang.String,java.lang.String>
implements io.micronaut.session.SessionStore<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>, java.lang.AutoCloseable
An implementation of the SessionStore
interface for Redis. Partially inspired by Spring Session.
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.
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.
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()
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
REDIS_SESSION_ENABLED |
Constructor and Description |
---|
RedisSessionStore(io.micronaut.session.SessionIdGenerator sessionIdGenerator,
RedisHttpSessionConfiguration sessionConfiguration,
io.micronaut.context.BeanLocator beanLocator,
io.micronaut.core.serialize.ObjectSerializer defaultSerializer,
java.util.concurrent.ExecutorService scheduledExecutorService,
io.micronaut.context.event.ApplicationEventPublisher eventPublisher)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
close() |
java.util.concurrent.CompletableFuture<java.lang.Boolean> |
deleteSession(java.lang.String id) |
java.util.concurrent.CompletableFuture<java.util.Optional<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>> |
findSession(java.lang.String id) |
io.micronaut.core.serialize.ObjectSerializer |
getValueSerializer()
Getter.
|
void |
message(java.lang.String channel,
java.lang.String message) |
void |
message(java.lang.String pattern,
java.lang.String channel,
java.lang.String message) |
io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession |
newSession() |
java.util.concurrent.CompletableFuture<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession> |
save(io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession session) |
public static final java.lang.String REDIS_SESSION_ENABLED
public RedisSessionStore(io.micronaut.session.SessionIdGenerator sessionIdGenerator, RedisHttpSessionConfiguration sessionConfiguration, io.micronaut.context.BeanLocator beanLocator, io.micronaut.core.serialize.ObjectSerializer defaultSerializer, @Named(value="scheduled") java.util.concurrent.ExecutorService scheduledExecutorService, io.micronaut.context.event.ApplicationEventPublisher eventPublisher)
sessionIdGenerator
- sessionIdGeneratorsessionConfiguration
- sessionConfigurationbeanLocator
- beanLocatordefaultSerializer
- The default value serializerscheduledExecutorService
- scheduledExecutorServiceeventPublisher
- eventPublisherpublic io.micronaut.core.serialize.ObjectSerializer getValueSerializer()
public void message(java.lang.String channel, java.lang.String message)
message
in interface io.lettuce.core.pubsub.RedisPubSubListener<java.lang.String,java.lang.String>
message
in class io.lettuce.core.pubsub.RedisPubSubAdapter<java.lang.String,java.lang.String>
public void message(java.lang.String pattern, java.lang.String channel, java.lang.String message)
message
in interface io.lettuce.core.pubsub.RedisPubSubListener<java.lang.String,java.lang.String>
message
in class io.lettuce.core.pubsub.RedisPubSubAdapter<java.lang.String,java.lang.String>
public io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession newSession()
newSession
in interface io.micronaut.session.SessionStore<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>
public java.util.concurrent.CompletableFuture<java.util.Optional<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>> findSession(java.lang.String id)
findSession
in interface io.micronaut.session.SessionStore<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>
public java.util.concurrent.CompletableFuture<java.lang.Boolean> deleteSession(java.lang.String id)
deleteSession
in interface io.micronaut.session.SessionStore<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>
public java.util.concurrent.CompletableFuture<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession> save(io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession session)
save
in interface io.micronaut.session.SessionStore<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>
@PreDestroy public void close()
close
in interface java.lang.AutoCloseable