Package org.apache.shiro.session.mgt.eis
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 configuredactiveSessionsCache. This property isnullby default and if one is not explicitly set, acacheManageris expected to be configured which will in turn be used to acquire theCacheinstance to use for theactiveSessionsCache. AllSessionDAOmethods 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
Fields Modifier and Type Field Description static StringACTIVE_SESSION_CACHE_NAMEThe default active sessions cache name, equal toshiro-activeSessionCache.
-
Constructor Summary
Constructors Constructor Description CachingSessionDAO()Default no-arg constructor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected 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.Serializablecreate(Session session)Callssuper.create(session), then caches the session keyed by the returnedsessionId, and then returns thissessionId.protected org.apache.shiro.cache.Cache<Serializable,Session>createActiveSessionsCache()Creates a cache instance used to store active sessions.voiddelete(Session session)Removes 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 voiddoDelete(Session session)Subclass implementation hook to permanently delete the given Session from the underlying EIS.protected abstract voiddoUpdate(Session session)Subclass implementation hook to actually persist theSession's state to the underlying EIS.Collection<Session>getActiveSessions()Returns all active sessions in the system.org.apache.shiro.cache.Cache<Serializable,Session>getActiveSessionsCache()Returns the cache instance to use for storing active sessions.StringgetActiveSessionsCacheName()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.CacheManagergetCacheManager()Returns the CacheManager to use for acquiring theactiveSessionsCacheif one is not configured.SessionreadSession(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 voiduncache(Session session)Removes the specified Session from the cache.voidupdate(Session session)Updates 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 Detail
-
ACTIVE_SESSION_CACHE_NAME
public static final String ACTIVE_SESSION_CACHE_NAME
The default active sessions cache name, equal toshiro-activeSessionCache.- See Also:
- Constant Field Values
-
-
Method Detail
-
setCacheManager
public void setCacheManager(org.apache.shiro.cache.CacheManager cacheManager)
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
public org.apache.shiro.cache.CacheManager 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
public String 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
public void setActiveSessionsCacheName(String activeSessionsCacheName)
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
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 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
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 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
protected org.apache.shiro.cache.Cache<Serializable,Session> 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
public Serializable create(Session session)
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
protected Session getCachedSession(Serializable sessionId)
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
protected void cache(Session session, Serializable sessionId)
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
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:
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
public void update(Session session) throws UnknownSessionException
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
protected abstract void doUpdate(Session session)
Subclass implementation hook to actually persist theSession'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 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
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:
getActiveSessionsin interfaceSessionDAO- Returns:
- the sessions found in the activeSessions cache.
-
-