Ehcache配置文件的详细说明

Ehcache配置文件的详细说明
 
<cache name="CACHE_FUNC" maxElementsInMemory="2" eternal="false" timeToIdleSeconds="10" timeToLiveSeconds="20" overflowToDisk="true" diskPersistent="true" diskExpiryThreadIntervalSeconds="120"  />
 
maxElementsInMemory :cache 中最多可以存放的元素的数量。如果放入cache中的元素超过这个数值,有两种情况:1、若 overflowToDisk的属性值为true,会将cache中多出的元素放入磁盘文件中。2、若 overflowToDisk的属性值为false,会根据 memoryStoreEvictionPolicy的策略替换cache中原有的元素。
 
eternal :意思是是否永驻内存。如果值是true,cache中的元素将一直保存在内存中,不会因为时间超时而丢失,所以在这个值为true的时候, timeToIdleSecondstimeToLiveSeconds两个属性的值就不起作用了。
 
timeToIdleSeconds :就是访问这个cache中元素的最大间隔时间。如果超过这个时间没有访问这个cache中的某个元素,那么这个元素将被从cache中清除。
 
timeToLiveSeconds : 这是cache中元素的生存时间。意思是从cache中的某个元素从创建到消亡的时间,从创建开始计时,当超过这个时间,这个元素将被从cache中清除。
 
overflowToDisk :溢出是否写入磁盘。系统会根据标签<diskStore path="java.io.tmpdir"/> 中path的值查找对应的属性值,如果系统的java.io.tmpdir的值是 D:/temp,写入磁盘的文件就会放在这个文件夹下。文件的名称是cache的名称,后缀名的data。如:CACHE_FUNC.data。这个属性在解释 maxElementsInMemory的时候也已经说过了。
 
diskExpiryThreadIntervalSeconds  :磁盘缓存的清理线程运行间隔
 
memoryStoreEvictionPolicy :内存存储与释放策略。有三个值:
LRU -least recently used
LFU -least frequently used
FIFO-first in first out, the oldest element by creation time
 
diskPersistent : 是否持久化磁盘缓存。当这个属性的值为true时,系统在初始化的时候会在磁盘中查找文件名为cache名称,后缀名为index的的文件,如CACHE_FUNC.index 。这个文件中存放了已经持久化在磁盘中的cache的index,找到后把cache加载到内存。要想把cache真正持久化到磁盘,写程序时必须注意,在是用 net.sf.ehcache.Cachevoid put (Element element)方法后要使用 void flush()方法。
 

以上时间值都是以秒作为单位的。



<?xml version="1.0" encoding="UTF-8"?>
<ehcache>

    <diskStore path="java.io.tmpdir/hibernate/NINE" />

    <!-- Special objects setting. -->
    <cache name="com.xxx.entity.invoice.Invoice" maxElementsInMemory="500" overflowToDisk="true" eternal="true">
        <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
            properties="replicatePuts=false,replicateUpdatesViaCopy=false" />
    </cache>
    
    <cache name="com.xxx.biz.entity.Role" maxElementsInMemory="500" overflowToDisk="true" eternal="true">
        <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
            properties="replicatePuts=false,replicateUpdatesViaCopy=false" />
    </cache>
    <cache name="com.xxx.biz.entity.Role.auths" maxElementsInMemory="500" overflowToDisk="true" eternal="true">
        <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
            properties="replicatePuts=false,replicateUpdatesViaCopy=false" />
    </cache>

    <cache name="com.xxx.biz.entity.Authority" maxElementsInMemory="500" overflowToDisk="true" eternal="true" />

    <!--
        DefaultCache setting.
        Modify ehcache-safe.xml for timeToIdleSeconds,timeToLiveSecond,diskExpiryThreadIntervalSeconds
        Use ehcache-safe.xml default for maxElementsInMemory,maxElementsOnDisk,overflowToDisk,eternal Use ehcache default
        for memoryStoreEvictionPolicy,diskPersistent,.
    -->
    <defaultCache maxElementsInMemory="10000" overflowToDisk="true" eternal="false"
        memoryStoreEvictionPolicy="LRU" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="600"
        timeToIdleSeconds="3600" timeToLiveSeconds="100000" diskPersistent="false" />
</ehcache>


ehcach.xml配置文件主要参数的解释:


//DiskStore 配置,cache文件的存放目录 ,主要的值有
*user.home - 用户主目录
* user.dir - 用户当前的工作目录
* java.io.tmpdir - Default temp file path默认的temp文件目录

//强制默认的cache配置

< defaultCache  
             maxElementsInMemory = " 10000 "
             eternal = " false "
             timeToIdleSeconds = " 120 "
             timeToLiveSeconds = " 120 "
             overflowToDisk = " true "
             diskSpoolBufferSizeMB = " 30 "
             maxElementsOnDisk = " 10000000 "
             diskPersistent = " false "
             diskExpiryThreadIntervalSeconds = " 120 "
             memoryStoreEvictionPolicy = " LRU "
             />

必须属性:
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,用于在初始化缓存,以及自动设置。

分布式缓存配置方法 是基于RMI方式的

实际例子如下所示:

[xhtml] view plain copy
  1. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:noNamespaceSchemaLocation="ehcache.xsd">  
  3.     <diskStore path="java.io.tmpdir/cacheweb" />  
  4.     <cacheManagerPeerProviderFactory  
  5.         class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"  
  6.         properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446" />  
  7.     <cacheManagerPeerListenerFactory  
  8.         class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" />  
  9.     <cacheManagerEventListenerFactory  
  10.         class="ins.framework.cache.ehcache.LogCacheManagerEventListenerFactory" />  
  11.     <defaultCache maxElementsInMemory="3" eternal="false"  
  12.         timeToIdleSeconds="1" timeToLiveSeconds="1" overflowToDisk="false"  
  13.         memoryStoreEvictionPolicy="LRU">  
  14.         <cacheEventListenerFactory  
  15.             class="ins.framework.cache.ehcache.LogCacheEventListenerFactory" />  
  16.         <cacheEventListenerFactory  
  17.             class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />  
  18.     </defaultCache>  
  19.     <cache name="userCache" maxElementsInMemory="2" eternal="false"  
  20.         overflowToDisk="false" timeToIdleSeconds="60" timeToLiveSeconds="120"  
  21.         memoryStoreEvictionPolicy="LRU">  
  22.         <cacheEventListenerFactory  
  23.             class="ins.framework.cache.ehcache.LogCacheEventListenerFactory" />  
  24.         <cacheEventListenerFactory  
  25.             class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />  
  26.     </cache>  
  27. </ehcache> 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值