日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

EhCache的配置

發(fā)布時間:2024/9/20 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 EhCache的配置 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標準>>>

Cache的配置很靈活,官方提供的Cache配置方式有好幾種。你可以通過聲明配置、在xml中配置、在程序里配置或者調(diào)用構(gòu)造方法時傳入不同的參數(shù)。

?

你可以將Cache的配置從代碼中剝離出來,也可以在使用運行時配置,所謂的運行時配置無非也就是在代碼中配置。以下是運行時配置的好處:

?

·???在同一個地方配置所有的Cache,這樣很容易管理Cache的內(nèi)存和磁盤消耗。

·???發(fā)布時可更改Cache配置。

·???可再安裝階段就檢查出配置錯誤信息,而避免了運行時錯誤。

?

本文將會對ehcache.xml配置文件進行詳細的闡述。在配置的時可以拷貝一個現(xiàn)有的ehcache.xml,如果沒有請點擊這里去下載。

?

ehcache-failsafe.xml

?

如果你調(diào)用了CacheManager默認構(gòu)造方法去創(chuàng)建CacheManager的實例,此方法會到classpath中找ehcache.xml文件,否則它會到類路徑下找ehcache-failsafe.xml文件。而ehcache-failsafe.xml被包含在jar包中,所有它肯定能找的到。

?

ehcache-failsafe.xml提供了一個非常簡單的默認配置,這樣可以使用戶在沒有創(chuàng)建ehcache.xml的情況下使用Ehcache

?

不過這樣做Ehcache會提醒用戶創(chuàng)建一個正確的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.xml和其他配置文件

?

Ehcache-1.6之前的版本,只支持ASCII編碼的ehcache.xml配置文件。在Ehcach-1.6之后版本中,支持UTF8編碼的ehcache.xml配置文件。因為向后兼容,所有采用ASCII編碼的配置文件完全沒有必要轉(zhuǎn)換為UTF8

?

一個CacheManager必須要有一個XML配置。由于磁盤路徑或是監(jiān)聽端口,多個CacheManager使用同一個配置文件時會出現(xiàn)錯誤。

?

下面是ehcache.xml具體實例以及配置指南

?

<ehcache xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance

?

&middot;???CacheManager配置

?

DmulticastGroupPort=4446,這樣可以配置監(jiān)聽端口。

?

&middot;???DiskStore配置

?

如果你使用的DiskStore(磁盤緩存),你必須要配置DiskStore配置項。如果不配置,Ehcache將會使用java.io.tmpdir

?

diskStroe的“path”屬性是用來配置磁盤緩存使用的物理路徑的,Ehcache磁盤緩存使用的文件后綴名是.data.index

?

<disStore path=”java.io.tmpdir”/>

?

&middot;???CacheManagerEventListener配置

?

我們通過CacheManagerEventListenerFactory可以實例化一個CacheManagerPeerProvider,當我們從CacheManageraddedremoved Cache時,將通知CacheManagerPeerProvider,這樣一來,我們就可以很方面的對CacheManager中的Cache做一些統(tǒng)計。

?

注冊到CacheManager的事件監(jiān)聽類名有:?adding a Cacheremoving a Cache

?

<cacheManagerEventListenerFacotory class=”” properties=””/>

?

&middot;???CacheManagerPeerProvider配置

?

在集群中CacheManager配置CacheManagerPeerProviderFactory創(chuàng)建CacheManagerPeerProvider。具體的實例如下:

<cacheManagerPeerProviderFactoryclass="net.sf.ehcache.distribution.

RMICacheManagerPeerProviderFactory"

properties="peerDiscovery=manual, rmiUrls=//server1:40000/sampleCache1|//server2:40000/sampleCache1| //server1:40000/sampleCache2|//server2:40000/sampleCache2"

propertySeparator="," />

&middot;???CacheManagerPeerListener配置

?

CacheManagerPeerListener配置是用來監(jiān)聽集群中緩存消息的分發(fā)的。

?

<cacheManagerPeerListenerFactory

????class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"

????properties="hostName=fully_qualified_hostname_or_ip,

????????????????port=40001,

????????????????socketTimeoutMillis=120000"

????????????????propertySeparator="," />

&middot;???Cache配置

?

