我正在尝试在 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/