Class AbstractSessionCache

java.lang.Object
com.aspectran.core.component.AbstractComponent
com.aspectran.core.component.session.AbstractSessionCache
All Implemented Interfaces:
Component, SessionCache
Direct Known Subclasses:
DefaultSessionCache

public abstract class AbstractSessionCache extends AbstractComponent implements SessionCache
A base implementation of the SessionCache interface for managing a set of Session objects pertaining to a context in memory.

Created: 2017. 6. 24.

  • Constructor Details

    • AbstractSessionCache

      public AbstractSessionCache(SessionHandler sessionHandler, SessionStore sessionStore, boolean clusterEnabled)
  • Method Details

    • getSessionHandler

      protected SessionHandler getSessionHandler()
    • getSessionStore

      protected SessionStore getSessionStore()
    • getStatistics

      protected SessionStatistics getStatistics()
    • isClusterEnabled

      public boolean isClusterEnabled()
      Specified by:
      isClusterEnabled in interface SessionCache
    • getEvictionIdleSecs

      public int getEvictionIdleSecs()
      Specified by:
      getEvictionIdleSecs in interface SessionCache
      Returns:
      the interval in seconds to evict inactive sessions from cache
    • setEvictionIdleSecs

      public void setEvictionIdleSecs(int evictionTimeout)
      -1 means we never evict inactive sessions. 0 means we evict a session after the last request for it exits. >0 is the number of seconds after which we evict inactive sessions from the cache.
      Specified by:
      setEvictionIdleSecs in interface SessionCache
      Parameters:
      evictionTimeout - -1 is never evict; 0 is evict-on-exit; and any other positive value is the time in seconds that a session can be idle before it can be evicted.
    • isSaveOnCreate

      public boolean isSaveOnCreate()
      Specified by:
      isSaveOnCreate in interface SessionCache
      Returns:
      if true the newly created session will be saved immediately
    • setSaveOnCreate

      public void setSaveOnCreate(boolean saveOnCreate)
      Description copied from interface: SessionCache
      Whether a session that is newly created should be immediately saved. If false, a session that is created and invalidated within a single request is never persisted.
      Specified by:
      setSaveOnCreate in interface SessionCache
      Parameters:
      saveOnCreate - if true, immediately save the newly created session
    • isSaveOnInactiveEviction

      public boolean isSaveOnInactiveEviction()
      Whether we should save a session that has been inactive before we boot it from the cache.
      Specified by:
      isSaveOnInactiveEviction in interface SessionCache
      Returns:
      true if an inactive session will be saved before being evicted
    • setSaveOnInactiveEviction

      public void setSaveOnInactiveEviction(boolean saveOnEvict)
      Description copied from interface: SessionCache
      Whether a session that is about to be evicted should be saved before being evicted.
      Specified by:
      setSaveOnInactiveEviction in interface SessionCache
      Parameters:
      saveOnEvict - if true, save the session before eviction
    • isRemoveUnloadableSessions

      public boolean isRemoveUnloadableSessions()
      Specified by:
      isRemoveUnloadableSessions in interface SessionCache
      Returns:
      true if sessions that can't be loaded are deleted from the store
    • setRemoveUnloadableSessions

      public void setRemoveUnloadableSessions(boolean removeUnloadableSessions)
      If a session's data cannot be loaded from the store without error, remove it from the persistent store.
      Specified by:
      setRemoveUnloadableSessions in interface SessionCache
      Parameters:
      removeUnloadableSessions - whether to delete sessions that can not be loaded
    • get

      public DefaultSession get(String id) throws Exception
      Description copied from interface: SessionCache
      Get an existing Session. If necessary, the cache will load the data for the session from the configured SessionStore.
      Specified by:
      get in interface SessionCache
      Parameters:
      id - the session id
      Returns:
      the Session if one exists, null otherwise
      Throws:
      Exception - if an error occurs
    • add

      public DefaultSession add(String id, long time, long maxInactiveInterval) throws Exception
      Description copied from interface: SessionCache
      Add an entirely new session to the cache.
      Specified by:
      add in interface SessionCache
      Parameters:
      id - the session id
      time - the timestamp of the session creation
      maxInactiveInterval - the max inactive time in milliseconds
      Throws:
      Exception
    • release

      public void release(String id, DefaultSession session) throws Exception
      Description copied from interface: SessionCache
      Finish using a Session. This is called by the SessionHandler once a request is finished with a Session. SessionCache implementations may want to delay writing out Session contents until the last request exits a Session.
      Specified by:
      release in interface SessionCache
      Parameters:
      id - the session id
      session - the session object
      Throws:
      Exception - if an error occurs
    • exists

      public boolean exists(String id) throws Exception
      Description copied from interface: SessionCache
      Check to see if a session exists: WILL consult the SessionStore.
      Specified by:
      exists in interface SessionCache
      Parameters:
      id - the session id
      Returns:
      true if the session exists; false otherwise
      Throws:
      Exception - if an error occurs
    • contains

      public boolean contains(String id) throws Exception
      Description copied from interface: SessionCache
      Check to see if a Session is in the cache. Does NOT consult the SessionStore.
      Specified by:
      contains in interface SessionCache
      Parameters:
      id - the session id
      Returns:
      true if a Session object matching the id is present in the cache; false otherwise
      Throws:
      Exception - if an error occurs
    • delete

      public DefaultSession delete(String id) throws Exception
      Description copied from interface: SessionCache
      Remove a Session completely: from both this cache and the SessionStore.
      Specified by:
      delete in interface SessionCache
      Parameters:
      id - the session id
      Returns:
      the Session that was removed, null otherwise
      Throws:
      Exception - if an error occurs when deleting a session
    • doGet

      protected abstract DefaultSession doGet(String id)
      Get the session matching the key.
      Parameters:
      id - the session id
      Returns:
      the Session object matching the id
    • doPutIfAbsent

      protected abstract DefaultSession doPutIfAbsent(String id, DefaultSession session)
      Put the session into the map if it wasn't already there.
      Parameters:
      id - the identity of the session
      session - the session object
      Returns:
      null if the session wasn't already in the map, or the existing entry otherwise
    • doComputeIfAbsent

      protected abstract DefaultSession doComputeIfAbsent(String id, Function<String,DefaultSession> mappingFunction)
      Compute the mappingFunction to create a Session object if the session with the given id isn't already in the map, otherwise return the existing Session. This method is expected to have precisely the same behaviour as ConcurrentHashMap.computeIfAbsent(K, java.util.function.Function<? super K, ? extends V>)
      Parameters:
      id - the session id
      mappingFunction - the function to load the data for the session
      Returns:
      an existing Session from the cache
    • doReplace

      protected abstract boolean doReplace(String id, DefaultSession oldValue, DefaultSession newValue)
      Replace the mapping from id to oldValue with newValue.
      Parameters:
      id - the session id
      oldValue - the old value
      newValue - the new value
      Returns:
      true if replacement was done
    • doDelete

      protected abstract DefaultSession doDelete(String id)
      Remove the session with this identity from the store.
      Parameters:
      id - the session id
      Returns:
      the Session object if removed; null otherwise
    • renewSessionId

      public DefaultSession renewSessionId(String oldId, String newId) throws Exception
      Description copied from interface: SessionCache
      Change the id of a Session.
      Specified by:
      renewSessionId in interface SessionCache
      Parameters:
      oldId - the current session id
      newId - the new session id
      Returns:
      the Session after changing its id
      Throws:
      Exception - if any error occurred
    • renewSessionId

      protected void renewSessionId(DefaultSession session, String newId) throws Exception
      Swap the id on a session.
      Parameters:
      session - the session for which to do the swap
      newId - the new id
      Throws:
      Exception - if there was a failure saving the change
    • checkExpiration

      public Set<String> checkExpiration(Set<String> candidates)
      Description copied from interface: SessionCache
      Check a list of session ids that belong to potentially expired sessions. The Session in the cache should be checked, but also the SessionStore, as that is the authoritative source of all session information.
      Specified by:
      checkExpiration in interface SessionCache
      Parameters:
      candidates - the session ids to check
      Returns:
      the set of session ids that have actually expired: this can be a superset of the original candidate list.
    • checkInactiveSession

      public boolean checkInactiveSession(DefaultSession session)
      Check a session for being inactive and thus being able to be evicted, if eviction is enabled.
      Specified by:
      checkInactiveSession in interface SessionCache
      Parameters:
      session - the session to check
      Returns:
      true if evicted session, false otherwise
    • cleanOrphans

      public void cleanOrphans(long time)
      Description copied from interface: SessionCache
      Remove all unmanaged sessions that expired at or before the given time.
      Specified by:
      cleanOrphans in interface SessionCache
      Parameters:
      time - the time before which the sessions must have expired