EhCache和Shiro整合使用,缓存权限数据,避免每次授权都查数据库

主要针对Shiro使用EhCache缓存框架.由于每次访问需要权限的页面或者方法等,每一次请求都会走数据库查询,很难受~都懂哈~

只要下面配置好了,只有第一次会走数据库查询是否拥有权限,之后都会从缓存中获取.(下面配置只针对权限效验,方法上使用缓存参考另一篇文章)


如果没使用Shiro用Redis更好

为何不使用Redis?

因为在Shiro包中,会发现里面有EhCache,支持更好.更方便

如果使用Redis使用稍微麻烦一点


maven坐标:



    <!-- ehcache-->  
            <dependency>  
                <groupId>net.sf.ehcache</groupId>  
                <artifactId>ehcache-core</artifactId>  
                <version>2.6.11</version>  
            </dependency>  


    <!--shiro权限控制-->  
            <dependency>  
                <groupId>org.apache.shiro</groupId>  
                <artifactId>shiro-all</artifactId>  
                <version>1.3.2</version>  
            </dependency>  


ehcache.xml (缓存配置文件)

[java] view plain copy
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">  
      
        <!--  <diskStore path="C:/ehcache"/> -->  
        <!-- user.home:用户的家目录。  
        user.dir:用户的当前工作目录。  
        java.io.tmpdir:Java临时目录。 -->  
        <diskStore path="${webapp.root}"/>  
      
        <!-- 默认缓存配置. -->  
        <!-- <defaultCache maxEntriesLocalHeap="100" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600"  
            overflowToDisk="true" maxEntriesLocalDisk="100000"/> -->  
      
        <cache name="cache name*"  
               maxElementsInMemory="10000"  
               eternal="false"  
               timeToIdleSeconds="120"  
               timeToLiveSeconds="120"  
               maxElementsOnDisk="10000000"  
               diskExpiryThreadIntervalSeconds="120"  
               memoryStoreEvictionPolicy="LRU">  
            <persistence strategy="localTempSwap"/>  
        </cache>  
      
        <!--默认缓存配置,以下属性是必须的:  
                name :cache的标识符,在一个CacheManager中必须唯一。  
                maxElementsInMemory : 在内存中缓存的element的最大数目。  
                maxElementsOnDisk : 在磁盘上缓存的element的最大数目。  
                eternal : 设定缓存的elements是否有有效期。如果为true,timeouts属性被忽略。  
                overflowToDisk : 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上。  
                以下属性是可选的:  
                timeToIdleSeconds : 缓存element在过期前的空闲时间。  
                timeToLiveSeconds : 缓存element的有效生命期。  
                diskPersistent : 在VM重启的时候是否持久化磁盘缓存,默认是false。  
                diskExpiryThreadIntervalSeconds : 磁盘缓存的清理线程运行间隔,默认是120秒.  
                memoryStoreEvictionPolicy : 当内存缓存达到最大,有新的element加入的时候,  
                移除缓存中element的策略。默认是LRU,可选的有LFU和FIFO  
                缓存子元素:  
                cacheEventListenerFactory:注册相应的的缓存监听类,用于处理缓存事件,如put,remove,update,和expire  
                bootstrapCacheLoaderFactory:指定相应的BootstrapCacheLoader,用于在初始化缓存,以及自动设置。  
        -->  
      
        <!--  
            LRU:LRU是Least Recently Used 的缩写 LRU缓存把最近最少使用的数据移除,让给最新读取的数据。而往往最常读取的,也是读取次数最多的,所以,利用LRU缓存,我们能够提高系统的performance(性能)  
            LFU是最近最不常用页面置换算法(Least Frequently Used),也就是淘汰一定时期内被访问次数最少的页!  
            FIFO(First In First Out ,先进先出)  
          算法是根据先进先出原理来淘汰数据的,实现上是最简单的一种,具体算法如下:  
          1. 新访问的数据插入FIFO队列尾部,数据在FIFO队列中顺序移动;  
          2. 淘汰FIFO队列头部的数据;  
         -->  
    </ehcache>  


applicationContext-shiro.xm

扫描二维码关注公众号,回复: 1994615 查看本文章
[java] view plain copy
    <!-- 安全管理器  -->  
       <bean id="securityManager"  
             class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
           <property name="realm" ref="bosRealm" />  
           <!--shiro的执行流程:应用程序代码-SecurityManager-Realm(查询授权和认证数据),所以将EhCache加在Realm之前最合适-->  
           <property name="cacheManager" ref="shiroCacheManager" />  
       </bean>  
      
      
       <!-- 配置Realm -->  
       <bean id="bosRealm" class="com.bos.web.action.base.realm.BosRealm">  
           <!-- 缓存区的名字 就是 ehcache.xml 自定义 cache的name -->  
           <property name="authorizationCacheName" value="bos" />  
       </bean>  
      
       <!-- 生命周期 -->  
       <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>  


applicationContext-cache.xmll

[java] view plain copy
    <?xml version="1.0" encoding="UTF-8"?>  
    <beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
        xmlns:cache="http://www.springframework.org/schema/cache"  
        xsi:schemaLocation="  
            http://www.springframework.org/schema/beans   
            http://www.springframework.org/schema/beans/spring-beans.xsd  
            http://www.springframework.org/schema/cache   
            http://www.springframework.org/schema/cache/spring-cache.xsd ">  
       
         <!-- 缓存配置  -->  
        <bean id="ehCacheManager"   
            class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
            <property name="configLocation" value="classpath:ehcache.xml" />  
        </bean>  
          
        <!-- shiro封装cacheManager -->  
        <bean id="shiroCacheManager"   
            class="org.apache.shiro.cache.ehcache.EhCacheManager">  
            <property name="cacheManager" ref="ehCacheManager" />  
        </bean>  
          
        <!-- spring 封装ehcache缓存管理器  -->  
        <bean id="springCacheManager"   
            class="org.springframework.cache.ehcache.EhCacheCacheManager">  
            <property name="cacheManager" ref="ehCacheManager" />  
        </bean>  
          
        <!-- 激活spring 缓存注解 -->  
        <cache:annotation-driven cache-manager="springCacheManager"/>  
          
    </beans>  


配置好了之后就可以自动对shiro的权限效验进行缓存了~

如果需要对普通方法,例如查询使用EhCache,点我

猜你喜欢

转载自blog.csdn.net/qq_35232663/article/details/80620855