springrain技术详解(4)-shiro的缓存

因为是权限拦截校验,很多方法调用的频率是非常频繁的,为了更好的性能,shiro拥有一套完整的缓存体系,特别是针对web领域,做了部分增强.

先看下缓存在shiro的权限管理器中的配置:

01<!-- 权限管理器 -->
02 <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
03<!-- 数据库认证的实现 org.springrain.frame.shiro.ShiroDbRealm -->
04 <property name="realm" ref="shiroDbRealm" />
05<!-- session 管理器 -->
06 <property name="sessionManager" ref="sessionManager" />
07<!-- 缓存管理器 -->
08 <property name="cacheManager" ref="shiroCacheManager" />
09 </bean>
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() {
2// 认证缓存
3// super.setAuthenticationCacheName(GlobalStatic.authenticationCacheName);
4 super.setAuthenticationCachingEnabled(false);
5// 授权缓存
6 super.setAuthorizationCacheName(GlobalStatic.authorizationCacheName);
7}

认证是不想缓存的,登陆的时候查询一下数据库也没有什么,所以设置了 false,授权是要缓存的,也指定了缓存的名称,主要是为了刷新用户的权限缓存.
这样用户每次校验用户的访问权限,就不需要再查询数据库了.

对于缓存的刷新,shiro也提供了方法,接着上例说,管理员修改了一个角色的访问权限,这个时候需要对缓存刷新.

在 org.springrain.demo.service.impl.UserRoleMenuServiceImpl中进行了缓存手动刷新

1 public void updateRoleMenu(String roleId,String[] menus) throws Exception {
2//刷新权限缓存
3shiroCacheManager.getCache(GlobalStatic.authorizationCacheName).clear();
4.......
5}

以上就是对shiro cahce的简单使用, 下一篇说下shiro对httpsession的缓存处理.

本文出自 9iu.org,转载时请注明出处及相应链接。

本文永久链接: http://www.9iu.org/2013/12/15/springrain4-shiro-cache.html

0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值