shiro权限控制下的ehcache缓存技术总结

shiro默认对ehcache缓存技术的支持,因此不使用redis缓冲技术

---------------------------------------------------------------------------------------
ehcache配置步骤:
    1、导包:ehcache的开发包    ehcache-core
        spring整合ehcache的包在spring-context-support里面
    2、解压ehcache-core包,将里面的 ehcache-failsafe.xml复制并改名为ehcache.xml
        自定义缓存区,cache标签后添加name属性
         <!-- 自定义缓存 -->
         <!-- 复制一份,修改成cache标签并添加name属性 -->
         <cache    name="bos"
                maxElementsInMemory="10000"
                eternal="false"
                timeToIdleSeconds="120"
                timeToLiveSeconds="120"
                maxElementsOnDisk="10000000"
                diskExpiryThreadIntervalSeconds="120"
                memoryStoreEvictionPolicy="LRU">
            <persistence strategy="localTempSwap"/>
        </cache>
    3、配置spring整合ehcache
        ehcacheManager交由spring管理,即创建ehcacheManagerFactoryBean
        <!-- ehcache缓存 -->
        <bean id="ehCacheManager"
            class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
            <property name="configLocation" value="classpath:ehcache.xml"></property>
        </bean>
----------------------------------------------------------------------------------------
shiro整合ehcache的配置步骤:
    1、配置shiro封装缓存管理器
        <!-- 配置shiro整合缓存管理器 -->
        <bean id="shiroEhCahceManager"
            class="org.apache.shiro.cache.ehcache.EhCacheManager">
            <property name="cacheManager" ref="ehCacheManager"></property>
        </bean>
    2、将shiro缓存管理器注入安全管理器
        <!-- 安全管理器 -->
        <bean id="securityManager"
            class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
            <!-- 自定义的realm -->
            <property name="realm" ref="bosRealm"></property>
            <property name="cacheManager" ref="shiroEhCahceManager"></property>
        </bean>
    3、对认证数据、授权数据    自定义的realm进行缓存配置
        <!-- 配置bosRealm -->
        <bean id="bosRealm" class="cn.itcast.bos.realm.BosRealm">
            <!-- 对其进行缓存配置 -->
            <!-- bos为缓存cahce.xml中自定义缓存配置的name属性 -->
            <property name="authenticationCacheName" value="bos"></property>
        </bean>
--------------------------------------------------------------------------------------        
    注意: 使需要缓存对象,实现 Serializable 接口
    java.io.NotSerializableException: cn.itcast.bos.domain.system.User
--------------------------------------------------------------------------------------        
ehcache对普通业务进行缓存
    1、spring 封装cache缓存管理器
        <!-- spring封装cache缓存管理器 -->
        <bean id="springCacheManager"
            class="org.springframework.cache.ehcache.EhCacheCacheManager">
            <property name="cacheManager" ref="ehCacheManager"></property>
        </bean>
    2、在自定义的applicationContext-cache.xml中引入cache名称空间
    3、激活spring的缓存注解
        <!-- spring激活缓存注解 -->
        <cache:annotation-driven cache-manager="springCacheManager"/>
    4、可以在自定义缓存对象类的方法上使用缓存注解
        @Cacheable 应用缓存区,对方法返回结果进行缓存 ---- 用于查询方法
        @CacheEvict 清除缓存区数据 --- 用于 增加、修改、删除 方法
        针对数据在不同条件下进行不同缓存,设置@Cacheable 注解 key 属性
       
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值