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 isnull
by default and if one is not explicitly set, acacheManager
is expected to be configured which will in turn be used to acquire theCache
instance to use for theactiveSessionsCache
. AllSessionDAO
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
Fields Modifier and Type Field Description static String
ACTIVE_SESSION_CACHE_NAME
The 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 void
cache(Session session, Serializable sessionId)
Caches the specified session under the cache entry key ofsessionId
.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
.Serializable
create(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.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)
.protected abstract void
doDelete(Session session)
Subclass implementation hook to permanently delete the given Session from the underlying EIS.protected abstract void
doUpdate(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.String
getActiveSessionsCacheName()
Returns the name of the actives sessions cache to be returned by theCacheManager
.protected Session
getCachedSession(Serializable sessionId)
Returns the cached session with the correspondingsessionId
ornull
if there is no session cached under that id (or if there is no Cache).protected Session
getCachedSession(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.CacheManager
getCacheManager()
Returns the CacheManager to use for acquiring theactiveSessionsCache
if one is not configured.Session
readSession(Serializable sessionId)
Attempts to acquire the Session from the cache first using the session ID as the cache key.void
setActiveSessionsCache(org.apache.shiro.cache.Cache<Serializable,Session> cache)
Sets the cache instance to use for storing active sessions.void
setActiveSessionsCacheName(String activeSessionsCacheName)
Sets the name of the active sessions cache to be returned by theCacheManager
.void
setCacheManager(org.apache.shiro.cache.CacheManager cacheManager)
Sets the cacheManager to use for acquiring theactiveSessionsCache
if one is not configured.protected void
uncache(Session session)
Removes the specified Session from the cache.void
update(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 theactiveSessionsCache
if one is not configured.- Specified by:
setCacheManager
in 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 theactiveSessionsCache
if one is not configured. That is, theCacheManager
will only be used if theactiveSessionsCache
property 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 beacquired
from theconfigured
CacheManager
using theactiveSessionsCacheName
.- Returns:
- the cache instance to use for storing active sessions or
null
if theCache
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 remainsnull
), it will beacquired
from theconfigured
CacheManager
using theactiveSessionsCacheName
.- Parameters:
cache
- the cache instance to use for storing active sessions ornull
if the cache is to be acquired from theconfigured
CacheManager
.
-
createActiveSessionsCache
protected org.apache.shiro.cache.Cache<Serializable,Session> createActiveSessionsCache()
Creates a cache instance used to store active sessions. Creation is done by firstacquiring
theCacheManager
. 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 theCacheManager
has 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:
create
in interfaceSessionDAO
- Overrides:
create
in classAbstractSessionDAO
- 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 correspondingsessionId
ornull
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
, ornull
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 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
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 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:
readSession
in interfaceSessionDAO
- Overrides:
readSession
in classAbstractSessionDAO
- 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.
-
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 aValidatingSession
instance, it will be added to the cache in any event.- Specified by:
update
in 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:
delete
in 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:
getActiveSessions
in interfaceSessionDAO
- Returns:
- the sessions found in the activeSessions cache.
-
-