簡介: EhCache是一個純java的進程內緩存框架,具有快速、精干等特點,是Hibernate中默認的CachePRoviderOSCache。
適用場所:
特點:
FIFO:first in first out,這個是大家最熟的,先進先出。
LFU:Less Frequently Used,一直以來最少被使用的將被清除,緩存的元素有一個hit屬性,hit值最小的將會被清出緩存。
LRU:Least Recently Used,最近最少使用的,緩存的元素有一個時間戳,當緩存容量滿了,而又需要騰出地方來緩存新的元素的時候,那么現有緩存元素中時間戳離當前時間最遠的元素將被清出緩存
2.緩存數據存儲位置有兩級:內存和磁盤,因此無需擔心容量問題
3.緩存數據會在虛擬機重啟的過程中寫入磁盤
深層原理: http://raychase.VEvb.com/blog/1545906,四火同學的講解釋的超詳細 相關配置:
1.通過自己編碼方式使用
xml文件配置:
<ehcache> <!-- 指定一個文件目錄,當EHCache把數據寫到硬盤上時,將把數據寫到這個文件目錄下 --> <diskStore path="java.io.tmpdir"/> <!-- 設定緩存的默認數據過期策略 --> <defaultCache maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="0" diskPersistent="false" diskExpiryThreadIntervalSeconds="120"/> <cache name="TESTCACHE" maxElementsInMemory="1000" eternal="true" overflowToDisk="true"/> </ehcache>
ehcache.xml元素的屬性: name:緩存名稱
maxElementsInMemory:內存中最大緩存對象數
maxElementsOnDisk:硬盤中最大緩存對象數,若是0表示無窮大
eternal:true表示對象永不過期,此時會忽略timeToIdleSeconds和timeToLiveSeconds屬性,默認為false
overflowToDisk:true表示當內存緩存的對象數目達到了maxElementsInMemory界限后,會把溢出的對象寫到硬盤緩存中。注意:如果緩存的對象要寫入到硬盤中的話,則該對象必須實現了Serializable接口才行。
diskSpoolBufferSizeMB:磁盤緩存區大小,默認為30MB。每個Cache都應該有自己的一個緩存區。
diskPersistent:是否緩存虛擬機重啟期數據
diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認為120
timeToIdleSeconds: 設定允許對象處于空閑狀態的最長時間,以秒為單位。當對象自從最近一次被訪問后,如果處于空閑狀態的時間超過了timeToIdleSeconds屬性值,這個對象就會過期,EHCache將把它從緩存中清空。只有當eternal屬性為false,該屬性才有效。如果該屬性值為0,則表示對象可以無限期地處于空閑狀態
timeToLiveSeconds:設定對象允許存在于緩存中的最長時間,以秒為單位。當對象自從被存放到緩存中后,如果處于緩存中的時間超過了 timeToLiveSeconds屬性值,這個對象就會過期,EHCache將把它從緩存中清除。只有當eternal屬性為false,該屬性才有效。如果該屬性值為0,則表示對象可以無限期地存在于緩存中。timeToLiveSeconds必須大于timeToIdleSeconds屬性,才有意義
memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理內存。可選策略有:LRU(最近最少使用,默認策略)、FIFO(先進先出)、LFU(最少訪問次數)。
java編程代碼:
//從classes目錄查找ehcache.xml配置文件 CacheManager cacheManager = CacheManager.getInstance(); //從classes目錄查找指定名稱的配置文件 //CacheManager cacheManager = CacheManager.create(getClass().getResource("/ehcache.xml")); //根據配置文件獲得Cache實例 Cache cache = cacheManager.getCache("TESTCACHE"); //清空Cache中的所有元素 cache.removeAll(); //往Cache中添加元素 cache.put(new Element("s1", "11111")); cache.put(new Element("s2", "22222")); cache.put(new Element("s3", "33333")); //從Cache中取得元素 Element e = cache.get("s3"); System.out.println(e.getValue()); //卸載緩存管理器 cacheManager.shutdown();
2.用于作為servlet緩存使用
在web.xml中的配置
<filter> <filter-name>SimplePageFragmentCachingFilter</filter-name> <filter-class> net.sf.ehcache.constructs.web.filter.SimplePageFragmentCachingFilter </filter-class> </filter> <filter-mapping> <filter-name>SimplePageFragmentCachingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
在ehcache.xml中的配置
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false" monitoring="on"> <diskStore path="java.io.tmpdir" /> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="3600" timeToLiveSeconds="3600" overflowToDisk="true" diskPersistent="false" diskExpiryThreadIntervalSeconds="3600" memoryStoreEvictionPolicy="LRU"/> <cache name="SimplePageFragmentCachingFilter" maxElementsInMemory="10000" maxElementsOnDisk="100000" overflowToDisk="true" diskSpoolBufferSizeMB="5120" eternal="false" timeToIdleSeconds="86400" timeToLiveSeconds="0" memoryStoreEvictionPolicy="LFU" /> </ehcache>
此時需要額外導入jar包:ehcache-web-2.0.4.jar
下載地址: Ehcache 2.5.1 下載地址:http://sourceforge.net/projects/ehcache/files/ehcache/ehcache-2.5.1/
新聞熱點
疑難解答