ehcache可以單獨使用也可以和hibernate、spring等一起使用。
本人今天單獨使用,並做些測試,分享經驗如下,
需要2個包,commons-logging.jar、ehcache-core-2.5.1.jar,最好使用ehcache的高版本,不然會配置文件的某些屬性不可用,如maxElementsInMemory等。這個版本需要commons-logging.jar包。
先配置xml文件,必須要有defaultCache,代碼類似如下,
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.sf.net/ehcache.xsd"> <diskStore path="E:/mytemp" /> <defaultCache maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="10" timeToLiveSeconds="20" diskPersistent="false" /> <cache name="requestCache" maxElementsInMemory="100000" eternal="false" overflowToDisk="false" timeToIdleSeconds="180" timeToLiveSeconds="180" diskPersistent="false" memoryStoreEvictionPolicy="LFU" /> <cache name="myCache" maxElementsInMemory="2" eternal="false" overflowToDisk="true" timeToIdleSeconds="180" timeToLiveSeconds="180" maxElementsOnDisk="0" diskPersistent="true" memoryStoreEvictionPolicy="LFU" /> </ehcache>
diskStore中的 是硬盤存放路勁,有個參數是user.io.dir表示eclipse默認路徑,在document and setting ....的某個子目錄下。
最好寫自己的路徑。
幾個重要屬性,請參考這裏。
需要注意的是
當overflowToDisk爲true時,在diskStore路勁下生成2個文件,一個是myCache.data和myCache.index,
若diskPersistent 這個屬性如果設置爲false,main方法(或web應用關閉)結束後上面2個文件就沒了。
若diskPersistent 爲true,則main方法結束後2文件存在
當再次啓動時,程序會加載myCache.index這個文件。
測試方法如下,
public class Test { public static void main(String[] args) { // 指定ehcache.xml的位置 String fileName = "F:/myeclipse/workspace/ecache/src/ehcache.xml"; CacheManager manager = new CacheManager(fileName); // 取出所有的cacheName String names[] = manager.getCacheNames(); for (int i = 0; i < names.length; i++) { System.out.println(names[i]); } Cache cache = manager.getCache("myCache"); System.out.println(cache.getSize()); for(int i=0;i<100000;i++){ cache.put(new Element("key1"+ i , "values1"+i)); } // cache.flush(); manager.shutdown(); } }
需要manager.shutdown(),不然要手動關閉
===================================================================
Ehcache 中ehcache.xml 配置詳解和示例
- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
- <diskStore path="java.io.tmpdir"/>
- <!--
- Mandatory Default Cache configuration. These settings will be applied to caches
- created programmtically using CacheManager.add(String cacheName)
- -->
- <!--
- name:緩存名稱。
- maxElementsInMemory:緩存最大個數。
- eternal:對象是否永久有效,一但設置了,timeout將不起作用。
- timeToIdleSeconds:設置對象在失效前的允許閒置時間(單位:秒)。僅當eternal=false對象不是永久有效時使用,可選屬性,默認值是0,也就是可閒置時間無窮大。
- timeToLiveSeconds:設置對象在失效前允許存活時間(單位:秒)。最大時間介於創建時間和失效時間之間。僅當eternal=false對象不是永久有效時使用,默認是0.,也就是對象存活時間無窮大。
- overflowToDisk:當內存中對象數量達到maxElementsInMemory時,Ehcache將會對象寫到磁盤中。
- diskSpoolBufferSizeMB:這個參數設置DiskStore(磁盤緩存)的緩存區大小。默認是30MB。每個Cache都應該有自己的一個緩衝區。
- maxElementsOnDisk:硬盤最大緩存個數。
- diskPersistent:是否緩存虛擬機重啓期數據 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
- diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認是120秒。
- memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理內存。默認策略是LRU(最近最少使用)。你可以設置爲FIFO(先進先出)或是LFU(較少使用)。
- clearOnFlush:內存數量最大時是否清除。
- -->
- <defaultCache
- maxElementsInMemory="10000"
- eternal="false"
- timeToIdleSeconds="120"
- timeToLiveSeconds="120"
- overflowToDisk="true"
- maxElementsOnDisk="10000000"
- diskPersistent="false"
- diskExpiryThreadIntervalSeconds="120"
- memoryStoreEvictionPolicy="LRU"
- />
- </ehcache>