Spring整合ehcache

来源:互联网 发布:linux显示文本名 编辑:程序博客网 时间:2024/06/11 15:35

配置文件

导入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层增加注解

原创粉丝点击