Class InMemorySessionStore

java.lang.Object
com.jsgalactic.axiom.security.InMemorySessionStore
All Implemented Interfaces:
SessionStore

public final class InMemorySessionStore extends Object implements SessionStore
A SessionStore in process memory with idle and absolute timeouts and a hard bound on the number of sessions.

 var store = InMemorySessionStore.builder()
         .idleTimeout(Duration.ofMinutes(30)).absoluteTimeout(Duration.ofHours(8)).maxSessions(50_000).build();
 

Bounds. At most maxSessions (10,000 by default) sessions exist. A new session at capacity first drops expired sessions, then the least recently used anonymous session (no identity), and only when there is none the least recently used authenticated one, so a flood of anonymous sessions cannot push signed-in users out while anonymous sessions remain to evict. An evicted user simply has to sign in again.

Timeouts. A session expires when it has been idle for idleTimeout (30 minutes by default) or lives longer than absoluteTimeout (8 hours), whichever comes first; expired sessions are never returned. A clock that moves backwards never expires a session early.

Storage. Entries are keyed by the SHA-256 digest of the identifier, so a heap dump of the map keys does not reveal usable identifiers. Sessions do not survive a restart and are not shared between instances; use another SessionStore for that.

Metrics, when configured: gauge axiom.security.sessions, counter axiom.security.sessions.evictions (tag reason = expired or capacity). Thread-safe; nothing to close.

  • Method Details

    • builder

      public static InMemorySessionStore.Builder builder()
      Starts a builder with 30 minutes idle, 8 hours absolute and 10,000 sessions.
      Returns:
      a builder
    • size

      public int size()
      Returns the number of sessions held, expired ones included until they are noticed.
      Returns:
      the count
    • create

      public boolean create(String id, SessionState state)
      Description copied from interface: SessionStore
      Stores a new session.
      Specified by:
      create in interface SessionStore
      Parameters:
      id - the identifier
      state - the initial state
      Returns:
      false if the identifier is already in use (the caller picks another)
    • find

      public Optional<SessionState> find(String id)
      Description copied from interface: SessionStore
      Returns a live session and counts the call as an access (the idle timeout restarts).
      Specified by:
      find in interface SessionStore
      Parameters:
      id - the identifier
      Returns:
      the state with the new access time, or empty when unknown or expired
    • update

      public boolean update(String id, UnaryOperator<SessionState> change)
      Description copied from interface: SessionStore
      Atomically replaces a live session's state with the function's result.
      Specified by:
      update in interface SessionStore
      Parameters:
      id - the identifier
      change - a pure function from the current state to the new one; may be called repeatedly
      Returns:
      false when the session is unknown or expired, in which case nothing changed
    • rename

      public boolean rename(String id, String newId)
      Description copied from interface: SessionStore
      Atomically gives a live session a new identifier; the old one stops working at once.
      Specified by:
      rename in interface SessionStore
      Parameters:
      id - the current identifier
      newId - the new identifier
      Returns:
      false when the session is unknown or expired, or the new identifier is in use
    • remove

      public void remove(String id)
      Description copied from interface: SessionStore
      Deletes a session; unknown identifiers are ignored.
      Specified by:
      remove in interface SessionStore
      Parameters:
      id - the identifier
    • toString

      public String toString()
      Overrides:
      toString in class Object