Ehcache 緩存配置
?Cache的配置很靈活,官方提供的Cache配置方式有好幾種。你可以通過聲明配置、在xml中配置、在程序里配置或者調(diào)用構(gòu)造方法時(shí)傳入不同的參數(shù)。?
你可以將Cache的配置從代碼中剝離出來(lái),也可以在使用運(yùn)行時(shí)配置,所謂的運(yùn)行時(shí)配置無(wú)非也就是在代碼中配置。以下是運(yùn)行時(shí)配置的好處:?
- 在同一個(gè)地方配置所有的Cache,這樣很容易管理Cache的內(nèi)存和磁盤消耗。
- 發(fā)布時(shí)可更改Cache配置。
- 可再安裝階段就檢查出配置錯(cuò)誤信息,而避免了運(yùn)行時(shí)錯(cuò)誤。?
本文將會(huì)對(duì)ehcache.xml配置文件進(jìn)行詳細(xì)的闡述。在配置的時(shí)可以拷貝一個(gè)現(xiàn)有的ehcache.xml,?
如果你調(diào)用了CacheManager默認(rèn)構(gòu)造方法去創(chuàng)建CacheManager的實(shí)例,此方法會(huì)到classpath中找ehcache.xml文件,否則它會(huì)到類路徑下找ehcache-failsafe.xml文件。而ehcache-failsafe.xml被包含在jar包中,所有它肯定能找的到。?
ehcache-failsafe.xml提供了一個(gè)非常簡(jiǎn)單的默認(rèn)配置,這樣可以使用戶在沒有創(chuàng)建ehcache.xml的情況下使用Ehcache。不過這樣做Ehcache會(huì)提醒用戶創(chuàng)建一個(gè)正確的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配置文件。因?yàn)橄蚝蠹嫒荩胁捎肁SCII編碼的配置文件完全沒有必要轉(zhuǎn)換為UTF8。?
一個(gè)CacheManager必須要有一個(gè)XML配置。由于磁盤路徑或是監(jiān)聽端口,多個(gè)CacheManager使用同一個(gè)配置文件時(shí)會(huì)出現(xiàn)錯(cuò)誤。?
下面是ehcache.xml具體實(shí)例以及配置指南?
<ehcache xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance?
- CacheManager配置
DmulticastGroupPort=4446,這樣可以配置監(jiān)聽端口。?
- DiskStore配置
如果你使用的DiskStore(磁盤緩存),你必須要配置DiskStore配置項(xiàng)。如果不配置,Ehcache將會(huì)使用java.io.tmpdir。?
diskStroe的“path”屬性是用來(lái)配置磁盤緩存使用的物理路徑的,Ehcache磁盤緩存使用的文件后綴名是.data和.index。?
<disStore path=”java.io.tmpdir”/>?
- CacheManagerEventListener配置
我們通過CacheManagerEventListenerFactory可以實(shí)例化一個(gè)CacheManagerPeerProvider,當(dāng)我們從CacheManager中added和removed Cache時(shí),將通知CacheManagerPeerProvider,這樣一來(lái),我們就可以很方面的對(duì)CacheManager中的Cache做一些統(tǒng)計(jì)。?
注冊(cè)到CacheManager的事件監(jiān)聽類名有:?adding a Cache和removing a Cache?
<cacheManagerEventListenerFacotory class=”” properties=””/>?
- CacheManagerPeerProvider配置
在集群中CacheManager配置CacheManagerPeerProviderFactory創(chuàng)建CacheManagerPeerProvider。具體的實(shí)例如下:
<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配置是用來(lái)監(jiān)聽集群中緩存消息的分發(fā)的。?
<cacheManagerPeerListenerFactory
????class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
????properties="hostName=fully_qualified_hostname_or_ip,
????????????????port=40001,
????????????????socketTimeoutMillis=120000"
????????????????propertySeparator="," />
- Cache配置
- name:Cache的唯一標(biāo)識(shí)
- maxElementsInMemory:內(nèi)存中最大緩存對(duì)象數(shù)。
- maxElementsOnDisk:磁盤中最大緩存對(duì)象數(shù),若是0表示無(wú)窮大。
- eternal:Element是否永久有效,一但設(shè)置了,timeout將不起作用。
- overflowToDisk:配置此屬性,當(dāng)內(nèi)存中Element數(shù)量達(dá)到maxElementsInMemory時(shí),Ehcache將會(huì)Element寫到磁盤中。
- timeToIdleSeconds:設(shè)置一個(gè)緩存項(xiàng)在過期前的閑置時(shí)間。即一個(gè)緩存項(xiàng)在其過期前,兩次訪問間隔的最大時(shí)間。僅在緩存項(xiàng)為非永久時(shí)有效。0表示不限閑置時(shí)間,默認(rèn)為 0。
- timeToLiveSeconds:設(shè)置Element在失效前允許存活時(shí)間。最大時(shí)間介于創(chuàng)建時(shí)間和失效時(shí)間之間。僅當(dāng)element不是永久有效時(shí)使用,默認(rèn)是0。也就是element存活時(shí)間無(wú)窮大。
- diskPersistent:在VM重啟的時(shí)候是否持久化磁盤緩存。
- diskExpiryThreadIntervalSeconds:清理保存在磁盤上的過期緩存項(xiàng)目的線程的啟動(dòng)時(shí)間間隔,默認(rèn) 120秒。
- diskSpoolBufferSizeMB:磁盤緩沖區(qū)的大小。寫入磁盤的內(nèi)容將緩沖在此區(qū)域,使用異步的方式寫入磁盤。默認(rèn) 30MB,每一個(gè)緩存區(qū)使用獨(dú)立的緩沖區(qū),如果遇到 OutOfMemory 錯(cuò)誤時(shí),可以嘗試減小這個(gè)值。改進(jìn)磁盤寫入性能時(shí),嘗試增加這個(gè)值。將日志級(jí)別開到 Trace,當(dāng) DiskStore 執(zhí)行磁盤寫入時(shí),可以看到對(duì)應(yīng)日志
- statistics:是否收集統(tǒng)計(jì)信息。如果需要監(jiān)控緩存使用情況,應(yīng)該打開這個(gè)選項(xiàng)。默認(rèn)為關(guān)閉(統(tǒng)計(jì)會(huì)影響性能)。設(shè)置 statistics="true"開啟統(tǒng)計(jì)。
-
memoryStoreEvictionPolicy:當(dāng)達(dá)到maxElementsInMemory限制時(shí),Ehcache將會(huì)根據(jù)指定的策略去清理內(nèi)存。
-
LRU -least recently used
LFU -least frequently used
FIFO-first in first out
-
-
通過使用<persistence/>子元素,可以配置緩存區(qū)的持久化策略。<persistence/>元素的主要屬性如下:
strategy:配置緩存區(qū)持久化的類型。可選值如下:
localRestartable:僅在使用 Ehcache企業(yè)版時(shí)有效。啟用 RestartStore,拷貝所有的緩存項(xiàng)(包含堆和非堆中的)到磁盤中,此選項(xiàng)提供了緩存快速重啟能力以及對(duì)磁盤上緩存的容錯(cuò)能力。
localTempSwap:當(dāng)緩存容量達(dá)到上限時(shí),將緩存對(duì)象 (包含堆和非堆中的)交換到磁盤中。"localTempSwap" 并不持久化緩存內(nèi)容。
none:不持久化緩存內(nèi)容。
distributed:按照<terracotta>標(biāo)簽配置的持久化方式執(zhí)行。非分布式部署時(shí),此選項(xiàng)不可用。
synchronousWrites:此屬性僅在strategy="localRestartable"時(shí)有意義。默認(rèn)false。設(shè)置為 true,緩存寫入方法在緩存項(xiàng)成功寫入磁盤前不會(huì)返回。 - Cache Exception Handling配置?
<cacheExceptionHandlerFactory
class="com.example.ExampleExceptionHandlerFactory"????
properties="logLevel=FINE"/> ?
?這里只對(duì)通用緩存的配置做了詳細(xì)的闡述,至于RMI緩存和集群緩存可以參考 這里 。
?下面給出幾個(gè)配置示例:
?·???Ehcache默認(rèn)Cache配置?
< defaultCache maxElementsInMemory ="10000" eternal ="false" timeToIdleSeconds ="120" timeToLiveSeconds ="120" overflowToDisk ="true" diskSpoolBufferSizeMB ="30" maxElementsOnDisk ="10000000" diskPersistent ="false" diskExpiryThreadIntervalSeconds ="120" memoryStoreEvictionPolicy ="LRU" />
- SampleCache1配置?
簡(jiǎn)單配置,在ehcache.xml文件中有此配置,在使用Ehcache前最好將其刪除掉,自己配置。?
緩存名sampleCache1,內(nèi)存中最多可緩存10000個(gè)Element,其中的element會(huì)在閑置5分鐘或是存活10分鐘之后失效。?
超過10000element時(shí),element將會(huì)輸出到磁盤中,輸出路徑是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,內(nèi)存中最多可以緩存1000個(gè)element,超出1000不能輸出到磁盤中。緩存是永久有效的。?
< cache name ="sampleCache2" maxElementsInMemory ="1000" eternal ="true" overflowToDisk ="false" memoryStoreEvictionPolicy ="FIFO" />
- SampleCache3配置?
Cache名為SampleCache3。可緩存到磁盤。磁盤緩存將會(huì)緩存虛擬機(jī)重啟期的數(shù)據(jù)。磁盤緩存失效線程運(yùn)行間隔時(shí)間是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 >
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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