Java 如何出于测试目的关闭ehcache
我有一个ehcache的实现,它不使用xml中的任何配置,但如下所示:Java 如何出于测试目的关闭ehcache,java,ehcache,Java,Ehcache,我有一个ehcache的实现,它不使用xml中的任何配置,但如下所示: private Ehcache cache; constructor { cache = CacheManager.getInstance().addCacheIfAbsent("nameOfCache"); CacheConfiguration config = cache.getCacheConfiguration(); config.eternal("false") config.setTimeToL
private Ehcache cache;
constructor {
cache = CacheManager.getInstance().addCacheIfAbsent("nameOfCache");
CacheConfiguration config = cache.getCacheConfiguration();
config.eternal("false")
config.setTimeToLiveSeconds(<someTime>);
}
私有Ehcache;
建造师{
cache=CacheManager.getInstance().addCacheIfAbsent(“nameOfCache”);
CacheConfiguration=cache.getCacheConfiguration();
config.external(“假”)
config.setTimeToLiveSeconds();
}
现在,我以前使用过guava缓存,我知道在guava缓存中将逐出时间设置为
0
,可以关闭缓存。但通过阅读有关ehcache的文档,我了解到情况并非如此。是否有其他方法关闭缓存?不知道如何为Ehcache执行此操作,但我们在我的项目中也做了类似的操作。首先,我们为cache编写了一个facade模式(允许在其后面使用Ehcache或Guava cache)。然后我实现了一个OptionalCache
,它将Nullable
缓存或缓存生成器作为构造函数。如果缓存存在,OptionalCache
使用它,否则它不会使用它(使用CacheLoader
获取值或返回null
)。这允许我们将缓存/缓存生成器的null传递给测试。从中,您可以设置net.sf.ehcache.disabled
系统属性以禁止ehcache向缓存添加元素。由于不会向缓存中添加任何元素,因此每个缓存请求都会导致缓存未命中,从而提供您正在查找的行为
如果从命令行启动JVM,可以如下设置属性:
-Dnet.sf.ehcache.disabled=true