ehcache是一个纯Java实现的进程内缓存实现,具有快速精简等特点。有单机版本、分布式实现版本。主要针对基于java开发的项目使用。支持磁盘持久化及磁盘load到内存。
介绍
EhCache是基于Java的开源缓存,有很好的性能,可扩展。因为功能强大、经过测试的、功能全而广泛的应用与Java开发的系统中。支持进程内、混合进程内/进程外继承部署。
- 1.1 特点
1、单机版本的ehcache是jvm进程内缓存,不走网卡,速度快、效率高。
2、冷热数据单独处理不方便,正常情况下数据都是放在内存中,超过配置阈值后才会进行持久化磁盘处理。
3、数据的持久化需要在配置文件中配置才会进行,否则ehcache关闭后会删除掉缓存的磁盘文件。
4、如果项目中缓存分类比较多,分类又需要单独配置参数的情况,则配置文件就会比较大,比较麻烦。
5、默认依赖于classpath下的ehcache.xml配置文件,如果名称不同则需要明确指明配置文件。
- 1.2 maven配置
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.3</version>
</dependency>
// 创建一个manager对象
final CacheManager cacheManager = new CacheManager();
// 创建一个cache对象,类似与map
final Cache cache = cacheManager.getCache("hello-world");
// 定义一个缓存对象的key值
final String key = "greeting";
// 创建一个缓存的基本存储对象Element
final Element putGreeting = new Element(key, "Hello, World!");
// 把实际的缓存对象放入到cache对象中去
cache.put(putGreeting);
// 从缓存中读取缓存内容
final Element getGreeting = cache.get(key);
// 打印缓存的内容
System.out.println(getGreeting.getObjectValue());
- 1.4 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" name="defaultCache">
<diskStore path="../temp/lehoon/ehcache"/>
<!--
maxEntriesLocalHeap:堆内存中最大缓存对象数
eternal:对象是否永久有效,一但设置了,timeout将不起作用
overflowToDisk:当缓存达到maxElementsInMemory值是,是否允许溢出到磁盘
timeToIdleSeconds:当缓存闲置n秒后销毁
timeToLiveSeconds:当缓存存活n秒后销毁
maxEntriesLocalDisk:硬盘最大缓存个数
diskPersistent:磁盘缓存在JVM重新启动时是否保持
-->
<!-- 默认缓存配置-当不配置缓存信息时使用此配置 -->
<defaultCache
maxEntriesLocalHeap="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="0"
timeToLiveSeconds="86400"
maxEntriesLocalDisk="100000"
diskPersistent="false">
</defaultCache>
<!-- 系统缓存 -->
<cache
name="SYS_CACHE"
maxEntriesLocalHeap="10000"
eternal="false"
timeToLiveSeconds="86400"
maxEntriesLocalDisk="10000000"
overflowToDisk="true"
timeToIdleSeconds="0"
diskPersistent="false">
</cache>
<!-- 订单缓存 -->
<cache
name="BILLING"
maxEntriesLocalHeap="10000"
eternal="true"
overflowToDisk="true"
timeToIdleSeconds="0"
diskPersistent="true">
<bootstrapCacheLoaderFactory
class="net.sf.ehcache.store.DiskStoreBootstrapCacheLoaderFactory"
properties="bootstrapAsynchronously=true">
</bootstrapCacheLoaderFactory>
</cache>
</ehcache>
使用常见问题
- 2.1 删除缓存对象方法
删除缓存cache对象的时候,使用CacheManager.removeCache方法,不能直接delete删除。
/**
* 删除cache对象
* @param ehCacheName
*/
private void removeCache(Object ehCacheName) {
String name = ehCacheName.toString();
if (cacheManager == null) {
this.cacheManager = (CacheManager) SpringContextHolder.getBean("cacheManager");
}
cacheManager.removeCache(name);
}
- 2.2 检查缓存是否可用状态
@Override
public boolean isAlive() {
if (cacheManager == null) {
cacheManager = (CacheManager) SpringContextHolder.getBean("cacheManager");
}
int status = cacheManager.getStatus().intValue();
return status == Status.STATUS_ALIVE.intValue();
}
- 2.3 缓存持久化及加载
缓存持久化是从内存持久化到磁盘,默认ehcache停止的时候会删除掉运行期间落盘的磁盘文件,因此需要配置告诉ehcache停止时候不需要删除缓存文件,配置参数如下:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" name="defaultCache">
<diskStore path="../temp/lehoon/ehcache"/>
<!--
maxEntriesLocalHeap:堆内存中最大缓存对象数
eternal:对象是否永久有效,一但设置了,timeout将不起作用
overflowToDisk:当缓存达到maxElementsInMemory值是,是否允许溢出到磁盘
timeToIdleSeconds:当缓存闲置n秒后销毁
timeToLiveSeconds:当缓存存活n秒后销毁
maxEntriesLocalDisk:硬盘最大缓存个数
diskPersistent:磁盘缓存在JVM重新启动时是否保持
-->
<!-- 订单缓存,通过eternal=true配持久化缓存文件 -->
<cache
name="BILLING"
maxEntriesLocalHeap="10000"
eternal="true"
overflowToDisk="true"
timeToIdleSeconds="0"
diskPersistent="true">
</cache>
</ehcache>
持久化的缓存在ehcache启动的时候加载到内存中去,需要增加配置bootstrapCacheLoaderFactory属性,如下
<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" name="defaultCache">
<diskStore path="../temp/lehoon/ehcache"/>
<!--
maxEntriesLocalHeap:堆内存中最大缓存对象数
eternal:对象是否永久有效,一但设置了,timeout将不起作用
overflowToDisk:当缓存达到maxElementsInMemory值是,是否允许溢出到磁盘
timeToIdleSeconds:当缓存闲置n秒后销毁
timeToLiveSeconds:当缓存存活n秒后销毁
maxEntriesLocalDisk:硬盘最大缓存个数
diskPersistent:磁盘缓存在JVM重新启动时是否保持
-->
<!-- 订单缓存 通过bootstrapCacheLoaderFactory参数配置启动加载到内存 -->
<cache
name="BILLING"
maxEntriesLocalHeap="10000"
eternal="true"
overflowToDisk="true"
timeToIdleSeconds="0"
diskPersistent="true">
<bootstrapCacheLoaderFactory
class="net.sf.ehcache.store.DiskStoreBootstrapCacheLoaderFactory"
properties="bootstrapAsynchronously=true">
</bootstrapCacheLoaderFactory>
</cache>
</ehcache>
本文暂时没有评论,来添加一个吧(●'◡'●)