java - 如何出于测试目的关闭 ehcache

标签 java ehcache

我有一个 ehcache 的实现,它不使用 xml 中的任何配置,但如下所示:

private Ehcache cache;

constructor {
  cache = CacheManager.getInstance().addCacheIfAbsent("nameOfCache");
  CacheConfiguration config = cache.getCacheConfiguration();
  config.eternal("false")
  config.setTimeToLiveSeconds(<someTime>);
}

现在,我以前使用过 guava 缓存,我知道通过在 guava 缓存中将驱逐时间设置为 0,它会关闭缓存。但是通过阅读有关 ehcache 的文档,我了解到情况不同。有没有其他方法可以关闭缓存?

最佳答案

来自 the Ehcache FAQ , 你可以设置 net.sf.ehcache.disabled 系统属性来禁止 Ehcache 添加元素到缓存。因为不会将任何元素添加到缓存中,所以每个缓存请求都会导致缓存未命中,从而提供您正在寻找的行为。

如果您从命令行启动 JVM,您可以像这样设置属性:

-Dnet.sf.ehcache.disabled=true

关于java - 如何出于测试目的关闭 ehcache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15390845/

相关文章:

java - JPA、Hibernate 和 Oracle 函数索引

java - Android key 对生成

java - 如何通过 BigIntegers 索引 BigIntegers 数组

java - 如何在没有 xml 的情况下配置 Ehcache 3 + spring boot + java config?

java - Spring事务感知缓存不起作用

java - ehcache缓存 "streams"的数据

java - 如何在服务器上编译我的服务器代码?

java - 文件传输功能在第一次成功后卡住

java - 为 Spring 的 @Cacheable 注释定义键的最佳方法是什么

java - 每次写入时 ehcache 都会刷新到磁盘