[英]Which Cache Manager/Cache library to use for Spring and EHCache 3?
[英]Which Ehcache imports to use?
我只是從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(在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>
您似乎在使用ehcache2(net.sf.ehcache)配置文件,而在代碼中使用的是ehcache3(org.ehcache)
使用ehcache3兼容的xml文件再試一次(您可以在ehcache3官方網站上找到靈感,也可以從窺視器示例中找到,或者甚至是我前幾天設置的這個小項目 )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.