ListenerObject not found in imports for Ehcache 3? -
i trying implement listener ehcache 3.3.1 project using code below. can suggest solution listenerobject? can't seem find anywhere,except on docs page got code from
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; } }
it indeed not mentioned since 1 method easy figure out.
here example:
public class listenerobject implements cacheeventlistener<object, object> { @override public void onevent(cacheevent<? extends object, ? extends object> event) { system.out.println(event); } }
the real 1 used in documentation here.
then, i've played bit code real production usable code.
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"); } } }
some comments:
- i assumed want singleton cache kept in variable. that's did. lazy
initcache
wasn't useful becausewithcache
tells ehcache create cache when creating cache manager. - we want keep reference
cachemanager
in order close @ end. - the
getcache
retrieving "foo", not "basiccache"
wiki
Comments
Post a Comment