關于Ehcache 配置

openkk 13年前發布 | 37K 次閱讀 Ehcache 緩存組件

ehcache的默認配置文件名為ehcache.xml,也可以自己指定。配置文件比較的簡單,基本上也就是常用的幾個元素。

配置元素說明:

diskStore:配置DiskStore,當需要使用磁盤保存的時候,需要對象實現序列化接口

    屬性:path 配置文件存儲位置,如user.home,user.dir,java.io.tmpdir

cacheManagerEventListenerFactory :指定CacheManagerEventListenerFactory,用于創建CacheManagerPeerProvider

用于提醒當CacheManager中的緩存增加或者刪除時。
   
    屬性:class:完整的工廠類名稱
         properties:逗號分割的屬性

事件包括 增加cache和刪除cache

cacheManagerPeerProviderFactory:指定CacheManagerPeerProviderFactory,用于創建CacheManagerPeerProvider,用于在cluster中查找CacheManagers,主要用于機群環境

    屬性:
        class:完整的工廠類名稱
        properties:逗號分割的屬性

默認的配置:
 

<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,用于創建CacheManagerPeerListener,監聽cluster中的復制信息
    屬性:
        class:完整的工廠類名稱
        properties:逗號分割的屬性

默認配置
 
    <cacheManagerPeerListenerFactory
        class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
        properties="hostName=fully_qualified_hostname_or_ip,
                    port=40001,
                    socketTimeoutMillis=120000"/>     


defaultCache: 默認緩存配置

    必須屬性:
        name:設置緩存的名稱,用于標志緩存,惟一
        maxElementsInMemory:在內存中最大的對象數量
        maxElementsOnDisk:在DiskStore中的最大對象數量,如為0,則沒有限制
        eternal:設置元素是否永久的,如果為永久,則timeout忽略
        overflowToDisk:是否當memory中的數量達到限制后,保存到Disk

    可選的屬性:
        timeToIdleSeconds:設置元素過期前的空閑時間
        timeToLiveSeconds:設置元素過期前的活動時間
        diskPersistent:是否disk store在虛擬機啟動時持久化。默認為false
        diskExpiryThreadIntervalSeconds:運行disk終結線程的時間,默認為120秒
        memoryStoreEvictionPolicy:策略關于Eviction

緩存子元素:
   
    cacheEventListenerFactory:注冊相應的的緩存監聽類,用于處理緩存事件,如put,remove,update,和expire
    bootstrapCacheLoaderFactory:指定相應的BootstrapCacheLoader,用于在初始化緩存,以及自動設置。
如下的例子:
 
 <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" /> </pre>

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>     

基本上都比較簡單,當然也可以直接使用程序實現相應的功能

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!