Class InMemorySessionStore
- All Implemented Interfaces:
SessionStore
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuildsInMemorySessionStores; used during configuration, not thread-safe. -
Method Summary
Modifier and TypeMethodDescriptionstatic InMemorySessionStore.Builderbuilder()Starts a builder with 30 minutes idle, 8 hours absolute and 10,000 sessions.booleancreate(String id, SessionState state) Stores a new session.Returns a live session and counts the call as an access (the idle timeout restarts).voidDeletes a session; unknown identifiers are ignored.booleanAtomically gives a live session a new identifier; the old one stops working at once.intsize()Returns the number of sessions held, expired ones included until they are noticed.toString()booleanupdate(String id, UnaryOperator<SessionState> change) Atomically replaces a live session's state with the function's result.
-
Method Details
-
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
Description copied from interface:SessionStoreStores a new session.- Specified by:
createin interfaceSessionStore- Parameters:
id- the identifierstate- the initial state- Returns:
- false if the identifier is already in use (the caller picks another)
-
find
Description copied from interface:SessionStoreReturns a live session and counts the call as an access (the idle timeout restarts).- Specified by:
findin interfaceSessionStore- Parameters:
id- the identifier- Returns:
- the state with the new access time, or empty when unknown or expired
-
update
Description copied from interface:SessionStoreAtomically replaces a live session's state with the function's result.- Specified by:
updatein interfaceSessionStore- Parameters:
id- the identifierchange- 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
Description copied from interface:SessionStoreAtomically gives a live session a new identifier; the old one stops working at once.- Specified by:
renamein interfaceSessionStore- Parameters:
id- the current identifiernewId- the new identifier- Returns:
- false when the session is unknown or expired, or the new identifier is in use
-
remove
Description copied from interface:SessionStoreDeletes a session; unknown identifiers are ignored.- Specified by:
removein interfaceSessionStore- Parameters:
id- the identifier
-
toString
-