Ehcache 缓存配置
简介 |
Cache 的配置很灵活,官方提供的 Cache 配置方式有好几种。你可以通过声明配置、在 xml 中配置、在程序里配置或者调用构造方法时传入不同的参数。
你可以将 Cache 的配置从代码中剥离出来,也可以在使用运行时配置,所谓的运行时配置无非也就是在代码中配置。以下是运行时配置的好处:
· 在同一个地方配置所有的 Cache ,这样很容易管理 Cache 的内存和磁盘消耗。
· 发布时可更改 Cache 配置。
· 可再安装阶段就检查出配置错误信息,而避免了运行时错误。
本文将会对 ehcache.xml 配置文件进行详细的阐述。在配置的时可以拷贝一个现有的 ehcache.xml ,如果没有请点击这里 去下载。
ehcache-failsafe.xml |
如果你调用了 CacheManager 默认构造方法去创建 CacheManager 的实例,此方法会到 classpath 中找 ehcache.xml 文件,否则它会到类路径下找 ehcache-failsafe.xml 文件。而 ehcache-failsafe.xml 被包含在 jar 包中,所有它肯定能找的到。
ehcache-failsafe.xml 提供了一个非常简单的默认配置,这样可以使用户在没有创建 ehcache.xml 的情况下使用 Ehcache 。
不过这样做 Ehcache 会提醒用户创建一个正确的 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 编码的配置文件完全没有必要转换为 UTF8 。
一个 CacheManager 必须要有一个 XML 配置。由于磁盘路径或是监听端口,多个 CacheManager 使用同一个配置文件时会出现错误。
下面是 ehcache.xml 具体实例以及配置指南
<ehcache xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
· CacheManager 配置
DmulticastGroupPort=4446 ,这样可以配置监听端口。
· DiskStore 配置
如果你使用的 DiskStore (磁盘缓存),你必须要配置 DiskStore 配置项。如果不配置, Ehcache 将会使用 java.io.tmpdir 。
diskStroe 的“ path ”属性是用来配置磁盘缓存使用的物理路径的, Ehcache 磁盘缓存使用的文件后缀名是 .data 和 .index 。
<disStore path=”java.io.tmpdir”/>
· CacheManagerEventListener 配置
我们通过 CacheManagerEventListenerFactory 可以实例化一个 CacheManagerPeerProvider ,当我们从 CacheManager 中 added 和 removed Cache 时,将通知 CacheManagerPeerProvider ,这样一来,我们就可以很方面的对 CacheManager 中的 Cache 做一些统计。
注册到 CacheManager 的事件监听类名有: adding a Cache 和 removing a Cache
<cacheManagerEventListenerFacotory class=”” properties=””/>
· CacheManagerPeerProvider 配置
在集群中 CacheManager 配置 CacheManagerPeerProviderFactory 创建 CacheManagerPeerProvider 。具体的实例如下:
<cacheManagerPeerProviderFactoryclass="net.sf.ehcache.distribution.
RMICacheManagerPeerProviderFactory "
properties="peerDiscovery=manual, rmiUrls=//server1:40000/sampleCache1|//server2:40000/sampleCache1| //server1:40000/sampleCache2|//server2:40000/sampleCache2"
propertySeparator="," />
· CacheManagerPeerListener 配置
CacheManagerPeerListener 配置是用来监听集群中缓存消息的分发的。
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=fully_qualified_hostname_or_ip,
port=40001,
socketTimeoutMillis=120000"
propertySeparator="," />
· Cache 配置
· name : Cache 的唯一标识
· maxElementsInMemory :内存中最大缓存对象数。
· maxElementsOnDisk :磁盘中最大缓存对象数,若是 0 表示无穷大。
· eternal : Element 是否永久有效,一但设置了, timeout 将不起作用。
· overflowToDisk :配置此属性,当内存中 Element 数量达到 maxElementsInMemory 时, Ehcache 将会 Element 写到磁盘中。
· timeToIdleSeconds :设置 Element 在失效前的允许闲置时间。仅当 element 不是永久有效时使用,可选属性,默认值是 0 ,也就是可闲置时间无穷大。
· timeToLiveSeconds :设置 Element 在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当 element 不是永久有效时使用,默认是 0. ,也就是 element 存活时间无穷大。
· diskPersistent :是否缓存虚拟机重启期数据。(这个虚拟机是指什么虚拟机一直没看明白是什么 , 有高人还希望能指点一二)。
· diskExpiryThreadIntervalSeconds :磁盘失效线程运行时间间隔,默认是 120 秒。
· diskSpoolBufferSizeMB :这个参数设置 DiskStore (磁盘缓存)的缓存区大小。默认是 30MB 。每个 Cache 都应该有自己的一个缓冲区。
· memoryStoreEvictionPolicy :当达到 maxElementsInMemory 限制时, Ehcache 将会根据指定的策略去清理内存。默认策略是 LRU (最近最少使用)。你可以设置为 FIFO (先进先出)或是 LFU (较少使用)。这里比较遗憾, Ehcache 并没有提供一个用户定制策略的接口,仅仅支持三种指定策略,感觉做的不够理想。
· Cache Exception Handling 配置
<cacheExceptionHandlerFactory class="com.example.ExampleExceptionHandlerFactory" properties="logLevel=FINE"/>
总结 |
这里只对通用缓存的配置做了详细的阐述,至于 RMI 缓存和集群缓存可以参考这里 。
下面给出几个配置示例:
· Ehcache 默认 Cache 配置
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskSpoolBufferSizeMB="30"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
· SampleCache1 配置
简单配置,在 ehcache.xml 文件中有此配置,在使用 Ehcache 前最好将其删除掉,自己配置。
缓存名 sampleCache1 ,内存中最多可缓存 10000 个 Element ,其中的 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"
/>
Cache 名为 SampleCache2 ,内存中最多可以缓存 1000 个 element ,超出 1000 不能输出到磁盘中。缓存是永久有效的。
<cache name="sampleCache2"
maxElementsInMemory="1000"
eternal="true"
overflowToDisk="false"
memoryStoreEvictionPolicy="FIFO"
/>
· SampleCache3 配置
Cache 名为 SampleCache3 。可缓存到磁盘。磁盘缓存将会缓存虚拟机重启期的数据。磁盘缓存失效线程运行间隔时间是 10 分钟。
<cache name="sampleCache3"
maxElementsInMemory="500"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="1"
memoryStoreEvictionPolicy="LFU"
/>
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>
· sampleDistributedCache2 配置
<cache name="sampleDistributedCache2"
maxElementsInMemory="10"
eternal="false"
timeToIdleSeconds="100"
timeToLiveSeconds="100"
overflowToDisk="false">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=false, replicatePuts=false,
replicateUpdates=true, replicateUpdatesViaCopy=true,
replicateRemovals=false"/>
</cache>
· sampleDistributedCache3 配置
<!--
Sample distributed cache named sampleDistributedCache3.
This cache replicates using defaults except that the asynchronous replication
interval is set to 200ms.
-->
<cache name="sampleDistributedCache3"
maxElementsInMemory="10"
eternal="false"
timeToIdleSeconds="100"
timeToLiveSeconds="100"
overflowToDisk="false">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="asynchronousReplicationIntervalMillis=200"/>
</cache>