@Singleton @Primary @Requires(property="micronaut.session.http.redis.enabled", value="true") @Replaces(value=InMemorySessionStore.class) public class RedisSessionStore extends io.lettuce.core.pubsub.RedisPubSubAdapter<String,String> implements SessionStore<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>
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 String |
REDIS_SESSION_ENABLED |
Constructor and Description |
---|
RedisSessionStore(SessionIdGenerator sessionIdGenerator,
RedisHttpSessionConfiguration sessionConfiguration,
BeanLocator beanLocator,
ObjectSerializer defaultSerializer,
ExecutorService scheduledExecutorService,
ApplicationEventPublisher eventPublisher)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
CompletableFuture<Boolean> |
deleteSession(String id)
Delete a session for the given ID.
|
CompletableFuture<Optional<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>> |
findSession(String id)
Find a session for the given ID.
|
ObjectSerializer |
getValueSerializer()
Getter.
|
void |
message(String channel,
String message) |
void |
message(String pattern,
String channel,
String message) |
io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession |
newSession()
Create a new unsaved session.
|
CompletableFuture<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession> |
save(io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession session)
Save the given session.
|
public static final String REDIS_SESSION_ENABLED
public RedisSessionStore(SessionIdGenerator sessionIdGenerator, RedisHttpSessionConfiguration sessionConfiguration, BeanLocator beanLocator, ObjectSerializer defaultSerializer, @Named(value="scheduled") ExecutorService scheduledExecutorService, ApplicationEventPublisher eventPublisher)
sessionIdGenerator
- sessionIdGeneratorsessionConfiguration
- sessionConfigurationbeanLocator
- beanLocatordefaultSerializer
- The default value serializerscheduledExecutorService
- scheduledExecutorServiceeventPublisher
- eventPublisherpublic ObjectSerializer getValueSerializer()
public io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession newSession()
SessionStore
newSession
in interface SessionStore<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>
public CompletableFuture<Optional<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>> findSession(String id)
SessionStore
findSession
in interface SessionStore<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>
id
- The ID of the sessionOptional
sessionpublic CompletableFuture<Boolean> deleteSession(String id)
SessionStore
deleteSession
in interface SessionStore<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>
id
- The ID of the sessionpublic CompletableFuture<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession> save(io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession session)
SessionStore
save
in interface SessionStore<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>
session
- The session to save