欠了很久的文章,本來(lái)都不想發(fā)了,可還是發(fā)吧
ehcache的默認(rèn)配置文件名為ehcache.xml,也可以自己指定。配置文件比較的簡(jiǎn)單,基本上也就是常用的幾個(gè)元素。
配置元素說(shuō)明:
diskStore:配置DiskStore,當(dāng)需要使用磁盤保存的時(shí)候,需要對(duì)象實(shí)現(xiàn)序列化接口
屬性:path 配置文件存儲(chǔ)位置,如user.home,user.dir,java.io.tmpdir
cacheManagerEventListenerFactory :指定CacheManagerEventListenerFactory,用于創(chuàng)建CacheManagerPeerProvider
用于提醒當(dāng)CacheManager中的緩存增加或者刪除時(shí)。
屬性:class:完整的工廠類名稱
properties:逗號(hào)分割的屬性
事件包括 增加cache和刪除cache
cacheManagerPeerProviderFactory:指定CacheManagerPeerProviderFactory,用于創(chuàng)建CacheManagerPeerProvider,用于在cluster中查找CacheManagers,主要用于機(jī)群環(huán)境
屬性:
class:完整的工廠類名稱
properties:逗號(hào)分割的屬性
默認(rèn)的配置:
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,
multicastGroupPort=4446, timeToLive=32"/>
<cacheManagerPeerProviderFactory class=
"net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,
rmiUrls=//server1:40000/sampleCache1|//server2:40000/sampleCache1
| //server1:40000/sampleCache2|//server2:40000/sampleCache2"/>
cacheManagerPeerListenerFactory:指定CacheManagerPeerListenerFactory,用于創(chuàng)建CacheManagerPeerListener,監(jiān)聽cluster中的復(fù)制信息
屬性:
class:完整的工廠類名稱
properties:逗號(hào)分割的屬性
默認(rèn)配置
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=fully_qualified_hostname_or_ip,
port=40001,
socketTimeoutMillis=120000"/>
defaultCache: 默認(rèn)緩存配置
必須屬性:
name:設(shè)置緩存的名稱,用于標(biāo)志緩存,惟一
maxElementsInMemory:在內(nèi)存中最大的對(duì)象數(shù)量
maxElementsOnDisk:在DiskStore中的最大對(duì)象數(shù)量,如為0,則沒有限制
eternal:設(shè)置元素是否永久的,如果為永久,則timeout忽略
overflowToDisk:是否當(dāng)memory中的數(shù)量達(dá)到限制后,保存到Disk
可選的屬性:
timeToIdleSeconds:設(shè)置元素過(guò)期前的空閑時(shí)間
timeToLiveSeconds:設(shè)置元素過(guò)期前的活動(dòng)時(shí)間
diskPersistent:是否disk store在虛擬機(jī)啟動(dòng)時(shí)持久化。默認(rèn)為false
diskExpiryThreadIntervalSeconds:運(yùn)行disk終結(jié)線程的時(shí)間,默認(rèn)為120秒
memoryStoreEvictionPolicy:策略關(guān)于Eviction
緩存子元素:
cacheEventListenerFactory:注冊(cè)相應(yīng)的的緩存監(jiān)聽類,用于處理緩存事件,如put,remove,update,和expire
bootstrapCacheLoaderFactory:指定相應(yīng)的BootstrapCacheLoader,用于在初始化緩存,以及自動(dòng)設(shè)置。
如下的例子:
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true,
replicatePuts=true,
replicateUpdates=true,
replicateUpdatesViaCopy=true,
replicateRemovals=true "/>
<bootstrapCacheLoaderFactory
class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"
properties="bootstrapAsynchronously=true, maximumChunkSizeBytes=5000000"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
cache配置同defaultCache
<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>
基本上都比較簡(jiǎn)單,當(dāng)然也可以直接使用程序?qū)崿F(xiàn)相應(yīng)的功能