Cache的配置很靈活,官方提供的Cache配置方式有好幾種。你可以通過聲明配置、在xml中配置、在程序里配置或者調用構造方法時傳入不同的參數。
你可以將Cache的配置從代碼中剝離出來,也可以在使用運行時配置,所謂的運行時配置無非也就是在代碼中配置。以下是運行時配置的好處:
· 在同一個地方配置所有的Cache,這樣很容易管理Cache的內存和磁盤消耗。
· 發布時可更改Cache配置。
· 可再安裝階段就檢查出配置錯誤信息,而避免了運行時錯誤。
本文將會對ehcache.xml配置文件進行詳細的闡述。在配置的時可以拷貝一個現有的ehcache.xml.
ehcache.xml片段:
<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.xml和其他配置文件
在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:設置Element在失效前的允許閑置時間。僅當element不是永久有效時使用,可選屬性,默認值是0,也就是可閑置時間無窮大。
· timeToLiveSeconds:設置Element在失效前允許存活時間。最大時間介於創建時間和失效時間之間。僅當element不是永久有效時使用,默認是0.,也就是element存活時間無窮大。
· diskPersistent:是否緩存虛擬機重啟期數據。(這個虛擬機是指什么虛擬機一直沒看明白是什么,有高人還希望能指點一二)。
· diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認是120秒。
· diskSpoolBufferSizeMB:這個參數設置DiskStore(磁盤緩存)的緩存區大小。默認是30MB。每個Cache都應該有自己的一個緩沖區。
· memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理內存。默認策略是LRU(最近最少使用)。你可以設置為FIFO(先進先出)或是LFU(較少使用)。這里比較遺憾,Ehcache並沒有提供一個用戶定制策略的接口,僅僅支持三種指定策略,感覺做的不夠理想。
· Cache Exception Handling配置
<cacheExceptionHandlerFactory class="com.example.ExampleExceptionHandlerFactory" properties="logLevel=FINE"/>
總結
這里只對通用緩存的配置做了詳細的闡述,至於RMI緩存和集群緩存可以參考這里。
下面給出幾個配置示例:
· Ehcache默認Cache配置
· SampleCache1配置
簡單配置,在ehcache.xml文件中有此配置,在使用Ehcache前最好將其刪除掉,自己配置。
緩存名sampleCache1,內存中最多可緩存10000個Element,其中的element會在閑置5分鍾或是存活10分鍾之后失效。
超過10000element時,element將會輸出到磁盤中,輸出路徑是java.io.tmpdir。
· SampleCache2配置
Cache名為SampleCache2,內存中最多可以緩存1000個element,超出1000不能輸出到磁盤中。緩存是永久有效的。
maxElementsInMemory="1000"
eternal="true"
overflowToDisk="false"
memoryStoreEvictionPolicy="FIFO"
/>
· SampleCache3配置
Cache名為SampleCache3。可緩存到磁盤。磁盤緩存將會緩存虛擬機重啟期的數據。磁盤緩存失效線程運行間隔時間是10分鍾。
· sampleDistributedCache1配置
· sampleDistributedCache2配置
· 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>

maxElementsInMemory="500"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="1"
memoryStoreEvictionPolicy="LFU"
/>

<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>

maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskSpoolBufferSizeMB="30"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
代碼
maxElementsInMemory="10000"
maxElementsOnDisk="1000"
eternal="false"
overflowToDisk="true"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
/>