使用 CacheEventListener 在 EhCache 中记录驱逐

标签 logging ehcache event-listener

我正在尝试在 Tomcat 6 项目中使用 EhCache。我使用的 EhCache 版本是 1.4,因为它已经在我的 Hibernate 项目中使用。我已经编写了自己的自定义 CacheEventListener(包含在下面),除了 notifyElementExpired() 的一个问题之外,它可以正常工作。似乎只有当我向缓存添加其他内容时才会调用 notifyElementExpired() 。附带说明一下,notifyElementPut() 正在被调用。

有人可以提出解决方案吗?

public class EhCacheEventListener implements CacheEventListener {
  private Logger log4j =
    LoggerFactory.getLogger(this.getClass().getPackage().getName());   

  public void notifyElementRemoved(Ehcache cache, Element element) throws CacheException {
    log4j.debug("cache element removed---->"+element.getKey());
  }

  public void notifyElementPut(Ehcache cache, Element element) throws CacheException {
    log4j.debug("cache element put---->"+element.getKey());
  }

  public void notifyElementUpdated(Ehcache cache, Element element) throws CacheException {
    log4j.debug("cache element updated---->"+element.getKey());
  }

  @Override
  public void notifyElementExpired(Ehcache cache, Element element) {
    //log4j.debug("Element creation time is "+element.getCreationTime());
    log4j.debug("cache element expired---->"+element.getKey());
    //log4j.debug("Element expiry time is "+element.getExpirationTime());
  }

  @Override
  public void notifyElementEvicted(Ehcache cache, Element element) {
    log4j.debug("cache element evicted---->"+element.getKey());
  }

  public void notifyRemoveAll(Ehcache cache) {}

  public void dispose() {}

  public Object clone(){
    throw new UnsupportedOperationException("Not supported yet.");
  }
}

最佳答案

在以下时间检查 ehcache 中的元素是否过期:

当发出 get 请求时 当元素根据 MemoryStore 逐出策略 假脱机到 diskStore 时。 在 DiskStore 中,当到期线程运行时,默认情况下为

Cache.DEFAULT_EXPIRY_THREAD_INTERVAL_SECONDS

更多详情请查看:ehCache Documentation

关于使用 CacheEventListener 在 EhCache 中记录驱逐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14485933/

相关文章:

java - 如何获取 Ehcache 3 中的缓存名称列表

java - 如何在不使用 hibernate/spring 拦截器的情况下为独立 Java 程序配置 EHcache?

javascript - Iframe/Javascript 检查 Youtube 视频是否结束

azure - ASP.NET Core 3.1 无法在 Azure 服务日志流上查看我的日志

Grails 日志记录 - 只记录我的应用程序,不记录 Grails 特定的垃圾

java - ehcache 和日志记录问题

java - 在没有 XML 配置的情况下将 Ehcache CacheManager (v 3.x) 转换为 Spring CacheManager

Java - num1 = event.getActionCommand() 不起作用(无法将字符串设置为 Int)

json - ZF2 查看策略

Python 仅记录来自脚本的日志