一、ehcache简单介绍
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,Ehcache是一种广泛使用的开 源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序等特点。
优点:
1. 快速 、简单 、拥有多种缓存策略。
2. 缓存数据有两级:内存和磁盘,因此无需担心容量问题
3. 缓存数据会在虚拟机重启的过程中写入磁盘
4. 具有缓存和缓存管理器的侦听接口 。支持多缓存管理器实例,以及一个实例的多个缓存区域 等。
缺点:
1. 使用磁盘Cache的时候非常占用磁盘空间:这是因为DiskCache的算法简单,该算法简单也导致Cache的效率非常高。它只是对元素直接追加存储。因此搜索元素的时候非常的快。如果使用DiskCache的,在很频繁的应用中,很快磁盘会满。
2. 不能保证数据的安全:当突然kill掉java的时候,可能会产生冲突,EhCache的解决方法是如果文件冲突了,则重建cache。这对于Cache 数据需要保存的时候可能不利。当然,Cache只是简单的加速,而不能保证数据的安全。
二、Spring整合ehcache
1、相关jar包依赖(Spring相关jar包自己引用不在此展示)
<!-- ehcache 相关依赖 -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.8.2</version>
</dependency>
2、添加ehcache配置文件ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="es">
<diskStore path="java.io.tmpdir"/>
<!--timeToIdleSeconds 当缓存闲置n秒后销毁 -->
<!--timeToLiveSeconds 当缓存存活n秒后销毁 -->
<!--
缓存配置
name:缓存名称。
maxElementsInMemory:缓存最大个数。
eternal:对象是否永久有效,一但设置了,timeout将不起作用。
timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
maxElementsOnDisk:硬盘最大缓存个数。
diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
clearOnFlush:内存数量最大时是否清除。
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToLiveSeconds="600"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
<!-- test 数据缓存5分钟 -->
<cache
name="test-cache"
eternal="false"
timeToLiveSeconds="60"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU">
</cache>
</ehcache>
这里我设置了test-cache策略,一分钟过期。
cache元素相关属性我已经在上面的xml里写的很清楚了,请广大读者仔细阅读。
3.创建一个applicationContext-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.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 支持缓存注解 -->
<cache:annotation-driven cache-manager="cacheManager" />
<!-- 默认是cacheManager -->
<bean id="cacheManager" name= "cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory"/>
</bean>
<!-- cache管理器配置 -->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache/ehcache.xml"/><!-- ehcache.xml的路径 -->
<property name="shared" value="true" />
</bean> </beans>
4、applicationContext.xml中引入上面的xml(名称可随意更改)
<import resource="classpath:applicationContext-ehcache.xml" />
三、测试
public class CacheTest extends BaseTest{
//获取cacheManager实例
@Resource
private CacheManager cacheManager;
@Test
public void putCache() throws InterruptedException {
System.out.println("cacheManager = " +cacheManager);
//获取配置文件中的缓存
Cache cache = cacheManager.getCache("test-cache");
//存值,key-value
cache.put("test", 1);
Thread.sleep(10000);//自行设置,测试过期与否
//取值,如果已过期,则取不出值
System.out.println(cache.get("test",Integer.class));
}
}
输出结果:
缓存没有过期(如下图)
缓存已过期(如下图)
以上就是Spring整合ehcache实现缓存功能的示例,限于本人水平有限,发现问题还请不吝赐教!