Spring整合ehcache

配置文件

导入maven坐标

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

Spring整合ehcache包 spring-context-support包

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>${spring.version}</version>
</dependency>

导入ehcache.xml配置文件
解压ehcache-core.jar包,将ehcache-failsafe.xml 复制src/main/resources 改名为ehcache.xml,并自定义缓存区

 <cache name="bos"
         maxElementsInMemory="10000"
         eternal="false"
         timeToIdleSeconds="120"
         timeToLiveSeconds="120"
         maxElementsOnDisk="10000000"
         diskExpiryThreadIntervalSeconds="120"
         memoryStoreEvictionPolicy="LRU">
     <persistence strategy="localTempSwap"/>
 </cache>

将ehcacheManager交给spring管理

<!-- 缓存配置  -->
<bean id="ehCacheManager" 
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:ehcache.xml" />
</bean>

整合ehcache
配置shiro封装缓存管理器

    <!-- spring 封装ehcache缓存管理器  -->
    <bean id="springCacheManager" 
    class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="ehCacheManager" />
    </bean>

将shiro的缓存管理器,注入到安全管理器中

    <!-- 安全管理器  -->
    <bean id="securityManager" 
        class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="bosRealm" />
        <property name="cacheManager" ref="shiroCacheManager" />
    </bean>

对认证数据、授权数据哪些进行缓存
配置Realm
注意:使需要缓存对象,实现Serializable接口

// 自定义Realm ,实现安全数据 连接
// @Service("bosRealm")
public class BosRealm extends AuthorizingRealm
<!-- 配置Realm -->
<bean id="bosRealm" class="com.kayo.bos.realm.BosRealm">
    <!-- 缓存区的名字 就是 ehcache.xml 自定义 cache的name -->
    <property name="authorizationCacheName" value="bos" />
</bean>

@Cacheable、@CacheEvict缓存注解的使用
参考spring整合ehcache管理CacheManager,前三步是一样的,接下来继续配置
配置spring缓存管理器,封装ehcache自带CacheManager

<!-- spring 封装ehcache缓存管理器  -->
<bean id="springCacheManager" 
    class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="ehCacheManager" />
</bean>

在applicationContext.xml中引入cache名称空间

xmlns:cache="http://www.springframework.org/schema/cache"

激活spring缓存注解
当spring整合缓存框架后,提供了一套注解

    <!-- 激活spring 缓存注解(这个注解是可以让工程中其他类中,通过注解可以快速方便使用缓存技术 -->
    <cache:annotation-driven cache-manager="springCacheManager"/>

在被spring管理bean对象方法上使用@Cacheable、@CacheEvict
@Cacheable应用缓存区,对方法返回结果进行缓存,用于查询方法
@CacheEvict清除缓存区数据,用于增加、修改、删除方法
在applicationContext-ehcache.xml自定义缓存区

    <cache name="standard"
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </cache>

对实体类进行序列化,在service层增加注解

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值