本文介绍了基于日期的ehcache的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ehcache 2.5.4。

我有一个对象需要在一天中进行缓存,并在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 = ...;
私人最后一次evictOn;

EvictOnGivenTimestampElement(最终可序列化键,最终可序列化值,最终长evictOn){
super(key,value);
this.evictOn = evictOn;


@Override
public boolean isExpired(){
return System.currentTimeMillis()> evictOn;
}
}

剩下的就像将新的 EvictOnGivenTimestampElement 对象,而不是元素



这种方法是你不必担心外部cronjob等
而明显的缺点是附件Ehcache API,我希望不会经常更改。

I'm working with ehcache 2.5.4.

I have an object that needs to be cached through out the day and refreshed with a new value at 00:00am every day.

Currently with ehcache configurations I can only set the time to live and time to idle, but that will depend on the time I created the object or when it's used. ie:

    <cache
    name="cache.expiry.application.date_status"
    maxElementsInMemory="10"
    eternal="false"
    timeToIdleSeconds="60"
    timeToLiveSeconds="50" />

Is there a way to get ehcache to expire specific caches based on specific times.

解决方案

I've done this by extending Ehcache's Element class like so:

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;
    }
}

The rest is as easy as putting new instance of EvictOnGivenTimestampElement object into the cache instead of Element.

Advantage of this approach is that you don't have to worry about external cronjobs, etc. And the obvious disadvantage is the attachment to Ehcache API which I hope won't change too often.

这篇关于基于日期的ehcache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 02:15