【问题标题】:How to turn off ehcache for test purposes如何关闭 ehcache 以进行测试
【发布时间】:2013-03-14 09:15:05
【问题描述】:

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

private Ehcache cache;

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

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

【问题讨论】:

    标签: java ehcache


    【解决方案1】:

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

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

    -Dnet.sf.ehcache.disabled=true
    

    【讨论】:

      【解决方案2】:

      不确定如何为 Ehcache 执行此操作,但我们在我的项目中做了类似的事情。首先,我们为缓存编写了一个外观模式(以允许在其后面使用 Ehcache 或 Guava 缓存)。然后我实现了一个OptionalCache,它采用Nullable 缓存或缓存构建器作为构造函数。如果缓存存在,OptionalCache 会使用它,否则不会(使用CacheLoader 获取值或返回null)。这允许我们为缓存/缓存构建器传递 null 以进行测试。

      【讨论】:

        猜你喜欢
        • 2017-12-11
        • 2013-04-23
        • 1970-01-01
        • 2011-05-28
        • 1970-01-01
        • 2012-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多