JCS 过期/删除通知

发布于 2024-10-07 19:59:53 字数 501 浏览 2 评论 0原文

我们使用 JCS 非常简单。没有分布式或任何东西,简单地说:

JCS jcs = JCS.getInstance("region-name");

我正在尝试注册某种侦听器,该侦听器可用于在元素从缓存中删除或过期时接收通知/事件...

我一直在挖掘 JCS javadoc一段时间了,我尝试过: - 将 IElementEventHandler 的实现添加到缓存的默认 ElementAttributes ...它永远不会被调用。 - 使用 ICacheObserver 的各种实现来注册 ICacheListener 但也永远不会被调用。我不确定这一点是正确的方法,因为我认为这是为了 JCS 的更高级用途......

有谁知道如何(或者如果可能)注册某种监听器/观察者/其他什么那会完成这个吗?我的最终目标是能够在基本上从缓存中删除某些内容时收到通知......我并不特别关心如何提供它不是一个巨大的混乱:P

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...

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 ...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

删除→记忆 2024-10-14 19:59:53

在对 JCS 源代码进行简短审查后,我发现这些接口似乎只与远程缓存内容相关 - 我从未使用过这些内容。此外,我检查了 LRUMemoryCache 和其他一些,看起来对删除的调用没有链接到任何事件处理程序。长话短说,我在 JCS 中找不到任何可以满足您要求的内容。

我不会说这是不可能的,但我会说这看起来不太可能。

您可以在此处查看源代码并进一步查看。

祝你好运。

From what I can tell after a short review of the JCS source, it looks like those interfaces are only tied to the remote cache stuff - which I've never used. Additionally, I examined LRUMemoryCache and a few others and it looks like the calls to remove don't link in to any event handlers. Long story short, I couldn't find anything in JCS that does what you are asking.

I won't say it is impossible, but I would say it looks unlikely.

You can check out the source here and look further.

Good luck.

溺ぐ爱和你が 2024-10-14 19:59:53

创建一个抽象类来注册您想要捕获的事件。这对我捕捉这两个事件很有用。

  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 工厂定义中,如下所示

     JCS jcs = JCSCacheFactory.getCacheInstance(regionName);
     IElementAttributes attributes = jcs.getDefaultElementAttributes();
     attributes.addElementEventHandler(handler);
     jcs.setDefaultElementAttributes(attributes);

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文