Ehcache 缓存配置
Cache的配置很灵活,官方提供的Cache配置方式有好几种。你可以通过声明配置、在xml中配置、在程序里配置或者调用构造方法时传入不同的参数。
你可以将Cache的配置从代码中剥离出来,也可以在使用运行时配置,所谓的运行时配置无非也就是在代码中配置。以下是运行时配置的好处:
- 在同一个地方配置所有的Cache,这样很容易管理Cache的内存和磁盘消耗。
- 发布时可更改Cache配置。
- 可再安装阶段就检查出配置错误信息,而避免了运行时错误。
本文将会对ehcache.xml配置文件进行详细的阐述。在配置的时可以拷贝一个现有的ehcache.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-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:设置一个缓存项在过期前的闲置时间。即一个缓存项在其过期前,两次访问间隔的最大时间。仅在缓存项为非永久时有效。0表示不限闲置时间,默认为 0。
- timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0。也就是element存活时间无穷大。
- diskPersistent:在VM重启的时候是否持久化磁盘缓存。
- diskExpiryThreadIntervalSeconds:清理保存在磁盘上的过期缓存项目的线程的启动时间间隔,默认 120秒。
- diskSpoolBufferSizeMB:磁盘缓冲区的大小。写入磁盘的内容将缓冲在此区域,使用异步的方式写入磁盘。默认 30MB,每一个缓存区使用独立的缓冲区,如果遇到 OutOfMemory 错误时,可以尝试减小这个值。改进磁盘写入性能时,尝试增加这个值。将日志级别开到 Trace,当 DiskStore 执行磁盘写入时,可以看到对应日志
- statistics:是否收集统计信息。如果需要监控缓存使用情况,应该打开这个选项。默认为关闭(统计会影响性能)。设置 statistics="true"开启统计。
-
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。
-
LRU -least recently used
LFU -least frequently used
FIFO-first in first out
-
-
通过使用<persistence/>子元素,可以配置缓存区的持久化策略。<persistence/>元素的主要属性如下:
strategy:配置缓存区持久化的类型。可选值如下:
localRestartable:仅在使用 Ehcache企业版时有效。启用 RestartStore,拷贝所有的缓存项(包含堆和非堆中的)到磁盘中,此选项提供了缓存快速重启能力以及对磁盘上缓存的容错能力。
localTempSwap:当缓存容量达到上限时,将缓存对象 (包含堆和非堆中的)交换到磁盘中。"localTempSwap" 并不持久化缓存内容。
none:不持久化缓存内容。
distributed:按照<terracotta>标签配置的持久化方式执行。非分布式部署时,此选项不可用。
synchronousWrites:此属性仅在strategy="localRestartable"时有意义。默认false。设置为 true,缓存写入方法在缓存项成功写入磁盘前不会返回。 - 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" />
- SampleCache2配置
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" />
- 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 >
- 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 >
- 快速重启cache配置
-
< cache name ="example4Cache" maxElementsInMemory ="100" eternal ="false" timeToIdleSeconds ="0" timeToLiveSeconds ="259200" overflowToOffHeap ="true" maxMemoryOffHeap ="1024M" memoryStoreEvictionPolicy ="LRU" statistics ="true" > < persistence strategy ="localRestartable" synchronousWrites ="false" /> </ cache >