是的,当在生产环境中使用 Ehcache 的磁盘持久化功能时,确实需要在服务器上创建相应的缓存文件夹目录,并确保应用程序有权限读写该目录。 以下是详细说明和配置建议:
1. 为什么需要创建缓存目录?
- Ehcache 的磁盘持久化功能需要将数据写入到磁盘文件中。
- 默认情况下,Ehcache 会尝试在系统临时目录(如 java.io.tmpdir)下创建缓存文件,但这通常不是最佳选择,因为:
- 临时目录可能会被系统清理
- 临时目录空间可能不足
- 权限可能受限
2. 如何配置缓存目录?
方法一:通过 Ehcache 配置文件指定
在 Ehcache 2.x 中,可以在 ehcache.xml 文件中配置磁盘存储路径:
<ehcache>
<!– 指定磁盘存储路径 –>
<diskStore path="/path/to/your/cache/directory"/>
<cache name="myCache"
maxEntriesLocalHeap="1000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="300">
</cache>
</ehcache>
方法二:通过 Java 代码配置(Ehcache 2.x)
Configuration config = new Configuration();
config.diskStore(new DiskStoreConfiguration().path("/path/to/your/cache/directory"));
// 然后创建 CacheManager 和 Cache
方法三:Ehcache 3.x 的配置方式(编程式)
PersistentCacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.with(CacheManagerBuilder.persistence(new File("/path/to/your/cache/directory")))
.withCache("myCache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(100, EntryUnit.ENTRIES)
.disk(100, MemoryUnit.MB, true)) // 启用磁盘持久化
).build(true);
3. 生产环境注意事项
(1) 目录权限
- 确保应用程序运行用户对该目录有读写权限
- 示例(Linux):
mkdir /data/myapp/ehcachechown appuser:appgroup /data/myapp/ehcachechmod 750 /data/myapp/ehcache
(2) 目录规划
- 建议使用专用磁盘或分区
- 避免使用 /tmp 等临时目录
- 示例路径:
- Linux: /data/myapp/ehcache
- Windows: D:\\app\\ehcache
(3) 磁盘空间监控
- 监控缓存目录所在磁盘的空间使用情况
- 设置合理的磁盘配额(通过 Ehcache 的 maxBytesLocalDisk 配置)
(4) 清理策略
- 配置合理的过期时间(TTL/TTI)
- 配置磁盘过期线程运行间隔(diskExpiryThreadIntervalSeconds)
4. 配置示例(Ehcache 2.x 生产配置)
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false" monitoring="autodetect">
<!– 生产环境专用缓存目录 –>
<diskStore path="/data/myapp/ehcache"/>
<!– 默认缓存设置 –>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="300"
maxElementsOnDisk="10000000"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
<!– 自定义缓存 –>
<cache name="productCache"
maxElementsInMemory="5000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="7200"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="600"
maxElementsOnDisk="100000">
</cache>
</ehcache>
5. 服务器准备步骤
部署前需要执行的操作:
mkdir -p /data/myapp/ehcache
chown tomcat:tomcat /data/myapp/ehcachechmod 750 /data/myapp/ehcache
6. 验证配置
启动应用后检查:
- 目录中是否生成 .data 文件(Ehcache 2.x)
- 日志中是否有权限错误
- 通过 JMX 或 Ehcache 监控界面确认磁盘使用情况
总结
在生产环境中使用 Ehcache 磁盘持久化功能时:
详细配置步骤
bash 复制 下载
创建缓存目录
sudo mkdir -p /data/myapp/ehcache
设置权限(根据应用运行用户调整)
sudo chown appuser:appgroup /data/myapp/ehcache sudo chmod 750 /data/myapp/ehcache
2. Ehcache 2.x 配置示例 在 ehcache.xml 中配置磁盘存储路径:
xml 复制 下载 运行
<!– 指定磁盘存储路径 –>
<diskStore path="/data/myapp/ehcache"/>
<!– 默认缓存设置 –>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="300"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
<!– 商品缓存 –>
<cache name="productCache"
maxElementsInMemory="5000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="7200"
diskPersistent="true"
diskSpoolBufferSizeMB="20"
maxElementsOnDisk="100000"
diskExpiryThreadIntervalSeconds="600">
</cache>
3. Ehcache 3.x 配置示例(Java Config) java 复制 下载 import org.ehcache.config.CacheConfiguration; import org.ehcache.config.builders.CacheConfigurationBuilder; import org.ehcache.config.builders.CacheManagerBuilder; import org.ehcache.config.builders.ResourcePoolsBuilder; import org.ehcache.config.units.MemoryUnit; import org.ehcache.impl.config.persistence.CacheManagerPersistenceConfiguration;
import java.io.File;
public class Ehcache3Config {
public static void main(String[] args) {
// 1. 定义缓存目录
File cacheDirectory = new File("/data/myapp/ehcache");
// 2. 创建缓存管理器配置
CacheManagerBuilder<PersistentCacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder()
.with(new CacheManagerPersistenceConfiguration(cacheDirectory));
// 3. 配置具体缓存
ResourcePoolsBuilder resourcePools = ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(1000, EntryUnit.ENTRIES) // 堆内缓存
.disk(100, MemoryUnit.MB, true); // 磁盘持久化
CacheConfiguration<Long, Product> productConfig = CacheConfigurationBuilder
.newCacheConfigurationBuilder(Long.class, Product.class, resourcePools)
.withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofHours(2)))
.build();
// 4. 构建缓存管理器
PersistentCacheManager cacheManager = managerBuilder
.withCache("productCache", productConfig)
.build(true);
// 使用缓存…
}
} 生产环境关键考虑因素
优化建议:
设置合理的堆内缓存:减少磁盘访问频率
使用SSD存储:提升磁盘I/O性能
配置磁盘缓冲池:
xml 复制 下载 运行
<cache … diskSpoolBufferSizeMB="30" /> 定期清理过期数据:
xml 复制 下载 运行 300 3. 监控与维护 关键监控指标:
磁盘空间使用率
磁盘I/O等待时间
缓存命中率(内存 vs 磁盘)
缓存驱逐率
日志配置示例(logback.xml):
xml 复制 下载 运行
4. 灾难恢复策略 缓存目录损坏处理:
bash 复制 下载
停止应用
systemctl stop myapp
备份当前缓存
mv /data/myapp/ehcache /data/myapp/ehcache_bak_$(date +%F)
重建目录
mkdir /data/myapp/ehcache chown appuser:appgroup /data/myapp/ehcache
重启应用
systemctl start myapp 缓存预热脚本(Spring Boot 示例):
java 复制 下载 @Component public class CacheWarmup implements ApplicationRunner {
@Autowired
private ProductService productService;
@Override
public void run(ApplicationArguments args) {
List<Long> hotProductIds = // 从数据库获取热门商品ID
hotProductIds.parallelStream()
.forEach(productService::getProductById);
}
} 5. 安全配置 目录权限加固:
bash 复制 下载
禁止其他用户访问
chmod 750 /data/myapp/ehcache
设置粘滞位(防止文件被误删)
chmod +t /data/myapp/ehcache 加密敏感数据:
java 复制 下载 // Ehcache 3.x 数据加密 CacheConfiguration<Long, Product> secureConfig = CacheConfigurationBuilder .newCacheConfigurationBuilder(Long.class, Product.class, resourcePools) .withValueSerializer(EncryptedSerializer.class) .build(); 容器化环境特殊考虑 在 Docker/Kubernetes 环境中:
使用持久化卷(PV):
yaml 复制 下载
Kubernetes 部署片段
volumeMounts:
- name: ehcache-volume mountPath: /data/myapp/ehcache
volumes:
- name: ehcache-volume persistentVolumeClaim: claimName: ehcache-pvc 初始化容器准备目录:
yaml 复制 下载 initContainers:
- name: init-cache-dir image: busybox command: ["sh", "-c", "mkdir -p /data/myapp/ehcache && chown 1000:1000 /data/myapp/ehcache"] volumeMounts:
- name: ehcache-volume mountPath: /data/myapp/ehcache 总结建议 必须创建专用目录:避免使用临时目录
权限最小化原则:应用用户专属权限
容量监控:防止磁盘写满导致服务故障
性能隔离:SSD 优于 HDD,独立磁盘优于共享磁盘
定期维护:监控日志,清理过期数据
灾难恢复:准备缓存重建和预热方案
按照这些实践部署 Ehcache 磁盘缓存,可以在生产环境中获得安全可靠的持久化缓存能力,同时保持高性能。
评论前必须登录!
注册