在 Ehcache 3 的导入中找不到 ListenerObject?

ListenerObject not found in imports for Ehcache 3?

我正在尝试使用下面的代码为 Ehcache 3.3.1 项目实现一个监听器。谁能建议 ListenerObject 的解决方案?我似乎无法在任何地方找到它,除了 docs page 我从

获得代码
import java.util.logging.Level;
import java.util.logging.Logger;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheEventListenerConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.event.EventType;

public class CacheHandler{
     private Logger LOG = Logger.getLogger(this.getClass().getName());
     private String cacheName="basicCache";
     public Cache cache;

     public CacheHandler(){
         if(cache==null)
             cache=initCache();
     }
     private Cache initCache(){
       CacheEventListenerConfigurationBuilder cacheEventListenerConfiguration = CacheEventListenerConfigurationBuilder
           .newEventListenerConfiguration(new ListenerObject(), EventType.CREATED, EventType.UPDATED) 
           .unordered().asynchronous(); 

       final CacheManager manager = CacheManagerBuilder.newCacheManagerBuilder()
           .withCache(cacheName,
               CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, ResourcePoolsBuilder.heap(10))
                   .add(cacheEventListenerConfiguration) 
           ).build(true);

       final Cache<String, String> cache = manager.getCache("foo", String.class, String.class);
       return cache;
     }

     public Cache getCache(){
        if(cache==null)
            cache=initCache();

        return cache;           
     }
}

确实没有提到,但由于它只是一种方法,所以通常很容易弄清楚。

这是一个例子:

public class ListenerObject implements CacheEventListener<Object, Object> {
  @Override
  public void onEvent(CacheEvent<? extends Object, ? extends Object> event) {
    System.out.println(event);
  }
}

文档中真正使用的是here

然后,我对您的代码进行了一些实际生产可用的代码。

public class CacheHandler implements AutoCloseable {
  private static final String CACHE_NAME = "basicCache";
  private final Cache<String, String> cache;
  private final CacheManager cacheManager;

  public CacheHandler() {
    cacheManager = initCacheManager();
    cache = cacheManager.getCache(CACHE_NAME, String.class, String.class);
  }

  private CacheManager initCacheManager(){
    CacheEventListenerConfigurationBuilder cacheEventListenerConfiguration = CacheEventListenerConfigurationBuilder
      .newEventListenerConfiguration(new ListenerObject(), EventType.CREATED, EventType.UPDATED)
      .ordered().synchronous();

    return CacheManagerBuilder.newCacheManagerBuilder()
      .withCache(CACHE_NAME,
        CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, ResourcePoolsBuilder.heap(10))
          .add(cacheEventListenerConfiguration)
      ).build(true);
  }

  public Cache getCache(){
    return cache;
  }

  @Override
  public void close() {
    cacheManager.close();
  }

  public static void main(String[] args) {
    try(CacheHandler handler = new CacheHandler()) {
      Cache<String, String> cache = handler.getCache();
      cache.put("a", "b");
      cache.putIfAbsent("a", "c");
    }

  }
}

一些评论:

  • 我假设您希望单例缓存保存在一个变量中。所以这就是我所做的。惰性 initCache 没有用,因为 withCache 告诉 Ehcache 在创建缓存管理器时创建缓存。
  • 我们希望保留对 CacheManager 的引用,以便在最后关闭它。
  • getCache 正在检索 "foo",而不是 "basicCache"