1.Ehcache简介
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点。是Hibernate中默认CacheProvider。
Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。
它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特点。
1.1 Ehcache和Redis区别
ehcache直接在jvm虚拟机中缓存,速度快,效率高;但是缓存共享麻烦,集群分布式应用不方便。
redis是通过socket访问到缓存服务,效率比Ehcache低,比数据库要快很多,处理集群和分布式缓存方便,有成熟的方案。如果是单个应用或者对缓存访问要求很高的应用,用ehcache。如果是大型系统,存在缓存共享、分布式部署、缓存内容很大的,建议用redis。
ehcache也有缓存共享方案,不过是通过RMI或者Jgroup多播方式进行广播缓存通知更新,缓存共享复杂,维护不方便;简单的共享可以,但是涉及到缓存恢复,大数据缓存,则不合适。
3.Ehcache的配置
3.1 在pom.xml中引入依赖
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.11</version>
</dependency>
3.2 Ehcache的配置文件-ehcache.xml
默认情况下Ehcache会自动加载classpath根目录下名为ehcache.xml文件,也可以将该文件放到其他地方在使用时指定文件的位置
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false" dynamicConfig="false">
<!-- 磁盘缓存位置 -->
<diskStore path="java.io.tmpdir/ehcache" />
<!-- 默认缓存 -->
<defaultCache
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
<cache name="HelloWorldCache"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="3000"
timeToLiveSeconds="3000"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/>
参数 | 说明 |
---|---|
diskStore | ehcache支持内存和磁盘两种存储 |
path | 指定磁盘存储的位置 |
defaultCache | 默认的缓存 |
cache | 自定的缓存,当自定的配置不满足实际情况时可以通过自定义(可以包含多个cache节点) |
name | 缓存的名称,可以通过指定名称获取指定的某个Cache对象 |
maxElementsInMemory | 内存中允许存储的最大的元素个数,0表示无穷大 |
maxElementsOnDisk | 硬盘中最大缓存对象数,若是0表示无穷大 |
clearOnFlush | 内存数量最大时是否清除 |
eternal | 设置缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。根据存储数据的不同,例如一些静态不变的数据如省市区等可以设置为永不过时 |
memoryStoreEvictionPolicy | 当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。可选策略有:LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数) |
overflowToDisk | true表示当内存缓存的对象数目达到了maxElementsInMemory界限后,会把溢出的对象写到硬盘缓存中。注意:如果缓存的对象要写入到硬盘中的话,则该对象必须实现了Serializable接口才行 |
timeToIdleSeconds | 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,如果处于空闲状态的时间超过了timeToIdleSeconds属性值,这个对象就会过期,EhCache将把它从缓存中清空。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地处于空闲状态 |
timeToLiveSeconds | 设定对象允许存在于缓存中的最长时间,以秒为单位。当对象自从被存放到缓存中后,如果处于缓存中的时间超过了 timeToLiveSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清除。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地存在于缓存中。timeToLiveSeconds必须大于timeToIdleSeconds属性,才有意义 |
diskSpoolBufferSizeMB | 这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区 |
diskPersistent | 是否在VM重启时存储硬盘的缓存数据。默认值是false |
diskExpiryThreadIntervalSeconds | 磁盘失效线程运行时间间隔,默认是120秒 |
3.3 与Spring的整合配置
3.3.1 spring-ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<description>ehcache缓存配置管理文件</description>
<!--Ehcache配置-->
<bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache-caches.xml"/>
<property name="shared" value="true"/>
</bean>
<!--缓存管理器:Ehcache-->
<bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcacheManager"/>
</bean>
<!--启用ehcache缓存注解, Spring 程序中使用缓存注解功能-->
<cache:annotation-driven cache-manager="springCacheManager"/>
</beans>
3.3.2 Spring缓存注解
-
@Cacheable:表明所修饰的方法是可以缓存的:当第一次调用这个方法时,它的结果会被缓存下来,在缓存的有效时间内,以后访问这个方法都直接返回缓存结果,不再执行方法中的代码段。
这个注解可以用condition属性来设置条件,如果不满足条件,就不使用缓存能力,直接执行方法。
可以使用key属性来指定key的生成规则。支持如下几个参数:
- value:缓存位置名称,不能为空,如果使用EhCache,就是ehcache.xml中声明的cache的name, 指明将值缓存到哪个Cache中
- key : 缓存的key,默认为空,既表示使用方法的参数类型及参数值作为key,支持SpEL,如果要引用参数值使用井号加参数名,如:#param,最好是能够唯一,因为这样可以准确的清除掉特定的缓存,而不会影响到其它缓存值
- condition:触发条件,只有满足条件的情况才会加入缓存,默认为空,既表示全部都加入缓存,支持SpEL
@Cacheable(value="HelloWorldCache", key= "#param",condition="#param>0")
public String test(int param) {
return "test";
}
- @CachePut:与@Cacheable不同,@CachePut不仅会缓存方法的结果,还会执行方法的代码段。它支持的属性和用法都与@Cacheable一致。
@CachePut(value="HelloWorldCache", key= "#param",condition="#param>0")
public String test(int param) {
return "test";
}
- @CacheEvict:与@Cacheable功能相反,@CacheEvict表明所修饰的方法是用来删除失效或无用的缓存数据。
@CacheEvict 支持如下几个参数:
-
value:缓存位置名称,不能为空,同上
-
key:缓存的key,默认为空,同上
-
condition:触发条件,只有满足条件的情况才会清除缓存,默认为空,支持SpEL
-
allEntries:true表示清除value中的全部缓存,默认为false
@CacheEvict(value="HelloWorldCache", key= "#param",condition="#param>0")
public String test(int param) {
return "test";
}
@CacheEvict(value="HelloWorldCache", allEntries="true")
public void removeAll() {
}