我正在使用 ehcache 2.5.4。
我有一个对象需要全天缓存并在每天凌晨 00:00 刷新一个新值。
目前,对于 ehcache 配置,我只能设置生存时间和闲置时间,但这取决于我创建对象的时间或对象的使用时间。即:
<cache
name="cache.expiry.application.date_status"
maxElementsInMemory="10"
eternal="false"
timeToIdleSeconds="60"
timeToLiveSeconds="50" />
有没有办法让 ehcache 根据特定时间使特定缓存过期。
最佳答案
我通过扩展 Ehcache 的 Element
类来做到这一点:
class EvictOnGivenTimestampElement extends Element {
private static final long serialVersionUID = ...;
private final long evictOn;
EvictOnGivenTimestampElement(final Serializable key, final Serializable value, final long evictOn) {
super(key, value);
this.evictOn = evictOn;
}
@Override
public boolean isExpired() {
return System.currentTimeMillis() > evictOn;
}
}
剩下的就像将 EvictOnGivenTimestampElement
对象的新实例而不是 Element
放入缓存中一样简单。
这种方法的优点是您不必担心外部 cronjobs 等。 明显的缺点是对 Ehcache API 的附件,我希望它不会经常更改。
关于java - 基于日期的ehcache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13942814/