Ehcache缓存框架
Spring和Ehcache框架的整合
1.在common_parent父类项目导入Ehcache,Spring和Ehcache整合包 jar包
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.6</version>
</dependency>
2.配置xml文件,解压chcache-core.jar包,将下面的xml文件复制到项目里面
<!-- 内存中缓存数据 过多,写入硬盘,diskStore 用来定义硬盘保存缓存数据位置 -->
<diskStore path="java.io.tmpdir"/>
<!-- 默认缓存配置策略 -->
<!-- maxElementsInMemory 内存中允许存放对象最大数量 -->
<!-- eternal 缓存数据是否是永久的 -->
<!-- maxElementsOnDisk 硬盘中缓存最大对象数量 -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
3.将ehcache交给Spring容器去管理
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
通过配置注解 使用cache
<!-- 使用ehcache 缓存 -->
<cache:annotation-driven cache-manager="ehCacheManager"/>
<!-- spring对ehcache的缓存工厂支持 -->
<bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
<property name="shared" value="false" />
</bean>
<!-- spring对ehcache的缓存管理 -->
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehCacheManagerFactory"></property>
</bean>
通过Spring 提供缓存注解,对数据进行缓存
@Cacheable和@CacheEvict来对缓存进行操作
@Cacheable:负责将方法的返回值加入到缓存中
@CacheEvict:负责清除缓存
@Cacheable("staff")// 对pageQuery 方法使用缓存
public Object pageQuery(int page, int rows) {
@CacheEvict(value = "staff", allEntries = true)// 清除缓存
public String save(Staff staff) {
通常在查询数据时,进行缓存, 在增加、修改、删除数据时,清除缓存 !
注意: ehcache在初始化时,至少要配置缓存区域, 而且配置区域名称需要和 @Cacheable 注解中指定区域名称一致!!!
<!-- 一个缓存区域, 用来存放取派员 -->
<!-- name 是 缓存区域的名称 -->
<cache name="staff"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
></cache>
4.shiro整合ehcach
4.1.配置 shiro 封装缓存管理器
<!-- shiro对ehcache的缓存管理直接使用spring的缓存工厂 -->
<bean id="shirocacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManager" ref="ehCacheManager" />
</bean>
4.2.将shiro缓存管理器注入到安全管理器
<!-- 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!--设置自定义realm -->
<property name="realm" ref="monitorRealm" />
<!-- 将缓存管理器,交给安全管理器 -->
<property name="cacheManager" ref="shirocacheManager"></property>
</bean>
4.3.配置realm
<!--自定义Realm 继承自AuthorizingRealm -->
<bean id="bosRealm" class="cn.itcast.bos.shiro.BosRealm">
<!-- 配置授权 信息 缓存区名称 -->
<property name="authorizationCacheName" value="Bos"></property>
</bean>
属性详解
1、diskStore :指定数据(.data and .index)存储位置,可指定磁盘中的文件夹位置期 The diskStore element is optional. It must be configured if you have overflowToDisk or diskPersistent enabled for any cache. If it is not configured, a warning will be issues and java.io.tmpdir will be used.
2、defaultCache : 默认的管理策略
以下属性是必须的:
1、name: Cache的名称,必须是唯一的(ehcache会把这个cache放到HashMap里)。
2、maxElementsInMemory:在内存中缓存的element的最大数目。
3、maxElementsOnDisk:在磁盘上缓存的element的最大数目,默认值为0,表示不限制。
4、eternal:设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据 timeToIdleSeconds,timeToLiveSeconds判断。
5、overflowToDisk: 如果内存中数据超过内存限制,是否要缓存到磁盘上。
以下属性是可选的:
1、timeToIdleSeconds: 对象空闲时间,指对象在多长时间没有被访问就会失效。只对eternal为false的有效。默认值0,表示一直可以访问。
2、timeToLiveSeconds: 对象存活时间,指对象从创建到失效所需要的时间。只对eternal为false的有效。默认值0,表示一直可以访问。
3、diskPersistent: 是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。
4、diskExpiryThreadIntervalSeconds: 对象检测线程运行时间间隔。标识对象状态的线程多长时间运行一次。
5、diskSpoolBufferSizeMB: DiskStore使用的磁盘大小,默认值30MB。每个cache使用各自的DiskStore。
6、memoryStoreEvictionPolicy: 如果内存中数据超过内存限制,向磁盘缓存时的策略。默认值LRU,可选FIFO、LFU。
缓存的3 种清空策略 :
1、FIFO ,first in first out (先进先出).
2、LFU , Less Frequently Used (最少使用).意思是一直以来最少被使用的。缓存的元素有一个hit 属性,hit 值最小的将会被清出缓存。
3、LRU ,Least Recently Used(最近最少使用). (ehcache 默认值).缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。