Class WebSessionKeyCache<T>

java.lang.Object
org.tentackle.web.app.WebSessionKeyCache<T>
Type Parameters:
T - the session key type

public class WebSessionKeyCache<T> extends Object
A cache mapping the web framework's session keys to the users' session infos.

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
    Constructor
    Description
    Creates a session key cache.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addSessionInfo(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.SessionInfo
    getSessionInfo(T sessionKey)
    Gets the session info.
    getSessionKeys(org.tentackle.session.SessionInfo sessionInfo)
    Gets the session keys for a user session info.
    boolean
    Returns whether the session key cache has been started.
    Only a started cache can be terminate()d.
    void
    removeSessionInfo(T sessionKey)
    Removes a mapping between a web session key and a session info.
    This is usually done in the logout controller.
    void
    startup(long cleanupInterval)
    Starts the session key cache.
    void
    Terminates the session cache.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 positive
      org.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 be terminate()d.
      Returns:
      true if startup(long) has been invoked and terminate() 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

      public void addSessionInfo(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. If a session with that key already exists, the session info will be replaced.
      Parameters:
      sessionKey - the (unique) session key
      sessionInfo - the user's session info
    • removeSessionInfo

      public void removeSessionInfo(T sessionKey)
      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

      public org.tentackle.session.SessionInfo getSessionInfo(T sessionKey)
      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

      public Collection<T> getSessionKeys(org.tentackle.session.SessionInfo sessionInfo)
      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