【发布时间】: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>
【问题讨论】: