shiro ehcache

项目部署shiro后发现任何操作都会导致重新查一遍数据库操作,本来以为只有登录才会触发,可是看到每次哪怕点击一个按钮 就查询一连串 权限表和角色表之后 发现必须要去优化了。

开始思路 因为所有操作都要通过 doGetAuthorizationInfo (授权)这个方法,然后把数据库查到的结果放到缓存 用户作为key 权限和角色所谓value。

后来发现有一个更为简便的方法 -> 对持久层做缓存 例如每次都要调用roleService.findAll();这个方法 那么就可以在roleService的实现方法上增加shiro标签@CacheAble("role")里边的role是在eheache配置文件中定义的。这样 就可以不去查数据库直接在缓存充取就行了,至于key和value就不是我们关心的了

好了 整体思路再过一遍


首先在pom文件中增加ehcache

<dependency>

          <groupId>net.sf.ehcache</groupId>

          <artifactId>ehcache-core</artifactId>

          <version>2.6.6</version>

 </dependency>

2 创建配置文件  ehcache.xml 


<!-- 一个缓存区域, 用来存放取派员 -->

    <!--name 是缓存区域的名称 -->

    <cache name="staff"

        maxElementsInMemory="10000"

           eternal="false"

           timeToIdleSeconds="120"

           timeToLiveSeconds="120"

    ></cache>



3 把echache配置到spring 中去

   <!-- spring对ehcache的缓存工厂支持 -->

   <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">

       <property name="configLocation" value="classpath:ehcache.xml" />

       <property name="shared" value="false"/>

   </bean>



   <!-- spring对ehcache的缓存管理 -->

   <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">

       <property name="cacheManager" ref="ehCacheManagerFactory"></property>

   </bean>

   


   <!-- 使用注解配置缓存 -->

   <cache:annotation-driven cache-manager="ehCacheManager"/>



4在service的实现类添加注解 

@Cacheable("staff")// 对pageQuery 方法使用缓存

    public Object pageQuery(int page, int rows) {

 

       @CacheEvict(value = "staff", allEntries = true)// 清除缓存

    public String save(Staffstaff) {


5 把ehcache和shiro结合



       <!-- 配置shiro需要缓存管理器 -->

   <!-- shiroehcache的缓存管理直接使用spring的缓存工厂 -->

   <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">

       <property name="cacheManager" ref="ehCacheManagerFactory" />

   </bean>



将缓存管理器,交给安全管理器

       <!-- 安全管理器 -->

   <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">

      <!-- 缓存管理器 -->

      <property name="cacheManager"ref="cacheManager" />

      <property name="realm"ref="bosRealm" />

   </bean>


在Realm中指定使用的缓存区域的名称

       <!-- 自定义Realm -->

   <bean id="bosRealm" class="cn.itcast.bos.shiro.BosRealm">

      <property name="userService"ref="userService" />

      <property name="roleService"ref="roleService" />

      <property name="functionService"ref="functionService" />

      <!-- 指定使用缓存区域的名称 -->

      <property name="authorizationCacheName" value="authorization"/>

   </bean>



经过这么多配置 就可以把权限的数据放到缓存中去啦

有时候可能会报ehcache的错误

intellj idea告诉我是cache.xsd文件找不到 这个 从官网上下载一份到本地 在 头部添加

xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"


就可以了



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值