&middot;???????????nameCache的唯一標識

&middot;???????????maxElementsInMemory:內(nèi)存中最大緩存對象數(shù)。

&middot;???????????maxElementsOnDisk:磁盤中最大緩存對象數(shù),若是0表示無窮大。

&middot;???????????eternalElement是否永久有效,一但設(shè)置了,timeout將不起作用。

&middot;???????????overflowToDisk:配置此屬性,當內(nèi)存中Element數(shù)量達到maxElementsInMemory時,Ehcache將會Element寫到磁盤中。

&middot;???????????timeToIdleSeconds:設(shè)置Element在失效前的允許閑置時間。僅當element不是永久有效時使用,可選屬性,默認值是0,也就是可閑置時間無窮大。

&middot;???????????timeToLiveSeconds:設(shè)置Element在失效前允許存活時間。最大時間介于創(chuàng)建時間和失效時間之間。僅當element不是永久有效時使用,默認是0.,也就是element存活時間無窮大。

&middot;???????????diskPersistent:是否緩存虛擬機重啟期數(shù)據(jù)。(這個虛擬機是指什么虛擬機一直沒看明白是什么,有高人還希望能指點一二)。

&middot;???????????diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認是120秒。

&middot;???????????diskSpoolBufferSizeMB:這個參數(shù)設(shè)置DiskStore(磁盤緩存)的緩存區(qū)大小。默認是30MB。每個Cache都應(yīng)該有自己的一個緩沖區(qū)。

&middot;???????????memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據(jù)指定的策略去清理內(nèi)存。默認策略是LRU(最近最少使用)。你可以設(shè)置為FIFO(先進先出)或是LFU(較少使用)。這里比較遺憾,Ehcache并沒有提供一個用戶定制策略的接口,僅僅支持三種指定策略,感覺做的不夠理想。

?

&middot;???Cache Exception Handling配置

?

<cacheExceptionHandlerFactory class="com.example.ExampleExceptionHandlerFactory"??????????????????????????????properties="logLevel=FINE"/>

?

總結(jié)

?

這里只對通用緩存的配置做了詳細的闡述,至于RMI緩存和集群緩存可以參考這里。

?

下面給出幾個配置示例:

?

&middot;???Ehcache默認Cache配置

?

<defaultCache

????????maxElementsInMemory="10000"

????????eternal="false"

????????timeToIdleSeconds="120"

????????timeToLiveSeconds="120"

????????overflowToDisk="true"

????????diskSpoolBufferSizeMB="30"

????????maxElementsOnDisk="10000000"

????????diskPersistent="false"

????????diskExpiryThreadIntervalSeconds="120"

????????memoryStoreEvictionPolicy="LRU"

????????/>

?

&middot;???SampleCache1配置

?

簡單配置,在ehcache.xml文件中有此配置,在使用Ehcache前最好將其刪除掉,自己配置。

?

緩存名sampleCache1,內(nèi)存中最多可緩存10000Element,其中的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"

????????/>

?

&middot;???SampleCache2配置

?

Cache名為SampleCache2,內(nèi)存中最多可以緩存1000element,超出1000不能輸出到磁盤中。緩存是永久有效的。

?

<cache name="sampleCache2"

???????maxElementsInMemory="1000"

???????eternal="true"

???????overflowToDisk="false"

???????memoryStoreEvictionPolicy="FIFO"

????????/>

?

&middot;???SampleCache3配置

?

Cache名為SampleCache3。可緩存到磁盤。磁盤緩存將會緩存虛擬機重啟期的數(shù)據(jù)。磁盤緩存失效線程運行間隔時間是10分鐘。

?

<cache name="sampleCache3"

???????maxElementsInMemory="500"

???????eternal="false"

???????overflowToDisk="true"

???????timeToIdleSeconds="300"

???????timeToLiveSeconds="600"

???????diskPersistent="true"

???????diskExpiryThreadIntervalSeconds="1"

???????memoryStoreEvictionPolicy="LFU"

????????/>

?

&middot;???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>

?

&middot;???sampleDistributedCache2配置

轉(zhuǎn)載于:https://my.oschina.net/xuqiang/blog/100332

總結(jié)

以上是生活随笔為你收集整理的EhCache的配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。