java – 如何为测试目的关闭ehcache

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

private Ehcache cache;

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

现在,我之前使用过番石榴缓存,我知道通过在番石榴缓存中将驱逐时间设置为0,它会关闭缓存.但是通过阅读有关ehcache的文档,我理解它不是同一个案例.还有其他方法可以关闭缓存吗?

解决方法:

the Ehcache FAQ开始,您可以设置net.sf.ehcache.disabled系统属性以禁止Ehcache将元素添加到缓存.由于没有元素将添加到缓存中,因此每个缓存请求都会导致缓存未命中,从而提供您正在寻找的行为.

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

-Dnet.sf.ehcache.disabled=true
上一篇:Shiro如何使用Ehcache实现Session共享


下一篇:关于ehcache缓存中eternal及timeToLiveSeconds和timeToIdleSeconds的说明