2011-09-07 63 views
0

我想做一些簡單的事情,比如將內容存儲在緩存中,並在下次檢索時檢索它。出於某種原因,第一次一切正常,第二次調用時,高速緩存文件中的所有內容都將被刪除,並且會再次創建緩存。這是我的Ehcache配置文件Ehcache沒有在文件中存儲任何東西

<ehcache> 
    <diskStore path="<TEMP_DIR_PATH>" /> 
    <defaultCache maxElementsInMemory="10000" eternal="true" 
     timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" 
     maxElementsOnDisk="10000000" diskPersistent="true" 
     diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> 
    <cache name="mycache" maxElementsInMemory="1" eternal="true" 
     overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" 
     diskPersistent="true" diskExpiryThreadIntervalSeconds="1" 
     memoryStoreEvictionPolicy="LFU"/> 
</ehcache> 

的代碼實際上創建了2個文件一隻叫mycache.index,另一個叫mycache.data。將值放入緩存的代碼如下所示。

Cache cache = cacheManager.getCache("mycache"); 
Element myElement= new Element("KEY1","This will be stored in cache"); 
cache.put(myElement); 

難道有人請指出事情會出錯嗎?

我想每次都使用相同的存儲緩存文件,並且只有當數據文件不存在時才創建一個新文件。

回答

0

您需要撥打cache.flush()cache.dispose()將掛起的數據轉儲到磁盤。

+0

完美的答案,謝謝。問題已經解決 –

+0

@sivaraman:在思考這個EhCache設計時,我得出結論,每次將某些東西放入緩存時刷新到磁盤是有效的(緩存應該非常快,對嗎?),所以我們可以原諒EhCache這個小API不清晰。 –

0
CacheManager.getInstance().addCache("test"); 
     CacheManager.getInstance().getCache("test").put(new Element("name", "abhi")); 
     CacheManager.getInstance().getCache("test").put(new Element("class", "ten")); 
     CacheManager.getInstance().getCache("test").put(new Element("age", "24")); 

     Element elt = CacheManager.getInstance().getCache("test").get("class"); 
     //return (elt == null ? null : elt.getObjectValue()); 
     System.out.println(elt); 

希望這會幫助,並將存儲工作正常。