I am just starting out with Ehcache, and trying to cache the result of a method call in a JAX-RS framework. Can someone tell me what my class imports should be? For some reason I cant seem to find those lines in the (very confusing) examples I have read. I would also appreciate any links to Java method caching in Ehcache.... everything I've found seems to be trying to do very complicated things!
import org.ehcache.Cache;
import org.ehcache.CacheManager;
/**
*
* @author king
*/
public class CacheTest {
CacheManager cacheMgr = CacheManager.newInstance();
//EJB?Stateless?
HelloService hello;
public Object getCache(){
//Initialise a cache if it does not already exist
if (cacheMgr.getCache("MyCache") == null) {
cacheMgr.addCache("MyCache");
}
Cache cache = cacheMgr.getCache("MyCache");
String s=hello.getUserInfo(103);
//Store an element
cache.put(new Element("103", s));
//Retrieve an element
Element el = cache.get("key");
Serializable myObj = <Serializable>el.getObjectValue();
return myObj;
}
}
ehcache.xml (in resources folder)
<ehcache>
<diskStore path="java.io.tmpdir"/>
<cache name="MyCache"
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
>
<persistence strategy="localTempSwap"/>
</cache>
</ehcache>
You seem to be using an ehcache2 (net.sf.ehcache) configuration file while in your code you're using ehcache3 (org.ehcache)
Try again with a ehcache3 compatible xml file (you can find inspiration on the ehcache3 official website or also the peeper example or even this little project I setup the other day )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.