Class WebSessionKeyCache<T>
- Type Parameters:
T- the session key type
The session infos are held via soft references, so the cache cannot cause a memory leak: if the
garbage collector reclaims a session info, the lookup reports a miss and the user must authenticate again.
Entries with cleared references are removed on lookup, during getSessionKeys(SessionInfo) and periodically
by a cleanup thread. Each eviction is conditional on the reference seen, so that a concurrent re-login
under the same key is never lost.
The cache is thread-safe. Session keys must not be null.
- Author:
- harald
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddSessionInfo(T sessionKey, org.tentackle.session.SessionInfo sessionInfo) Adds a mapping between a web session key and a session info.
This is usually done in the login controller.org.tentackle.session.SessionInfogetSessionInfo(T sessionKey) Gets the session info.getSessionKeys(org.tentackle.session.SessionInfo sessionInfo) Gets the session keys for a user session info.booleanReturns whether the session key cache has been started.
Only a started cache can beterminate()d.voidremoveSessionInfo(T sessionKey) Removes a mapping between a web session key and a session info.
This is usually done in the logout controller.voidstartup(long cleanupInterval) Starts the session key cache.voidTerminates the session cache.
-
Constructor Details
-
WebSessionKeyCache
public WebSessionKeyCache()Creates a session key cache.
-
-
Method Details
-
startup
public void startup(long cleanupInterval) Starts the session key cache.This will start a daemon thread that periodically evicts the entries whose session info has been reclaimed by the garbage collector, such as the ones of web sessions that were never logged out.
- Parameters:
cleanupInterval- interval in [ms] to run cleanup- Throws:
IllegalArgumentException- if the interval is not positiveorg.tentackle.common.TentackleRuntimeException- if the cleanup thread is already running
-
isRunning
public boolean isRunning()Returns whether the session key cache has been started.
Only a started cache can beterminate()d.- Returns:
- true if
startup(long)has been invoked andterminate()not yet
-
terminate
public void terminate()Terminates the session cache.Stops the cleanup thread. The mappings are kept, and the cache may be started again.
- Throws:
org.tentackle.common.TentackleRuntimeException- if the cache has not been started, or the cleanup thread died
-
addSessionInfo
Adds a mapping between a web session key and a session info.
This is usually done in the login controller. If a session with that key already exists, the session info will be replaced.- Parameters:
sessionKey- the (unique) session keysessionInfo- the user's session info
-
removeSessionInfo
Removes a mapping between a web session key and a session info.
This is usually done in the logout controller. If there is no such session, the method will do nothing.- Parameters:
sessionKey- the (unique) session key
-
getSessionInfo
Gets the session info.- Parameters:
sessionKey- the session key- Returns:
- the session info, null if no such key or the session info has been reclaimed
-
getSessionKeys
Gets the session keys for a user session info.The session infos are compared by
SessionInfo.equals, i.e., by username for the default implementation. Entries whose session info has been reclaimed are removed along the way.- Parameters:
sessionInfo- the session info- Returns:
- the session keys, never null, independent of the cache
-