Ehcache簡介_動力節(jié)點Java學院整理
使用Spring的AOP進行整合,可以靈活的對方法的返回結(jié)果對象進行緩存。
CachingFilter功能可以對HTTP響應(yīng)的內(nèi)容進行緩存。
1、主要特性
1. 快速.
2. 簡單.
3. 多種緩存策略
4. 緩存數(shù)據(jù)有兩級:內(nèi)存和磁盤,因此無需擔心容量問題
5. 緩存數(shù)據(jù)會在虛擬機重啟的過程中寫入磁盤
6. 可以通過RMI、可插入API等方式進行分布式緩存
7. 具有緩存和緩存管理器的偵聽接口
8. 支持多緩存管理器實例,以及一個實例的多個緩存區(qū)域
9. 提供Hibernate的緩存實現(xiàn)
10. 等等
2、配置文件介紹(普通緩存)
<ehcache> <!-- 指定一個文件目錄,當EHCache把數(shù)據(jù)寫到硬盤上時,將把數(shù)據(jù)寫到這個文件目錄下 --> <diskStore path="java.io.tmpdir"/> <!-- 設(shè)定緩存的默認數(shù)據(jù)過期策略 --> <defaultCache maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="0" diskPersistent="false" diskExpiryThreadIntervalSeconds="120"/> <!-- 設(shè)定具體的命名緩存的數(shù)據(jù)過期策略 cache元素的屬性: name:緩存名稱 maxElementsInMemory:內(nèi)存中最大緩存對象數(shù) maxElementsOnDisk:硬盤中最大緩存對象數(shù),若是0表示無窮大 eternal:true表示對象永不過期,此時會忽略timeToIdleSeconds和timeToLiveSeconds屬性,默認為false overflowToDisk:true表示當內(nèi)存緩存的對象數(shù)目達到了maxElementsInMemory界限后,會把溢出的對象寫到硬盤緩存中。注意:如果緩存的對象要寫入到硬盤中的話,則該對象必須實現(xiàn)了Serializable接口才行。 diskSpoolBufferSizeMB:磁盤緩存區(qū)大小,默認為30MB。每個Cache都應(yīng)該有自己的一個緩存區(qū)。 diskPersistent:是否緩存虛擬機重啟期數(shù)據(jù) diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認為120秒 timeToIdleSeconds: 設(shè)定允許對象處于空閑狀態(tài)的最長時間,以秒為單位。當對象自從最近一次被訪問后,如果處于空閑狀態(tài)的時間超過了timeToIdleSeconds屬性值,這個對象就會過期,EHCache將把它從緩存中清空。只有當eternal屬性為false,該屬性才有效。如果該屬性值為0,則表示對象可以無限期地處于空閑狀態(tài) timeToLiveSeconds:設(shè)定對象允許存在于緩存中的最長時間,以秒為單位。當對象自從被存放到緩存中后,如果處于緩存中的時間超過了 timeToLiveSeconds屬性值,這個對象就會過期,EHCache將把它從緩存中清除。只有當eternal屬性為false,該屬性才有效。如果該屬性值為0,則表示對象可以無限期地存在于緩存中。timeToLiveSeconds必須大于timeToIdleSeconds屬性,才有意義 memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據(jù)指定的策略去清理內(nèi)存。可選策略有:LRU(最近最少使用,默認策略)、FIFO(先進先出)、LFU(最少訪問次數(shù))。 --> <cache name="CACHE1" maxElementsInMemory="1000" eternal="true" overflowToDisk="true"/> <cache name="CACHE2" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="200" timeToLiveSeconds="4000" overflowToDisk="true"/> </ehcache>
3、配置文件介紹(分布式緩存)
1)RMI集群模式
A、手工發(fā)現(xiàn)
需要指定節(jié)點發(fā)現(xiàn)模式peerDiscovery值為manual,rmiUrls設(shè)置為另一臺服務(wù)器的IP、端口和緩存名等信息。
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=manual, rmiUrls=//192.168.0.12:4567/bjpowernode_cache|//192.168.0.13:4567/bjpowernode_cache" />
B、自動發(fā)現(xiàn)
需要指定節(jié)點發(fā)現(xiàn)模式peerDiscovery值為automatic自動,同時組播地址可以指定D類IP地址空間,范圍從 224.0.1.0 到 238.255.255.255 中的任何一個地址。
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446, timeToLive=32" />
需要在每個cache屬性中加入
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/> <cache name="demoCache" maxElementsInMemory="10000" eternal="true" overflowToDisk="true"> <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/> </cache>
4、通過編程方式使用EhCache
//從classes目錄查找ehcache.xml配置文件 CacheManager cacheManager = CacheManager.getInstance(); //從classes目錄查找指定名稱的配置文件 //CacheManager cacheManager = CacheManager.create(getClass().getResource("/ehcache.xml")); //根據(jù)配置文件獲得Cache實例 Cache cache = cacheManager.getCache("CACHE1"); //清空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();
5、頁面緩存
在web.xml文件中配置過濾器。此處對test_tag.jsp頁面進行緩存。
<filter> <filter-name>testPageCachingFilter</filter-name> <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter</filter-class> </filter> <filter-mapping> <filter-name>testPageCachingFilter</filter-name> <url-pattern>/test_tag.jsp</url-pattern> </filter-mapping>
在ehcache.xml文件中配置Cache節(jié)點。注意:cache的name屬性必需為SimplePageCachingFilter。
<cache name="SimplePageCachingFilter" maxElementsInMemory="10" overflowToDisk="true" eternal="false" timeToIdleSeconds="100" timeToLiveSeconds="100" memoryStoreEvictionPolicy="LFU" />
相關(guān)文章
java 設(shè)計模式之State(狀態(tài)模式)
這篇文章主要介紹了java 設(shè)計模式之State(狀態(tài)模式)的相關(guān)資料,一個類的行為基于它的狀態(tài)的改變而改變。狀態(tài)模式歸屬于行為型模式,需要的朋友可以參考下2017-08-08Java之SpringBoot集成ActiveMQ消息中間件案例講解
這篇文章主要介紹了Java之SpringBoot集成ActiveMQ消息中間件案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07詳解Java數(shù)組擴容縮容與拷貝的實現(xiàn)和原理
這篇文章主要帶大家學習數(shù)組的擴容、縮容及拷貝,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-05-05