/** * Caches the specified session in the given cache under the key of {@code sessionId}. This implementation * simply calls {@code cache.put(sessionId,session)} and can be overridden for custom behavior. * * @param session the session to cache * @param sessionId the id of the session, expected to be the cache key. * @param cache the cache to store the session */ protected void cache(Session session, Serializable sessionId, Cache<Serializable, Session> cache) { cache.put(sessionId, session); }
/** * Returns the Session with the specified id from the specified cache. This method simply calls * {@code cache.get(sessionId)} and can be overridden by subclasses for custom acquisition behavior. * * @param sessionId the id of the session to acquire. * @param cache the cache to acquire the session from * @return the cached session, or {@code null} if the session wasn't in the cache. */ protected Session getCachedSession(Serializable sessionId, Cache<Serializable, Session> cache) { return cache.get(sessionId); }
/** * Sets the internal {@code CacheManager} on the {@code SessionDAO} if it implements the * {@link org.apache.shiro.cache.CacheManagerAware CacheManagerAware} interface. * <p/> * This method is called after setting a cacheManager via the * {@link #setCacheManager(org.apache.shiro.cache.CacheManager) setCacheManager} method <em>em</em> when * setting a {@code SessionDAO} via the {@link #setSessionDAO} method to allow it to be propagated * in either case. * * @since 1.0 */ private void applyCacheManagerToSessionDAO() { if (this.cacheManager != null && this.sessionDAO != null && this.sessionDAO instanceof CacheManagerAware) { ((CacheManagerAware) this.sessionDAO).setCacheManager(this.cacheManager); } }
/** * Removes the specified Session from the cache. * * @param session the session to remove from the cache. */ protected void uncache(Session session) { if (session == null) { return; } Serializable id = session.getId(); if (id == null) { return; } Cache<Serializable, Session> cache = getActiveSessionsCacheLazy(); if (cache != null) { cache.remove(id); } }
private Cache<Object, AuthorizationInfo> getAuthorizationCacheLazy() { if (this.authorizationCache == null) { if (log.isDebugEnabled()) { log.debug("No authorizationCache instance set. Checking for a cacheManager..."); } CacheManager cacheManager = getCacheManager(); if (cacheManager != null) { String cacheName = getAuthorizationCacheName(); if (log.isDebugEnabled()) { log.debug("CacheManager [" + cacheManager + "] has been configured. Building " + "authorization cache named [" + cacheName + "]"); } this.authorizationCache = cacheManager.getCache(cacheName); } else { if (log.isDebugEnabled()) { log.debug("No cache or cacheManager properties have been set. Authorization cache cannot " + "be obtained."); } } } return this.authorizationCache; }
public int size() { try { return cache.getSize(); } catch (Throwable t) { throw new CacheException(t); } }
@Subscribe public void handleUserSave(UserChangedEvent event) { getAuthorizationCache().clear(); } }
@Override protected Cache<Serializable, Session> createCache(String name) throws CacheException { return new MapCache<Serializable, Session>(name, new ConcurrentHashMap<Serializable, Session>()); } });
/** * Returns all active sessions in the system. * <p/> * <p>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. * * @return the sessions found in the activeSessions cache. */ public Collection<Session> getActiveSessions() { Cache<Serializable, Session> cache = getActiveSessionsCacheLazy(); if (cache != null) { return cache.values(); } else { return Collections.emptySet(); } } }
/** * Clears out the AuthorizationInfo cache entry for the specified account. * <p/> * This method is provided as a convenience to subclasses so they can invalidate a cache entry when they * change an account's authorization data (add/remove roles or permissions) during runtime. Because an account's * AuthorizationInfo can be cached, there needs to be a way to invalidate the cache for only that account so that * subsequent authorization operations don't used the (old) cached value if account data changes. * <p/> * After this method is called, the next authorization check for that same account will result in a call to * {@link #getAuthorizationInfo(org.apache.shiro.subject.PrincipalCollection) getAuthorizationInfo}, and the * resulting return value will be cached before being returned so it can be reused for later authorization checks. * <p/> * If you wish to clear out all associated cached data (and not just authorization data), use the * {@link #clearCache(org.apache.shiro.subject.PrincipalCollection)} method instead (which will in turn call this * method by default). * * @param principals the principals of the account for which to clear the cached AuthorizationInfo. */ protected void clearCachedAuthorizationInfo(PrincipalCollection principals) { if (principals == null) { return; } Cache<Object, AuthorizationInfo> cache = getAvailableAuthorizationCache(); //cache instance will be non-null if caching is enabled: if (cache != null) { Object key = getAuthorizationCacheKey(principals); cache.remove(key); } }
/** * Checks to see if the authenticationCache class attribute is null, and if so, attempts to acquire one from * any configured {@link #getCacheManager() cacheManager}. If one is acquired, it is set as the class attribute. * The class attribute is then returned. * * @return an available cache instance to be used for authentication caching or {@code null} if one is not available. * @since 1.2 */ private Cache<Object, AuthenticationInfo> getAuthenticationCacheLazy() { if (this.authenticationCache == null) { log.trace("No authenticationCache instance set. Checking for a cacheManager..."); CacheManager cacheManager = getCacheManager(); if (cacheManager != null) { String cacheName = getAuthenticationCacheName(); log.debug("CacheManager [{}] configured. Building authentication cache '{}'", cacheManager, cacheName); this.authenticationCache = cacheManager.getCache(cacheName); } } return this.authenticationCache; }
/** * Caches the specified info if authentication caching * {@link #isAuthenticationCachingEnabled(org.apache.shiro.authc.AuthenticationToken, org.apache.shiro.authc.AuthenticationInfo) isEnabled} * for the specific token/info pair and a cache instance is available to be used. * * @param token the authentication token submitted which resulted in a successful authentication attempt. * @param info the AuthenticationInfo to cache as a result of the successful authentication attempt. * @since 1.2 */ private void cacheAuthenticationInfoIfPossible(AuthenticationToken token, AuthenticationInfo info) { if (!isAuthenticationCachingEnabled(token, info)) { log.debug("AuthenticationInfo caching is disabled for info [{}]. Submitted token: [{}].", info, token); //return quietly, caching is disabled for this token/info pair: return; } Cache<Object, AuthenticationInfo> cache = getAvailableAuthenticationCache(); if (cache != null) { Object key = getAuthenticationCacheKey(token); cache.put(key, info); log.trace("Cached AuthenticationInfo for continued authentication. key=[{}], value=[{}].", key, info); } }
/** * Returns the size (in bytes) that this EhCache's disk store is consuming or <code>-1</code> if * that number is unknown or cannot be calculated. * * @return the size (in bytes) that this EhCache's disk store is consuming or <code>-1</code> if * that number is unknown or cannot be calculated. */ public long getDiskStoreSize() { try { return cache.getDiskStoreSize(); } catch (Throwable t) { throw new CacheException(t); } }
/** * Returns a {@link MapCache} instance representing the named Hazelcast-managed * {@link com.hazelcast.core.IMap IMap}. The Hazelcast Map is obtained by calling * {@link HazelcastInstance#getMap(String) hazelcastInstance.getMap(name)}. * * @param name the name of the cache to acquire. * @param <K> the type of map key * @param <V> the type of map value * @return a {@link MapCache} instance representing the named Hazelcast-managed {@link com.hazelcast.core.IMap IMap}. * @throws CacheException * @see HazelcastInstance#getMap(String) * @see #ensureHazelcastInstance() * */ public <K, V> Cache<K, V> getCache(String name) throws CacheException { Map<K, V> map = ensureHazelcastInstance().getMap(name); //returned map is a ConcurrentMap return new MapCache<K, V>(name, map); }
/** * Ensures the internal delegate <code>SessionManager</code> is injected with the newly set * {@link #setCacheManager CacheManager} so it may use it for its internal caching needs. * <p/> * Note: This implementation only injects the CacheManager into the SessionManager if the SessionManager * instance implements the {@link CacheManagerAware CacheManagerAware} interface. */ protected void applyCacheManagerToSessionManager() { if (this.sessionManager instanceof CacheManagerAware) { ((CacheManagerAware) this.sessionManager).setCacheManager(getCacheManager()); } }
/** * Clears out the AuthenticationInfo cache entry for the specified account. * <p/> * This method is provided as a convenience to subclasses so they can invalidate a cache entry when they * change an account's authentication data (e.g. reset password) during runtime. Because an account's * AuthenticationInfo can be cached, there needs to be a way to invalidate the cache for only that account so that * subsequent authentication operations don't used the (old) cached value if account data changes. * <p/> * After this method is called, the next authentication for that same account will result in a call to * {@link #doGetAuthenticationInfo(org.apache.shiro.authc.AuthenticationToken) doGetAuthenticationInfo}, and the * resulting return value will be cached before being returned so it can be reused for later authentications. * <p/> * If you wish to clear out all associated cached data (and not just authentication data), use the * {@link #clearCache(org.apache.shiro.subject.PrincipalCollection)} method instead (which will in turn call this * method by default). * * @param principals the principals of the account for which to clear the cached AuthorizationInfo. * @see #clearCache(org.apache.shiro.subject.PrincipalCollection) * @since 1.2 */ protected void clearCachedAuthenticationInfo(PrincipalCollection principals) { if (!isEmpty(principals)) { Cache<Object, AuthenticationInfo> cache = getAvailableAuthenticationCache(); //cache instance will be non-null if caching is enabled: if (cache != null) { Object key = getAuthenticationCacheKey(principals); cache.remove(key); } } }
/** * Creates a cache instance used to store active sessions. Creation is done by first * {@link #getCacheManager() acquiring} the {@code CacheManager}. If the cache manager is not null, the * cache returned is that resulting from the following call: * <pre> String name = {@link #getActiveSessionsCacheName() getActiveSessionsCacheName()}; * cacheManager.getCache(name);</pre> * * @return a cache instance used to store active sessions, or {@code null} if the {@code CacheManager} has * not been set. */ protected Cache<Serializable, Session> createActiveSessionsCache() { Cache<Serializable, Session> cache = null; CacheManager mgr = getCacheManager(); if (mgr != null) { String name = getActiveSessionsCacheName(); cache = mgr.getCache(name); } return cache; }
/** * Returns the size (in bytes) that this EhCache's memory store is using (RAM), or <code>-1</code> if * that number is unknown or cannot be calculated. * * @return the size (in bytes) that this EhCache's memory store is using (RAM), or <code>-1</code> if * that number is unknown or cannot be calculated. */ public long getMemoryStoreSize() { try { return cache.getMemoryStoreSize(); } catch (Throwable t) { throw new CacheException(t); } }
/** * Returns a new {@link MapCache MapCache} instance backed by a {@link SoftHashMap}. * * @param name the name of the cache * @return a new {@link MapCache MapCache} instance backed by a {@link SoftHashMap}. */ @Override protected Cache createCache(String name) { return new MapCache<Object, Object>(name, new SoftHashMap<Object, Object>()); } }
/** * Removes all elements in the cache, but leaves the cache in a useable state. */ public void clear() throws CacheException { if (log.isTraceEnabled()) { log.trace("Clearing all objects from cache [" + cache.getName() + "]"); } try { cache.removeAll(); } catch (Throwable t) { throw new CacheException(t); } }