Class CachingSessionDAO

  • All Implemented Interfaces:
    org.apache.shiro.cache.CacheManagerAware, SessionDAO
    Direct Known Subclasses:
    EnterpriseCacheSessionDAO

    public abstract class CachingSessionDAO
    extends AbstractSessionDAO
    implements org.apache.shiro.cache.CacheManagerAware
    An CachingSessionDAO is a SessionDAO that provides a transparent caching layer between the components that use it and the underlying EIS (Enterprise Information System) session backing store (for example, filesystem, database, enterprise grid/cloud, etc).

    This implementation caches all active sessions in a configured activeSessionsCache. This property is null by default and if one is not explicitly set, a cacheManager is expected to be configured which will in turn be used to acquire the Cache instance to use for the activeSessionsCache.

    All SessionDAO methods are implemented by this class to employ caching behavior and delegates the actual EIS operations to respective do* methods to be implemented by subclasses (doCreate, doRead, etc).
    Since:
    0.2
    • Field Detail

      • ACTIVE_SESSION_CACHE_NAME

        public static final String ACTIVE_SESSION_CACHE_NAME
        The default active sessions cache name, equal to shiro-activeSessionCache.
        See Also:
        Constant Field Values
    • Constructor Detail

      • CachingSessionDAO

        public CachingSessionDAO()
        Default no-arg constructor.
    • Method Detail

      • setCacheManager

        public void setCacheManager(org.apache.shiro.cache.CacheManager cacheManager)
        Sets the cacheManager to use for acquiring the activeSessionsCache if one is not configured.
        Specified by:
        setCacheManager in interface org.apache.shiro.cache.CacheManagerAware
        Parameters:
        cacheManager - the manager to use for constructing the session cache.
      • getCacheManager

        public org.apache.shiro.cache.CacheManager getCacheManager()
        Returns the CacheManager to use for acquiring the activeSessionsCache if one is not configured. That is, the CacheManager will only be used if the activeSessionsCache property is null.
        Returns:
        the CacheManager used by the implementation that creates the activeSessions Cache.
      • setActiveSessionsCacheName

        public void setActiveSessionsCacheName(String activeSessionsCacheName)
        Sets the name of the active sessions cache to be returned by the CacheManager. Defaults to ACTIVE_SESSION_CACHE_NAME.
        Parameters:
        activeSessionsCacheName - the name of the active sessions cache to be returned by the CacheManager.
      • getActiveSessionsCache

        public org.apache.shiro.cache.Cache<Serializable,Session> getActiveSessionsCache()
        Returns the cache instance to use for storing active sessions. If one is not available (it is null), it will be acquired from the configured CacheManager using the activeSessionsCacheName.
        Returns:
        the cache instance to use for storing active sessions or null if the Cache instance should be retrieved from the
      • setActiveSessionsCache

        public void setActiveSessionsCache(org.apache.shiro.cache.Cache<Serializable,Session> cache)
        Sets the cache instance to use for storing active sessions. If one is not set (it remains null), it will be acquired from the configured CacheManager using the activeSessionsCacheName.
        Parameters:
        cache - the cache instance to use for storing active sessions or null if the cache is to be acquired from the configured CacheManager.
      • createActiveSessionsCache

        protected org.apache.shiro.cache.Cache<Serializable,Session> createActiveSessionsCache()
        Creates a cache instance used to store active sessions. Creation is done by first acquiring the CacheManager. If the cache manager is not null, the cache returned is that resulting from the following call:
               String name = getActiveSessionsCacheName();
         cacheManager.getCache(name);
        Returns:
        a cache instance used to store active sessions, or null if the CacheManager has not been set.
      • create

        public Serializable create(Session session)
        Calls super.create(session), then caches the session keyed by the returned sessionId, and then returns this sessionId.
        Specified by:
        create in interface SessionDAO
        Overrides:
        create in class AbstractSessionDAO
        Parameters:
        session - Session object to create in the EIS and then cache.
        Returns:
        the EIS id (e.g. primary key) of the created Session object.
      • getCachedSession

        protected Session getCachedSession(Serializable sessionId)
        Returns the cached session with the corresponding sessionId or null if there is no session cached under that id (or if there is no Cache).
        Parameters:
        sessionId - the id of the cached session to acquire.
        Returns:
        the cached session with the corresponding sessionId, or null if the session does not exist or is not cached.
      • getCachedSession

        protected Session getCachedSession(Serializable sessionId,
                                           org.apache.shiro.cache.Cache<Serializable,Session> cache)
        Returns the Session with the specified id from the specified cache. This method simply calls cache.get(sessionId) and can be overridden by subclasses for custom acquisition behavior.
        Parameters:
        sessionId - the id of the session to acquire.
        cache - the cache to acquire the session from
        Returns:
        the cached session, or null if the session wasn't in the cache.
      • cache

        protected void cache(Session session,
                             Serializable sessionId)
        Caches the specified session under the cache entry key of sessionId.
        Parameters:
        session - the session to cache
        sessionId - the session id, to be used as the cache entry key.
        Since:
        1.0
      • cache

        protected void cache(Session session,
                             Serializable sessionId,
                             org.apache.shiro.cache.Cache<Serializable,Session> cache)
        Caches the specified session in the given cache under the key of sessionId. This implementation simply calls cache.put(sessionId,session) and can be overridden for custom behavior.
        Parameters:
        session - the session to cache
        sessionId - the id of the session, expected to be the cache key.
        cache - the cache to store the session
      • readSession

        public Session readSession(Serializable sessionId)
                            throws UnknownSessionException
        Attempts to acquire the Session from the cache first using the session ID as the cache key. If no session is found, super.readSession(sessionId) is called to perform the actual retrieval.
        Specified by:
        readSession in interface SessionDAO
        Overrides:
        readSession in class AbstractSessionDAO
        Parameters:
        sessionId - the id of the session to retrieve from the EIS.
        Returns:
        the session identified by sessionId in the EIS.
        Throws:
        UnknownSessionException - if the id specified does not correspond to any session in the cache or EIS.
      • doUpdate

        protected abstract void doUpdate(Session session)
        Subclass implementation hook to actually persist the Session's state to the underlying EIS.
        Parameters:
        session - the session object whose state will be propagated to the EIS.
      • delete

        public void delete(Session session)
        Removes the specified session from any cache and then permanently deletes the session from the EIS by delegating to doDelete(org.apache.shiro.session.Session).
        Specified by:
        delete in interface SessionDAO
        Parameters:
        session - the session to remove from caches and permanently delete from the EIS.
      • doDelete

        protected abstract void doDelete(Session session)
        Subclass implementation hook to permanently delete the given Session from the underlying EIS.
        Parameters:
        session - the session instance to permanently delete from the EIS.
      • uncache

        protected void uncache(Session session)
        Removes the specified Session from the cache.
        Parameters:
        session - the session to remove from the cache.
      • getActiveSessions

        public Collection<Session> getActiveSessions()
        Returns all active sessions in the system.

        This implementation merely returns the sessions found in the activeSessions cache. Subclass implementations may wish to override this method to retrieve them in a different way, perhaps by an RDBMS query or by other means.

        Specified by:
        getActiveSessions in interface SessionDAO
        Returns:
        the sessions found in the activeSessions cache.