请教ehcache的notifyElementExpired的事件用法

zhangzining 发布于 2012/05/01 17:20
阅读 3K+
收藏 3
我想用ehcache的notifyElementExpired过期事件, 我的需求是这样的:把对象放cache里,超过固定事件移除,移除时我能获取到一个事件。 请教用ehcache的过期事件能实现吗?如果不能有什么合适的框架吗?谢谢!
加载中
0
红薯
红薯
配置方式
<cache
	name="orderCache"
	maxElementsInMemory="10000"
	eternal="false"
	timeToIdleSeconds="120"
	timeToLiveSeconds="0"
	overflowToDisk="true">
	<!-- 缓存监听器,监听缓存元素的过期、销毁等 -->
	<cacheEventListenerFactory class="xxxx.CacheEventListenerFactoryImpl"/>
</cache> 
0
z
zhangzining

我试过了,但是notifyElementExpired 这个方法在对象过期时并没有被调用,而是在获取这个对象时调用了。难道不框架在对象过期时不能主动通知我吗?

0
HuaChen
HuaChen
同有上面疑问 为什么?
0
HuaChen
HuaChen

我试过了,但是notifyElementExpired 这个方法在对象过期时并没有被调用,而是在下次获取这个对象时调用了。难道不框架在对象过期时不能主动通知我吗???

谁有解决办法没有?

0
HuaChen
HuaChen
    /**
     * Called immediately after an element is <i>found</i> to be expired. The
     * {@link  net.sf.ehcache.Cache#remove(Object)} method will block until this method returns.
     * <p/>
     * As the {@link  Element} has been expired, only what was the key of the element is known.
     * <p/>
     * Elements are checked for expiry in ehcache at the following times:
     * <ul>
     * <li>When a get request is made
     * <li>When an element is spooled to the diskStore in accordance with a MemoryStore
     * eviction policy
     * <li>In the DiskStore when the expiry thread runs, which by default is
     * {@link  net.sf.ehcache.Cache#DEFAULT_EXPIRY_THREAD_INTERVAL_SECONDS}
     * </ul>
     * If an element is found to be expired, it is deleted and this method is notified.
     *
     * @param cache   the cache emitting the notification
     * @param element the element that has just expired
     *                <p/>
     *                Deadlock Warning: expiry will often come from the <code>DiskStore</code>
     *                expiry thread. It holds a lock to the DiskStorea the time the
     *                notification is sent. If the implementation of this method calls into a
     *                synchronized <code>Cache</code> method and that subsequently calls into
     *                DiskStore a deadlock will result. Accordingly implementers of this method
     *                should not call back into Cache.
     */
    void notifyElementExpired(final Ehcache cache, final Element element);

 

以上是 Ehcache notifyElementExpired 接口的说明,看来触发条件有2个

1、When a get request is made

2、When an element is spooled to the diskStore in accordance with a MemoryStore

     eviction policy

 

请问第二个是什么意思? 怎么配置能满足第二条,让题目所示需求能够满足?

0
HuaChen
HuaChen
第二个的意思是   缓存满了时做检查。 比如你 内存中限制放 1000个 ,第1001个缓存对象时,做一次全面检查。
0
HuaChen
HuaChen

没办法,自己做了一个quartz ,每隔 30S 主动调用一下检查。

/**
	 * 检查 缓存中过期元素
	 * @author  ChenHua
	 * create on 2012-9-11  上午10:11:56
	 */
	public void cacheElementExpiredCheck(){
		Ehcache alarmCache=((Ehcache)SpringContextHelper.getBean(CacheBeanName.ALARM_RECORD_CACHE));
		alarmCache.evictExpiredElements(); //目前先只对 报警缓存做主动检查
	}

0
finikes二世
finikes二世
对象过期是 notifyElementEvicted方法
Romotc
Romotc
evicted 是被驱赶到Disk时调用 楼主说的这个问题,我刚才试了一下是可以调用的。 只不过是在有缓存操作才会调用,如果ehcache处于静态,没有进程读取ehcache,ehcache是不会主动检查缓存失效的。
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部