JCS通知过期/删除 [英] JCS notify on expire/remove
问题描述
我们非常简单地使用JCS.没有分发或任何东西,简单地:
we use JCS very simply. Not distributed or anything, simply:
JCS jcs = JCS.getInstance("region-name");
我正在尝试注册某种侦听器,当从缓存中删除元素或使元素过期时,该侦听器可用于接收通知/事件...
I'm trying to register some kind of listener that can be used to receive a notification/event when an element is removed or expired from the cache...
我已经研究了JCS javadoc一段时间了,并且尝试了:
-在高速缓存的默认ElementAttributes中添加IElementEventHandler
的实现...永远不会被调用.
-使用ICacheObserver
的各种实现来注册ICacheListener
,但是永远不会被调用.我不一定要确定这一点是正确的方法,因为我认为这是为了更高级地使用JCS ...
I've been digging through the JCS javadoc for awhile now and I've tried:
- adding an Implementation of IElementEventHandler
to the default ElementAttributes of the cache ... it never gets called.
- using the various implementations of ICacheObserver
to register an ICacheListener
but that never gets called either. I'm not necessarily sure this point is the correct way of doing it as I think this is intended for more advanced uses of JCS ...
有人知道(或是否有可能)注册某种类型的侦听器/观察者/什么东西来完成此任务?我的最终目标是基本上可以从缓存中删除某些内容时得到通知...我并不特别在乎它是不是一个巨大的麻烦:P
Does anyone know how (or if it's possible) to register some kind of listener/obsverver/whatever that will accomplish this? My final goal is to be able to be notified of when something is removed from the cache basically ... I don't particularly care about how provided it isn't a massive kludge :P
推荐答案
创建一个抽象类,注册您有兴趣捕获的事件.这对我有用,以捕获这两个事件.
Create an abstract class that registers the events your interested in capturing. This works for me to capture the two events.
private static final Set<Integer> EVENTS = new HashSet<Integer>();
{
EVENTS.add(IElementEventHandler.ELEMENT_EVENT_EXCEEDED_IDLETIME_BACKGROUND);
EVENTS.add(IElementEventHandler.ELEMENT_EVENT_EXCEEDED_MAXLIFE_BACKGROUND);
}
@Override
public synchronized void handleElementEvent(IElementEvent event) {
// Check for element expiration based on EVENTS.
LOG.debug("Handling event of type : " + event.getElementEvent() + ".");
if (EVENTS.contains(event.getElementEvent())) {
ElementEvent elementEvent = (ElementEvent)event;
CacheElement element = (CacheElement)elementEvent.getSource();
handleEvent(element);
}
}
// Abstract method to handle events
protected abstract void handleEvent(CacheElement element);
}
按如下所示将此抽象事件处理程序添加到jcs工厂定义中
Add this abstract event handler to the jcs factory definition as follows
JCS jcs = JCSCacheFactory.getCacheInstance(regionName);
IElementAttributes attributes = jcs.getDefaultElementAttributes();
attributes.addElementEventHandler(handler);
jcs.setDefaultElementAttributes(attributes);
这篇关于JCS通知过期/删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!