因为是权限拦截校验,很多方法调用的频率是非常频繁的,为了更好的性能,shiro拥有一套完整的缓存体系,特别是针对web领域,做了部分增强.
先看下缓存在shiro的权限管理器中的配置:
02 | < bean id = "securityManager" class = "org.apache.shiro.web.mgt.DefaultWebSecurityManager" > |
04 | < property name = "realm" ref = "shiroDbRealm" /> |
06 | < property name = "sessionManager" ref = "sessionManager" /> |
08 | < property name = "cacheManager" ref = "shiroCacheManager" /> |
10 | < bean id = "shiroCacheManager" class = "org.apache.shiro.cache.MemoryConstrainedCacheManager" /> |
cacheManager就是shiro的缓存管理器,springrain默认使用了shiro提供的MemoryConstrainedCacheManager,也可以实现相应的接口,实现自定义的cacheManager,例如springrain扩展的ShiroRedisCacheManager.
缓存的用途主要是两个.一:权限相关的授权,要是每次都从数据库查询,太不合理了.二:对session对象的缓存,实现分布式的session共享.
授权当然是有realm来做了,看下springrain中shiroDbRealm对缓存的支持.
1 | public ShiroDbRealm() { |
4 | super .setAuthenticationCachingEnabled( false ); |
6 | super .setAuthorizationCacheName(GlobalStatic.authorizationCacheName); |
认证是不想缓存的,登陆的时候查询一下数据库也没有什么,所以设置了 false,授权是要缓存的,也指定了缓存的名称,主要是为了刷新用户的权限缓存.
这样用户每次校验用户的访问权限,就不需要再查询数据库了.
对于缓存的刷新,shiro也提供了方法,接着上例说,管理员修改了一个角色的访问权限,这个时候需要对缓存刷新.
在 org.springrain.demo.service.impl.UserRoleMenuServiceImpl中进行了缓存手动刷新
1 | public void updateRoleMenu(String roleId,String[] menus) throws Exception { |
3 | shiroCacheManager.getCache(GlobalStatic.authorizationCacheName).clear(); |
以上就是对shiro cahce的简单使用, 下一篇说下shiro对httpsession的缓存处理.
本文出自 9iu.org,转载时请注明出处及相应链接。
本文永久链接: http://www.9iu.org/2013/12/15/springrain4-shiro-cache.html