Shiro在Spring中的緩存配置

1、開發環境
①、spring開發環境配置
②、shiro相關開發環境配置
http://blog.csdn.net/u012737182/article/details/53002281
在此基礎上再增加shiro的緩存組件包

<dependency> 
    <groupId>org.apache.shiro</groupId> 
    <artifactId>shiro-ehcache</artifactId> 
    <version>1.3.0</version> 
</dependency>

2、定義相應的緩存配置文件

<?xml version="1.1" encoding="UTF-8"?>
<ehcache name="shirocache">

    <diskStore path="java.io.tmpdir"/>

     <defaultCache 
        maxElementsInMemory="2000"
        eternal="true"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"/>


    <cache name="passwordRetryCache"
           maxElementsInMemory="2000"
           eternal="false"
           timeToIdleSeconds="300"
           timeToLiveSeconds="0"
           overflowToDisk="false">
    </cache>

    <cache name="authorizationCache"
           maxElementsInMemory="2000"
           eternal="false"
           timeToIdleSeconds="1800"
           timeToLiveSeconds="0"
           overflowToDisk="false">
    </cache>

    <cache name="authenticationCache"
           maxElementsInMemory="2000"
           eternal="false"
           timeToIdleSeconds="1800"
           timeToLiveSeconds="0"
           overflowToDisk="false">
    </cache>

    <cache name="shiro-activeSessionCache"
           maxElementsInMemory="2000"
           eternal="false"
           timeToIdleSeconds="1800"
           timeToLiveSeconds="0"
           overflowToDisk="false">
    </cache>
</ehcache>

在此配置文件之中實際上有以下幾個核心選項:
“diskStore path=”java.io.tmpdir”:磁盤的存儲目錄;
“name=”xxx””:對要進行緩存的項進行一個標註;
“maxElementsInMemory=”2000”:可以緩存的最大的對象個數;
“eternal=”false”:是否允許自動失效(如果某一個對象長時間不使用);
“timeToIdleSeconds=”1800”:最小的失效時間,1800秒;
“timeToLiveSeconds=”0”:最大的保存時間,單位是秒;
“overflowToDisk=”false”:如果容量過多,可以將其保存在磁盤
3、如果要想使緩存生效,則還需要修改applicationContext.xml文件進行緩存配置:
· 定義緩存管理器:

<!-- 進行緩存的操作配置 --> 
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> 
    <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/> 
</bean>

在安全管理器之中註冊此緩存管理器:

<!-- 配置SecurityManager的管理 --> 
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> 
    <!-- 配置你需要使用的Realms --> 
    <property name="realm" ref="memberRealm"/> 
    <property name="cacheManager" ref="cacheManager"/> 
</bean>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章