以上时间值都是以秒作为单位的。
<?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配置
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方式的
实际例子如下所示:
- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="ehcache.xsd">
- <diskStore path="java.io.tmpdir/cacheweb" />
- <cacheManagerPeerProviderFactory
- class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
- properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446" />
- <cacheManagerPeerListenerFactory
- class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" />
- <cacheManagerEventListenerFactory
- class="ins.framework.cache.ehcache.LogCacheManagerEventListenerFactory" />
- <defaultCache maxElementsInMemory="3" eternal="false"
- timeToIdleSeconds="1" timeToLiveSeconds="1" overflowToDisk="false"
- memoryStoreEvictionPolicy="LRU">
- <cacheEventListenerFactory
- class="ins.framework.cache.ehcache.LogCacheEventListenerFactory" />
- <cacheEventListenerFactory
- class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
- </defaultCache>
- <cache name="userCache" maxElementsInMemory="2" eternal="false"
- overflowToDisk="false" timeToIdleSeconds="60" timeToLiveSeconds="120"
- memoryStoreEvictionPolicy="LRU">
- <cacheEventListenerFactory
- class="ins.framework.cache.ehcache.LogCacheEventListenerFactory" />
- <cacheEventListenerFactory
- class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
- </cache>
- </ehcache>