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>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值