Chapter 11 Caching Mechanism

Shiro provides a Cache abstraction similar to Spring , that is, Shiro itself does not implement Cache , but abstracts Cache again, which is convenient to replace different underlying Cache implementations. For some concepts of Cache , please refer to my " Spring Cache Abstraction Detailed": http://jinnianshilongnian.iteye.com/blog/2001040 .

 

Cache interface provided by Shiro : 

Java code   Favorite code
  1. public  interface  Cache<K, V> {  
  2.     / / Get the value in the cache according to the Key  
  3.     public V get(K key) throws CacheException;  
  4.     //Put the key-value into the cache and return the previous value in the cache  
  5.     public V put(K key, V value) throws CacheException;   
  6.     //Remove the value corresponding to the key in the cache and return the value  
  7.     public V remove(K key) throws CacheException;  
  8.     // clear the entire cache  
  9.     public void clear() throws CacheException;  
  10.     //return cache size  
  11.     public int size();  
  12.     //Get all the keys in the cache  
  13.     public Set<K> keys();  
  14.     //Get all the values ​​in the cache  
  15.     public Collection<V> values();  
  16. }  

  

The CacheManager interface provided by Shiro : 

Java code   Favorite code
  1. public  interface  CacheManager {  
  2.     //Get a Cache based on the cache name  
  3.     public <K, V> Cache<K, V> getCache(String name) throws CacheException;  
  4. }  

  

Shiro also provides CacheManagerAware for injecting CacheManager : 

Java code   Favorite code
  1. public  interface  CacheManagerAware {  
  2.     //Inject CacheManager  
  3.     void  setCacheManager(CacheManager cacheManager);  
  4. }  

 

The corresponding component ( DefaultSecurityManager ) in Shiro will automatically detect whether the corresponding object (such as Realm ) implements CacheManagerAware and automatically inject the corresponding CacheManager .

  

The use cases in this chapter use the same code from Chapter 6.

 

Realm cache

Shiro provides CachingRealm, which implements the CacheManagerAware interface and provides some basic implementations of caching; in addition, AuthenticatingRealm and AuthorizingRealm provide caching of AuthenticationInfo and AuthorizationInfo information respectively.

 

ini placement   

Java code   Favorite code
  1. userRealm=com.github.zhangkaitao.shiro.chapter11.realm.UserRealm  
  2. userRealm.credentialsMatcher=$credentialsMatcher  
  3. userRealm.cachingEnabled=true  
  4. userRealm.authenticationCachingEnabled=true  
  5. userRealm.authenticationCacheName=authenticationCache  
  6. userRealm.authorizationCachingEnabled=true  
  7. userRealm.authorizationCacheName=authorizationCache  
  8. securityManager.realms=$userRealm  
  9.   
  10. cacheManager=org.apache.shiro.cache.ehcache.EhCacheManager  
  11. cacheManager.cacheManagerConfigFile=classpath:shiro-ehcache.xml  
  12. securityManager.cacheManager=$cacheManager   

userRealm.cachingEnabled: enable caching, default false;

userRealm.authenticationCachingEnabled : Enable authentication caching, that is, cache AuthenticationInfo information, the default is false ;

userRealm.authenticationCacheName : the cache name for caching AuthenticationInfo information;

userRealm. authorizationCachingEnabled : Enable authorization caching, that is, cache AuthorizationInfo information, default false ;

userRealm. authorizationCacheName : the cache name for caching AuthorizationInfo information;

cacheManager:缓存管理器,此处使用EhCacheManager,即Ehcache实现,需要导入相应的Ehcache依赖,请参考pom.xml

 

因为测试用例的关系,需要将EhcacheCacheManager改为使用VM单例模式:

this.manager = new net.sf.ehcache.CacheManager(getCacheManagerConfigFileInputStream());

改为

this.manager = net.sf.ehcache.CacheManager.create(getCacheManagerConfigFileInputStream());

 

测试用例 

