简体   繁体   English

使用哪个Ehcache导入?

[英]Which Ehcache imports to use?

I am just starting out with Ehcache, and trying to cache the result of a method call in a JAX-RS framework. 我只是从Ehcache开始,并试图将方法调用的结果缓存在JAX-RS框架中。 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! 我还要感谢在Ehcache中指向Java方法缓存的任何链接。...我发现的所有内容似乎都在试图做非常复杂的事情!

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.xml(在resources文件夹中)

<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) 您似乎在使用ehcache2(net.sf.ehcache)配置文件,而在代码中使用的是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 ) 使用ehcache3兼容的xml文件再试一次(您可以在ehcache3官方网站上找到灵感,也可以从窥视器示例中找到,或者甚至是我前几天设置的这个小项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM