先在pom文件添加依赖Jar包
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.9</version>
</dependency>
</dependencies>
定义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">
<!-- 磁盘缓存位置 -->
<diskStore path="java.io.tmpdir/ehcache"/>
<!-- 默认缓存 -->
<defaultCache
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"/>
<cache name="myCache"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="30000"
timeToLiveSeconds="60000"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
记录一下遇到的问题,ehcache.xml中的xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd报错
打开File->settings下将http://ehcache.org/ehcache.xsd添加进去即可。如下图
注意ehcache.xml要放在根目录下,maven根目录是resources.否则会加载不到ehcache.xml文件,就会加载默认的配置文件(引用的ehcache.jar包中有一个ehcache-failsafe.xml 配置文件)
ehcache.xml配置参数说明:
name:缓存名称。
maxElementsInMemory:缓存最大个数。
eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
timeToIdleSeconds:置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
timeToLiveSeconds:缓存数据的生存时间(TTL),也就是一个元素从构建到消亡的最大时间间隔值,这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。
maxEntriesLocalDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
overflowToDisk:内存不足时,是否启用磁盘缓存。
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
maxElementsOnDisk:硬盘最大缓存个数。
diskPersistent:是否在VM重启时存储硬盘的缓存数据。默认值是false。
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
clearOnFlush:内存数量最大时是否清除。
工具类实现
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class EhcacheUtil {
private final static Log log = (Log) LogFactory.getLog(EhcacheUtil.class);;
static CacheManager manager = null;
static {
try {
manager = CacheManager.create(EhcacheUtil.class.getClassLoader().getResourceAsStream("ehcache.xml"));
} catch (CacheException e) {
e.printStackTrace();
log.error("获取ehcache.xml失败", e);
}
}
public static void put(String cacheName, String key, Object value) {
Cache cache = checkCache(cacheName);
Element e = new Element(key, value);
cache.put(e);
}
public static Object get(String cacheName, String key) {
Cache cache = checkCache(cacheName);
Element element = cache.get(key);
return element == null ? null : element.getObjectValue();
}
public static void remove(String cacheName, String key) {
Cache cache = checkCache(cacheName);
cache.remove(key);
}
public static void removeAll(String cacheName) {
Cache cache = checkCache(cacheName);
cache.removeAll();
}
private static Cache checkCache(String cacheName) {
Cache cache = manager.getCache(cacheName);
if (null == cache) {
throw new IllegalArgumentException("name=["+cacheName+"],不存在对应的缓存组,请查看ehcache.xml");
}
return cache;
}
}