Class RedisSessionStore

java.lang.Object
io.lettuce.core.pubsub.RedisPubSubAdapter<String,String>
io.micronaut.configuration.lettuce.session.RedisSessionStore
All Implemented Interfaces:
io.lettuce.core.pubsub.RedisPubSubListener<String,String>, io.micronaut.session.SessionStore<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>, AutoCloseable

@Singleton @Primary @Requires(property="micronaut.session.http.redis.enabled", value="true") @Replaces(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>, 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 Details

  • 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 - sessionIdGenerator
      sessionConfiguration - sessionConfiguration
      beanLocator - beanLocator
      defaultSerializer - The default value serializer
      conversionService - The conversion service
      scheduledExecutorService - scheduledExecutorService
      eventPublisher - eventPublisher
      Since:
      6.0.0
  • Method Details

    • getValueSerializer

      public io.micronaut.core.serialize.ObjectSerializer getValueSerializer()
      Getter.
      Returns:
      ObjectSerializer
    • message

      public void message(String channel, String message)
      Specified by:
      message in interface io.lettuce.core.pubsub.RedisPubSubListener<String,String>
      Overrides:
      message in class io.lettuce.core.pubsub.RedisPubSubAdapter<String,String>
    • message

      public void message(String pattern, String channel, String message)
      Specified by:
      message in interface io.lettuce.core.pubsub.RedisPubSubListener<String,String>
      Overrides:
      message in class io.lettuce.core.pubsub.RedisPubSubAdapter<String,String>
    • newSession

      public io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession newSession()
      Specified by:
      newSession in interface io.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 interface io.micronaut.session.SessionStore<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>
    • deleteSession

      public CompletableFuture<Boolean> deleteSession(String id)
      Specified by:
      deleteSession in interface io.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 interface io.micronaut.session.SessionStore<io.micronaut.configuration.lettuce.session.RedisSessionStore.RedisSession>
    • close

      @PreDestroy public void close()
      Specified by:
      close in interface AutoCloseable