我有一个 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/