0
我剛開始使用Ehcache,並嘗試在JAX-RS框架中緩存方法調用的結果。有人能告訴我我的班級進口應該是什麼嗎?出於某種原因,我似乎無法在我讀過的(非常混淆的)例子中找到這些行。我也很感激Ehcache中任何Java方法緩存的鏈接....我發現的一切似乎都試圖做非常複雜的事情!哪些Ehcache導入使用?
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>