本文主要介绍 Ehacche2 的基本使用,文中所使用到的软件版本:Java 1.8.0_341、Ehcache 2.10.9.2。
1、引入依赖
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.9.2</version>
</dependency>
2、缓存配置和创建
2.1、XML 方式配置缓存
A、在 src/main/resources 目录下新建 ehcache2.xml:
<ehcache>
<diskStore path="d:/temp"/>
<defaultCache
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
<cache name="myCache"
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
</ehcache>
defaultCache 表示缓存的默认配置,cache 表示配置的具体缓存。
B、加载配置
@Test
public void test01() {
//加载配置文件
CacheManager cacheManager = new CacheManager(getClass().getClassLoader().getResourceAsStream("ehcache2.xml"));
Cache myCache = cacheManager.getCache("myCache");
Element element = new Element(1, "aaa");
myCache.put(element);
Element element2 = myCache.get(1);
log.info(element2.getObjectValue() + "");
myCache.remove(1);
log.info(myCache.get(1) + "");
cacheManager.shutdown();
}
2.2、API 方式配置缓存
@Test
public void test02() {
//CacheManager 配置
Configuration configuration = new Configuration();
DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration();
diskStoreConfiguration.setPath("d:/temp");
configuration.diskStore(diskStoreConfiguration);
CacheManager cacheManager = new CacheManager(configuration);
//Cache 配置
CacheConfiguration cacheConfiguration = new CacheConfiguration();
cacheConfiguration.setName("myCache");
cacheConfiguration.setMaxEntriesLocalHeap(1000);
cacheConfiguration.setMaxEntriesLocalDisk(10000);
cacheConfiguration.setTimeToIdleSeconds(60);
cacheConfiguration.setTimeToLiveSeconds(120);
cacheConfiguration.setMemoryStoreEvictionPolicy("LFU");//缓存对象清除策略
cacheConfiguration.setDiskExpiryThreadIntervalSeconds(120);
cacheConfiguration.setEternal(false);
PersistenceConfiguration persistenceConfiguration = new PersistenceConfiguration();
persistenceConfiguration.setStrategy("LOCALTEMPSWAP");
cacheConfiguration.persistence(persistenceConfiguration);
Cache cache = new Cache(cacheConfiguration);
cacheManager.addCache(cache);
//获取缓存并使用
Cache myCache = cacheManager.getCache("myCache");
Element element = new Element(1, "aaa");
myCache.put(element);
Element element2 = myCache.get(1);
log.info(element2.getObjectValue() + "");
myCache.remove(1);
log.info(myCache.get(1) + "");
cacheManager.shutdown();
}
完整代码:
package com.abc.demo.cache;
import lombok.extern.slf4j.Slf4j;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration;
import net.sf.ehcache.config.DiskStoreConfiguration;
import net.sf.ehcache.config.PersistenceConfiguration;
import org.junit.Test;
@Slf4j
public class Ehcache2Case {
@Test
public void test01() {
//加载配置文件
CacheManager cacheManager = new CacheManager(getClass().getClassLoader().getResourceAsStream("ehcache2.xml"));
Cache myCache = cacheManager.getCache("myCache");
Element element = new Element(1, "aaa");
myCache.put(element);
Element element2 = myCache.get(1);
log.info(element2.getObjectValue() + "");
myCache.remove(1);
log.info(myCache.get(1) + "");
cacheManager.shutdown();
}
@Test
public void test02() {
//CacheManager 配置
Configuration configuration = new Configuration();
DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration();
diskStoreConfiguration.setPath("d:/temp");
configuration.diskStore(diskStoreConfiguration);
CacheManager cacheManager = new CacheManager(configuration);
//Cache 配置
CacheConfiguration cacheConfiguration = new CacheConfiguration();
cacheConfiguration.setName("myCache");
cacheConfiguration.setMaxEntriesLocalHeap(1000);
cacheConfiguration.setMaxEntriesLocalDisk(10000);
cacheConfiguration.setTimeToIdleSeconds(60);
cacheConfiguration.setTimeToLiveSeconds(120);
cacheConfiguration.setMemoryStoreEvictionPolicy("LFU");//缓存对象清除策略
cacheConfiguration.setDiskExpiryThreadIntervalSeconds(120);
cacheConfiguration.setEternal(false);
PersistenceConfiguration persistenceConfiguration = new PersistenceConfiguration();
persistenceConfiguration.setStrategy("LOCALTEMPSWAP");
cacheConfiguration.persistence(persistenceConfiguration);
Cache cache = new Cache(cacheConfiguration);
cacheManager.addCache(cache);
//获取缓存并使用
Cache myCache = cacheManager.getCache("myCache");
Element element = new Element(1, "aaa");
myCache.put(element);
Element element2 = myCache.get(1);
log.info(element2.getObjectValue() + "");
myCache.remove(1);
log.info(myCache.get(1) + "");
cacheManager.shutdown();
}
}
Ehcache2Case.java
3、Ehcache2 配置参数说明
参数 | 子参数 | 说明 | 默认值 |
diskStore | | 磁盘配置 | |
| path | 数据存储目录 | java.io.tmpdir |
name | | 缓存名称 | |
maxEntriesLocalHeap | | 内存中可以存放的最大条目数,0 表示无限制 | 0 |
maxEntriesLocalDisk | | 磁盘上可以存放的最大条目数,0 表示无限制 | 0 |
timeToIdleSeconds | | 缓存中条目的最大空闲时间,0 表示无限制 | 0 |
timeToLiveSeconds | | 缓存中条目的最大存活时间,0 表示无限制 | 0 |
eternal | | 缓存条目是否永不过期;如果为 true,将忽略过期时间(timeToIdleSeconds,timeToLiveSeconds) | false |
memoryStoreEvictionPolicy | | 缓存对象清除策略:FIFO,LFU,LRU | LRU |
diskExpiryThreadIntervalSeconds | | 缓存条目过期清理的时间间隔 | 120 |
persistence | | 持久化配置 | |
| strategy | 持久策略:localTempSwap,localRestartable,none,distributed | |
3.1、缓存对象清除策略
FIFO(First In First Out):先进先出
LFU(Least Frequently Used):最少使用,使用次数最少的条目将被清理
LRU(Least Recenly Used):最近最少使用,最近一段时间内使用次数最少的条目将被清理
3.2、持久策略
localTempSwap:当内存中的条目已经满了的时候,将条目临时存放在磁盘上,一旦重启就会消失。
localRestartable:该策略只对企业版 Ehcache 有用,它可以将内存里面的条目持久化到硬盘上,重启之后再从硬盘上恢复到内存中。
none:不持久化缓存的条目。
distributed:该策略是用于分布式情况下的。