Java代码   Favorite code
  1. @Test  
  2. public void testClearCachedAuthenticationInfo() {  
  3.     login(u1.getUsername(), password);  
  4.     userService.changePassword(u1.getId(), password + "1");  
  5.   
  6.     RealmSecurityManager securityManager =  
  7.      (RealmSecurityManager) SecurityUtils.getSecurityManager();  
  8.     UserRealm userRealm = (UserRealm) securityManager.getRealms().iterator().next();  
  9.     userRealm.clearCachedAuthenticationInfo(subject().getPrincipals());  
  10.   
  11.     login(u1.getUsername(), password + "1");  
  12. }   

首先登录成功(此时会缓存相应的AuthenticationInfo),然后修改密码;此时密码就变了;接着需要调用RealmclearCachedAuthenticationInfo方法清空之前缓存的AuthenticationInfo;否则下次登录时还会获取到修改密码之前的那个AuthenticationInfo

 

Java代码   Favorite code
  1. @Test  
  2. public void testClearCachedAuthorizationInfo() {  
  3.     login(u1.getUsername(), password);  
  4.     subject().checkRole(r1.getRole());  
  5.     userService.correlationRoles(u1.getId(), r2.getId());  
  6.   
  7.     RealmSecurityManager securityManager =  
  8.       (RealmSecurityManager) SecurityUtils.getSecurityManager();  
  9.     UserRealm userRealm = (UserRealm)securityManager.getRealms().iterator().next();  
  10.     userRealm.clearCachedAuthorizationInfo(subject().getPrincipals());  
  11.   
  12.     subject().checkRole(r2.getRole());  
  13. }   

和之前的用例差不多;此处调用RealmclearCachedAuthorizationInfo清空之前缓存的AuthorizationInfo

 

另外还有clearCache,其同时调用clearCachedAuthenticationInfoclearCachedAuthorizationInfo,清空AuthenticationInfoAuthorizationInfo

 

UserRealm还提供了clearAllCachedAuthorizationInfoclearAllCachedAuthenticationInfoclearAllCache,用于清空整个缓存。

 

在某些清空下这种方式可能不是最好的选择,可以考虑直接废弃Shiro的缓存,然后自己通过如AOP机制实现自己的缓存;可以参考:

https://github.com/zhangkaitao/es/tree/master/web/src/main/java/com/sishuok/es/extra/aop

 

另外如果和Spring集成时可以考虑直接使用SpringCache抽象,可以考虑使用SpringCacheManagerWrapper,其对Spring Cache进行了包装,转换为ShiroCacheManager实现:

https://github.com/zhangkaitao/es/blob/master/web/src/main/java/org/apache/shiro/cache/spring/SpringCacheManagerWrapper.java 

 

Session缓存

当我们设置了SecurityManagerCacheManager时,如:

Java代码   Favorite code
  1. securityManager.cacheManager=$cacheManager  

 

当我们设置SessionManager时:

Java代码   Favorite code
  1. sessionManager=org.apache.shiro.session.mgt.DefaultSessionManager  
  2. securityManager.sessionManager=$sessionManager   

securityManager实现了SessionsSecurityManager,其会自动判断SessionManager是否实现了CacheManagerAware接口,如果实现了会把CacheManager设置给它。然后sessionManager会判断相应的sessionDAO(如继承自CachingSessionDAO)是否实现了CacheManagerAware,如果实现了会把CacheManager设置给它;如第九章的MySessionDAO就是带缓存的SessionDAO;其会先查缓存,如果找不到才查数据库。

 

对于CachingSessionDAO,可以通过如下配置设置缓存的名称:

Java代码   Favorite code
  1. sessionDAO=com.github.zhangkaitao.shiro.chapter11.session.dao.MySessionDAO  
  2. sessionDAO.activeSessionsCacheName=shiro-activeSessionCache   

activeSessionsCacheName is shiro-activeSessionCache by default .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326542107&siteId=291194637