【问题标题】:Which Ehcache imports to use?使用哪个 Ehcache 导入?
【发布时间】:2018-01-25 04:59:45
【问题描述】:

我刚开始使用 Ehcache,并尝试在 JAX-RS 框架中缓存方法调用的结果。有人能告诉我我的班级进口应该是什么吗?出于某种原因,我似乎无法在我读过的(非常令人困惑的)示例中找到这些行。我也很感激任何指向 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(在资源文件夹中)

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

【问题讨论】:

    标签: java caching ehcache


    【解决方案1】:

    您似乎在使用 ehcache2 (net.sf.ehcache) 配置文件,而在您的代码中您正在使用 ehcache3 (org.ehcache)

    使用 ehcache3 兼容的 xml 文件重试(您可以在 ehcache3 official websitethe peeper example 甚至 this little project I setup the other day 上找到灵感)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多