ehcache是Java领域广泛使用的开源本地缓存框架,能够以轻量高效的方式为应用提供内存级或磁盘级的缓存能力,而ehcache.xml就是该框架的核心配置文件,所有缓存的运行规则都通过这份文件定义。

ehcache.xml是什么
ehcache.xml是ehcache框架的专属配置文件,用来声明缓存管理器的基础属性、定义不同的缓存区域以及每个缓存区域的独立运行规则。当Java应用启动时,ehcache会默认从classpath根目录加载ehcache.xml文件,解析其中的配置项并初始化对应的缓存实例。如果没有自定义ehcache.xml,框架会使用内置的默认配置,但默认配置通常无法满足生产环境的个性化需求,因此实际开发中都需要自定义这份配置文件。
ehcache.xml的核心配置项说明
根标签与缓存管理器配置
ehcache.xml的根标签是<ehcache>,可以配置全局属性,比如磁盘存储路径、更新检查开关等,常用属性如下:
- diskStorePath:指定缓存数据持久化到磁盘时的存储目录,当内存缓存不足时,ehcache会把部分数据溢出到该目录
- updateCheck:是否检查框架新版本,生产环境建议设置为false避免不必要的网络请求
缓存区域配置标签<cache>
每个<cache>标签对应一个独立的缓存区域,不同业务可以使用不同的缓存区域隔离数据,常用配置参数如下:
| 参数名 | 含义 | 默认值 |
|---|---|---|
| name | 缓存区域的唯一标识,Java代码中通过此名称获取缓存实例 | 无,必填项 |
| maxEntriesLocalHeap | 堆内存中最多存储的缓存条目数,超过后会触发淘汰策略 | 0,表示不限制 |
| timeToIdleSeconds | 缓存条目的最大空闲时间,单位秒,超过该时间未被访问则过期 | 0,表示不过期 |
| timeToLiveSeconds | 缓存条目的最大存活时间,单位秒,从创建时开始计算,到期自动过期 | 0,表示不过期 |
| overflowToDisk | 内存不足时是否将缓存数据溢出到磁盘 | false |
| diskPersistent | 应用重启后是否保留磁盘上的缓存数据 | false |
| memoryStoreEvictionPolicy | 内存满时的淘汰策略,可选值有LRU(最近最少使用)、LFU(最不经常使用)、FIFO(先进先出) | LRU |
完整ehcache.xml配置示例
以下是一个适配常见业务场景的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"
diskStorePath="java.io.tmpdir/ehcache_data"
updateCheck="false">
<!-- 默认缓存配置,未被单独定义的缓存区域会沿用此配置 -->
<defaultCache
maxEntriesLocalHeap="1000"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/>
<!-- 用户缓存区域,专门存储用户相关信息 -->
<cache name="userCache"
maxEntriesLocalHeap="5000"
timeToIdleSeconds="1800"
timeToLiveSeconds="3600"
overflowToDisk="true"
diskPersistent="false"
memoryStoreEvictionPolicy="LRU"/>
<!-- 商品缓存区域,专门存储商品相关信息 -->
<cache name="productCache"
maxEntriesLocalHeap="10000"
timeToIdleSeconds="3600"
timeToLiveSeconds="7200"
overflowToDisk="true"
diskPersistent="true"
memoryStoreEvictionPolicy="LFU"/>
</ehcache>
Java代码中调用配置的缓存策略
配置好ehcache.xml后,需要在Java代码中加载配置并使用对应的缓存,以下是基于ehcache 3.x版本的示例代码:
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.xml.XmlConfiguration;
import java.net.URL;
public class EhcacheDemo {
public static void main(String[] args) {
// 加载classpath下的ehcache.xml配置文件
URL configUrl = EhcacheDemo.class.getClassLoader().getResource("ehcache.xml");
XmlConfiguration xmlConfiguration = new XmlConfiguration(configUrl);
// 创建缓存管理器
CacheManager cacheManager = CacheManagerBuilder.newCacheManager(xmlConfiguration);
cacheManager.init();
// 获取名为userCache的缓存区域,键类型为String,值类型为User对象
Cache<String, User> userCache = cacheManager.getCache("userCache", String.class, User.class);
// 写入缓存数据
User user = new User("1001", "张三", 25);
userCache.put("user_1001", user);
// 读取缓存数据
User cachedUser = userCache.get("user_1001");
System.out.println("从缓存中获取的用户:" + cachedUser.getName());
// 关闭缓存管理器
cacheManager.close();
}
// 内部用户类
static class User {
private String id;
private String name;
private int age;
public User(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
}
配置注意事项
在配置和使用ehcache.xml时需要注意以下几点:
- ehcache.xml必须放在Java项目的classpath根目录下,比如Maven项目的src/main/resources目录,否则框架无法自动加载
- 如果同时存在ehcache.xml和ehcache-failsafe.xml,框架会优先加载ehcache.xml
- 磁盘存储路径需要确保应用有读写权限,否则会导致溢出到磁盘失败
- 缓存的过期时间需要根据业务数据的更新频率合理设置,避免缓存数据长时间不一致
ehcacheehcache_xmlJava缓存缓存策略修改时间:2026-06-18 22:09:26