Class CachingSessionDAO
java.lang.Object
org.apache.shiro.session.mgt.eis.AbstractSessionDAO
org.apache.shiro.session.mgt.eis.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 Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe default active sessions cache name, equal toshiro-activeSessionCache. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcache(Session session, Serializable sessionId) Caches the specified session under the cache entry key ofsessionId.protected voidcache(Session session, Serializable sessionId, org.apache.shiro.cache.Cache<Serializable, Session> cache) Caches the specified session in the given cache under the key ofsessionId.Callssuper.create(session), then caches the session keyed by the returnedsessionId, and then returns thissessionId.protected org.apache.shiro.cache.Cache<Serializable, Session> Creates a cache instance used to store active sessions.voidRemoves the specified session from any cache and then permanently deletes the session from the EIS by delegating todoDelete(org.apache.shiro.session.Session).protected abstract voidSubclass implementation hook to permanently delete the given Session from the underlying EIS.protected abstract voidSubclass implementation hook to actually persist theSession's state to the underlying EIS.Returns all active sessions in the system.org.apache.shiro.cache.Cache<Serializable, Session> Returns the cache instance to use for storing active sessions.Returns the name of the actives sessions cache to be returned by theCacheManager.protected SessiongetCachedSession(Serializable sessionId) Returns the cached session with the correspondingsessionIdornullif there is no session cached under that id (or if there is no Cache).protected SessiongetCachedSession(Serializable sessionId, org.apache.shiro.cache.Cache<Serializable, Session> cache) Returns the Session with the specified id from the specified cache.org.apache.shiro.cache.CacheManagerReturns the CacheManager to use for acquiring theactiveSessionsCacheif one is not configured.readSession(Serializable sessionId) Attempts to acquire the Session from the cache first using the session ID as the cache key.voidsetActiveSessionsCache(org.apache.shiro.cache.Cache<Serializable, Session> cache) Sets the cache instance to use for storing active sessions.voidsetActiveSessionsCacheName(String activeSessionsCacheName) Sets the name of the active sessions cache to be returned by theCacheManager.voidsetCacheManager(org.apache.shiro.cache.CacheManager cacheManager) Sets the cacheManager to use for acquiring theactiveSessionsCacheif one is not configured.protected voidRemoves the specified Session from the cache.voidUpdates the state of the given session to the EIS by first delegating todoUpdate(org.apache.shiro.session.Session).Methods inherited from class org.apache.shiro.session.mgt.eis.AbstractSessionDAO
assignSessionId, doCreate, doReadSession, generateSessionId, getSessionIdGenerator, setSessionIdGenerator
-
Field Details
-
ACTIVE_SESSION_CACHE_NAME
The default active sessions cache name, equal toshiro-activeSessionCache.- See Also:
-
-
Constructor Details
-
CachingSessionDAO
public CachingSessionDAO()Default no-arg constructor.
-
-
Method Details
-
setCacheManager
Sets the cacheManager to use for acquiring theactiveSessionsCacheif one is not configured.- Specified by:
setCacheManagerin interfaceorg.apache.shiro.cache.CacheManagerAware- Parameters:
cacheManager- the manager to use for constructing the session cache.
-
getCacheManager
Returns the CacheManager to use for acquiring theactiveSessionsCacheif one is not configured. That is, theCacheManagerwill only be used if theactiveSessionsCacheproperty isnull.- Returns:
- the CacheManager used by the implementation that creates the activeSessions Cache.
-
getActiveSessionsCacheName
Returns the name of the actives sessions cache to be returned by theCacheManager. Unless overridden bysetActiveSessionsCacheName(String), defaults toACTIVE_SESSION_CACHE_NAME.- Returns:
- the name of the active sessions cache.
-
setActiveSessionsCacheName
Sets the name of the active sessions cache to be returned by theCacheManager. Defaults toACTIVE_SESSION_CACHE_NAME.- Parameters:
activeSessionsCacheName- the name of the active sessions cache to be returned by theCacheManager.
-
getActiveSessionsCache
Returns the cache instance to use for storing active sessions. If one is not available (it isnull), it will beacquiredfrom theconfiguredCacheManagerusing theactiveSessionsCacheName.- Returns:
- the cache instance to use for storing active sessions or
nullif theCacheinstance should be retrieved from the
-
setActiveSessionsCache
Sets the cache instance to use for storing active sessions. If one is not set (it remainsnull), it will beacquiredfrom theconfiguredCacheManagerusing theactiveSessionsCacheName.- Parameters:
cache- the cache instance to use for storing active sessions ornullif the cache is to be acquired from theconfiguredCacheManager.
-
createActiveSessionsCache
Creates a cache instance used to store active sessions. Creation is done by firstacquiringtheCacheManager. 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
nullif theCacheManagerhas not been set.
-
create
Callssuper.create(session), then caches the session keyed by the returnedsessionId, and then returns thissessionId.- Specified by:
createin interfaceSessionDAO- Overrides:
createin classAbstractSessionDAO- Parameters:
session- Session object to create in the EIS and then cache.- Returns:
- the EIS id (e.g. primary key) of the created
Sessionobject.
-
getCachedSession
Returns the cached session with the correspondingsessionIdornullif 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, ornullif 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 callscache.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
nullif the session wasn't in the cache.
-
cache
Caches the specified session under the cache entry key ofsessionId.- Parameters:
session- the session to cachesessionId- 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 ofsessionId. This implementation simply callscache.put(sessionId,session)and can be overridden for custom behavior.- Parameters:
session- the session to cachesessionId- the id of the session, expected to be the cache key.cache- the cache to store the session
-
readSession
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:
readSessionin interfaceSessionDAO- Overrides:
readSessionin classAbstractSessionDAO- Parameters:
sessionId- the id of the session to retrieve from the EIS.- Returns:
- the session identified by
sessionIdin the EIS. - Throws:
UnknownSessionException- if the id specified does not correspond to any session in the cache or EIS.
-
update
Updates the state of the given session to the EIS by first delegating todoUpdate(org.apache.shiro.session.Session). If the session is aValidatingSession, it will be added to the cache only if it isValidatingSession.isValid()and if invalid, will be removed from the cache. If it is not aValidatingSessioninstance, it will be added to the cache in any event.- Specified by:
updatein interfaceSessionDAO- Parameters:
session- the session object to update in the EIS.- Throws:
UnknownSessionException- if no existing EIS session record exists with the identifier ofsession.getId()
-
doUpdate
-
delete
Removes the specified session from any cache and then permanently deletes the session from the EIS by delegating todoDelete(org.apache.shiro.session.Session).- Specified by:
deletein interfaceSessionDAO- Parameters:
session- the session to remove from caches and permanently delete from the EIS.
-
doDelete
-
uncache
-
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:
getActiveSessionsin interfaceSessionDAO- Returns:
- the sessions found in the activeSessions cache.
-