Ehcache 緩存配置
?Cache的配置很靈活,官方提供的Cache配置方式有好幾種。你可以通過聲明配置、在xml中配置、在程序里配置或者調用構造方法時傳入不同的參數。?
你可以將Cache的配置從代碼中剝離出來,也可以在使用運行時配置,所謂的運行時配置無非也就是在代碼中配置。以下是運行時配置的好處:?
- 在同一個地方配置所有的Cache,這樣很容易管理Cache的內存和磁盤消耗。
- 發布時可更改Cache配置。
- 可再安裝階段就檢查出配置錯誤信息,而避免了運行時錯誤。?
本文將會對ehcache.xml配置文件進行詳細的闡述。在配置的時可以拷貝一個現有的ehcache.xml,?
如果你調用了CacheManager默認構造方法去創建CacheManager的實例,此方法會到classpath中找ehcache.xml文件,否則它會到類路徑下找ehcache-failsafe.xml文件。而ehcache-failsafe.xml被包含在jar包中,所有它肯定能找的到。?
ehcache-failsafe.xml提供了一個非常簡單的默認配置,這樣可以使用戶在沒有創建ehcache.xml的情況下使用Ehcache。不過這樣做Ehcache會提醒用戶創建一個正確的Ehcache配置。?
ehcache.xml片段:
< ehcache > < diskStore path ="java.io.tmpdir" /> < defaultCache maxElementsInMemory ="10000" eternal ="false" timeToIdleSeconds ="120" timeToLiveSeconds ="120" overflowToDisk ="true" maxElementsOnDisk ="10000000" diskPersistent ="false" diskExpiryThreadIntervalSeconds ="120" memoryStoreEvictionPolicy ="LRU" /> </ ehcache >
?在Ehcache-1.6之前的版本,只支持ASCII編碼的ehcache.xml配置文件。在Ehcach-1.6之后版本中,支持UTF8編碼的ehcache.xml配置文件。因為向后兼容,所有采用ASCII編碼的配置文件完全沒有必要轉換為UTF8。?
一個CacheManager必須要有一個XML配置。由于磁盤路徑或是監聽端口,多個CacheManager使用同一個配置文件時會出現錯誤。?
下面是ehcache.xml具體實例以及配置指南?
<ehcache xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance?
- CacheManager配置
DmulticastGroupPort=4446,這樣可以配置監聽端口。?
- DiskStore配置
如果你使用的DiskStore(磁盤緩存),你必須要配置DiskStore配置項。如果不配置,Ehcache將會使用java.io.tmpdir。?
diskStroe的“path”屬性是用來配置磁盤緩存使用的物理路徑的,Ehcache磁盤緩存使用的文件后綴名是.data和.index。?
<disStore path=”java.io.tmpdir”/>?
- CacheManagerEventListener配置
我們通過CacheManagerEventListenerFactory可以實例化一個CacheManagerPeerProvider,當我們從CacheManager中added和removed Cache時,將通知CacheManagerPeerProvider,這樣一來,我們就可以很方面的對CacheManager中的Cache做一些統計。?
注冊到CacheManager的事件監聽類名有:?adding a Cache和removing a Cache?
<cacheManagerEventListenerFacotory class=”” properties=””/>?
- CacheManagerPeerProvider配置
在集群中CacheManager配置CacheManagerPeerProviderFactory創建CacheManagerPeerProvider。具體的實例如下:
<cacheManagerPeerProviderFactoryclass="net.sf.ehcache.distribution.
RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual, rmiUrls=//server1:40000/sampleCache1|//server2:40000/sampleCache1| //server1:40000/sampleCache2|//server2:40000/sampleCache2"
propertySeparator="," />
- CacheManagerPeerListener配置
CacheManagerPeerListener配置是用來監聽集群中緩存消息的分發的。?
<cacheManagerPeerListenerFactory
????class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
????properties="hostName=fully_qualified_hostname_or_ip,
????????????????port=40001,
????????????????socketTimeoutMillis=120000"
????????????????propertySeparator="," />
- Cache配置
- name:Cache的唯一標識
- maxElementsInMemory:內存中最大緩存對象數。
- maxElementsOnDisk:磁盤中最大緩存對象數,若是0表示無窮大。
- eternal:Element是否永久有效,一但設置了,timeout將不起作用。
- overflowToDisk:配置此屬性,當內存中Element數量達到maxElementsInMemory時,Ehcache將會Element寫到磁盤中。
- timeToIdleSeconds:設置一個緩存項在過期前的閑置時間。即一個緩存項在其過期前,兩次訪問間隔的最大時間。僅在緩存項為非永久時有效。0表示不限閑置時間,默認為 0。
- timeToLiveSeconds:設置Element在失效前允許存活時間。最大時間介于創建時間和失效時間之間。僅當element不是永久有效時使用,默認是0。也就是element存活時間無窮大。
- diskPersistent:在VM重啟的時候是否持久化磁盤緩存。
- diskExpiryThreadIntervalSeconds:清理保存在磁盤上的過期緩存項目的線程的啟動時間間隔,默認 120秒。
- diskSpoolBufferSizeMB:磁盤緩沖區的大小。寫入磁盤的內容將緩沖在此區域,使用異步的方式寫入磁盤。默認 30MB,每一個緩存區使用獨立的緩沖區,如果遇到 OutOfMemory 錯誤時,可以嘗試減小這個值。改進磁盤寫入性能時,嘗試增加這個值。將日志級別開到 Trace,當 DiskStore 執行磁盤寫入時,可以看到對應日志
- statistics:是否收集統計信息。如果需要監控緩存使用情況,應該打開這個選項。默認為關閉(統計會影響性能)。設置 statistics="true"開啟統計。
-
memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理內存。
-
LRU -least recently used
LFU -least frequently used
FIFO-first in first out
-
-
通過使用<persistence/>子元素,可以配置緩存區的持久化策略。<persistence/>元素的主要屬性如下:
strategy:配置緩存區持久化的類型。可選值如下:
localRestartable:僅在使用 Ehcache企業版時有效。啟用 RestartStore,拷貝所有的緩存項(包含堆和非堆中的)到磁盤中,此選項提供了緩存快速重啟能力以及對磁盤上緩存的容錯能力。
localTempSwap:當緩存容量達到上限時,將緩存對象 (包含堆和非堆中的)交換到磁盤中。"localTempSwap" 并不持久化緩存內容。
none:不持久化緩存內容。
distributed:按照<terracotta>標簽配置的持久化方式執行。非分布式部署時,此選項不可用。
synchronousWrites:此屬性僅在strategy="localRestartable"時有意義。默認false。設置為 true,緩存寫入方法在緩存項成功寫入磁盤前不會返回。 - Cache Exception Handling配置?
<cacheExceptionHandlerFactory
class="com.example.ExampleExceptionHandlerFactory"????
properties="logLevel=FINE"/> ?
?這里只對通用緩存的配置做了詳細的闡述,至于RMI緩存和集群緩存可以參考 這里 。
?下面給出幾個配置示例:
?·???Ehcache默認Cache配置?
< defaultCache maxElementsInMemory ="10000" eternal ="false" timeToIdleSeconds ="120" timeToLiveSeconds ="120" overflowToDisk ="true" diskSpoolBufferSizeMB ="30" maxElementsOnDisk ="10000000" diskPersistent ="false" diskExpiryThreadIntervalSeconds ="120" memoryStoreEvictionPolicy ="LRU" />
- SampleCache1配置?
簡單配置,在ehcache.xml文件中有此配置,在使用Ehcache前最好將其刪除掉,自己配置。?
緩存名sampleCache1,內存中最多可緩存10000個Element,其中的element會在閑置5分鐘或是存活10分鐘之后失效。?
超過10000element時,element將會輸出到磁盤中,輸出路徑是java.io.tmpdir。?
< cache name ="sampleCache1" maxElementsInMemory ="10000" maxElementsOnDisk ="1000" eternal ="false" overflowToDisk ="true" diskSpoolBufferSizeMB ="20" timeToIdleSeconds ="300" timeToLiveSeconds ="600" memoryStoreEvictionPolicy ="LFU" />
- SampleCache2配置?
Cache名為SampleCache2,內存中最多可以緩存1000個element,超出1000不能輸出到磁盤中。緩存是永久有效的。?
< cache name ="sampleCache2" maxElementsInMemory ="1000" eternal ="true" overflowToDisk ="false" memoryStoreEvictionPolicy ="FIFO" />
- SampleCache3配置?
Cache名為SampleCache3。可緩存到磁盤。磁盤緩存將會緩存虛擬機重啟期的數據。磁盤緩存失效線程運行間隔時間是10分鐘。?
< cache name ="sampleCache3" maxElementsInMemory ="500" eternal ="false" overflowToDisk ="true" timeToIdleSeconds ="300" timeToLiveSeconds ="600" diskPersistent ="true" diskExpiryThreadIntervalSeconds ="1" memoryStoreEvictionPolicy ="LFU" />
- sampleDistributedCache1配置?
Cache名為sampleDistributedCache1。?
< cache name ="sampleDistributedCache1" maxElementsInMemory ="10" eternal ="false" timeToIdleSeconds ="100" timeToLiveSeconds ="100" overflowToDisk ="false" > < cacheEventListenerFactory class ="net.sf.ehcache.distribution.RMICacheReplicatorFactory" /> < bootstrapCacheLoaderFactory class ="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" /> </ cache >
- sampleDistributedCache2配置?
< cache name ="sampleDistributedCache2" maxElementsInMemory ="10" eternal ="false" timeToIdleSeconds ="100" timeToLiveSeconds ="100" overflowToDisk ="false" > < cacheEventListenerFactory class ="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties ="replicateAsynchronously=false, replicatePuts=false, replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=false" /> </ cache >
- sampleDistributedCache3配置?
<!-- Sample distributed cache named sampleDistributedCache3. This cache replicates using defaults except that the asynchronous replication interval is set to 200ms. --> < cache name ="sampleDistributedCache3" maxElementsInMemory ="10" eternal ="false" timeToIdleSeconds ="100" timeToLiveSeconds ="100" overflowToDisk ="false" > < cacheEventListenerFactory class ="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties ="asynchronousReplicationIntervalMillis=200" /> </ cache >
- 快速重啟cache配置?
-
< cache name ="example4Cache" maxElementsInMemory ="100" eternal ="false" timeToIdleSeconds ="0" timeToLiveSeconds ="259200" overflowToOffHeap ="true" maxMemoryOffHeap ="1024M" memoryStoreEvictionPolicy ="LRU" statistics ="true" > < persistence strategy ="localRestartable" synchronousWrites ="false" /> </ cache >
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
