【问题标题】:How to Evict less frequently used cache region periodicly?如何定期驱逐不常用的缓存区域?
【发布时间】:2014-12-11 16:40:50
【问题描述】:

据此 Ehcache_Configuration_Guide.pdf

配置如何影响元素刷新和驱逐
以下示例显示了具有某些过期设置的缓存:
<cache name="myCache"
eternal="false" timeToIdleSeconds="3600"
timeToLiveSeconds="0" memoryStoreEvictionPolicy="LFU">
</cache>
请注意有关 myCache 配置的以下几点:
如果客户端访问 myCache 中已空闲超过一个小时 timeToIdleSeconds) 的条目,则该元素将被逐出。
如果条目过期但未被访问,并且没有资源限制强制驱逐,则过期条目将保留在原位,直到定期驱逐者将其移除。

我没有找到任何关于如何定期配置驱逐的例子,

它是可配置的吗? 还是必须对休眠进行硬编码?

据此页面ExpiryTaskExtension.java ehCache 中没有默认的时间驱逐时间表

【问题讨论】:

标签: java hibernate ehcache


【解决方案1】:
import java.util.Timer;
import java.util.TimerTask;
import net.sf.ehcache.CacheManager;

public class EhCacheExpiryTask {
    class ExpiryTask extends TimerTask{
        @Override
        public void run() {
            for (CacheManager manager : CacheManager.ALL_CACHE_MANAGERS) {
                for (String name : manager.getCacheNames()) {
                    //manager.getCache(name).getCacheConfiguration().setStatistics(true);
                    manager.getCache(name).evictExpiredElements();                 
                } 
            }        
        }
    }
    private Timer timer;
    private long period;
    private ExpiryTask expiryTask;
    public EhCacheExpiryTask(long period){
        this.period = period;
        this.timer = new Timer();
    } 
    public void stop(){
        timer.cancel();
    }
    public void start(){
        expiryTask = new ExpiryTask();      
        timer.schedule(expiryTask, 10000, period);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    相关资源
    最近更新 更多