@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<String,String> implements io.micronaut.session.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(io.micronaut.session.SessionIdGenerator sessionIdGenerator,
RedisHttpSessionConfiguration sessionConfiguration,
io.micronaut.context.BeanLocator beanLocator,
io.micronaut.core.serialize.ObjectSerializer defaultSerializer,
ExecutorService scheduledExecutorService,
io.micronaut.context.event.ApplicationEventPublisher eventPublisher)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
CompletableFuture<Boolean> |
deleteSession(String id) |
CompletableFuture<Optional<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>> |
findSession(String id) |
io.micronaut.core.serialize.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() |
CompletableFuture<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession> |
save(io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession session) |
public static final 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") 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 io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession newSession()
newSession
in interface io.micronaut.session.SessionStore<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>
public CompletableFuture<Optional<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>> findSession(String id)
findSession
in interface io.micronaut.session.SessionStore<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>
public CompletableFuture<Boolean> deleteSession(String id)
deleteSession
in interface io.micronaut.session.SessionStore<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>
public 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>