ehcache缓存说明

转自:http://scholers.iteye.com/blog/630373
配置文件例子:
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>




1. 预览
Ehcache 从 Hibernate 发展而来,逐渐涵盖了Cahce界的全部功能,是目前发展势头最好的一个项目。

标准缓存
分布式缓存(基于RMI/JGroups/JMS)
URL,页面片段缓存(类似OSCache的相关部分)
中央缓存服务器(类似Memcached)

2. 基本功能与配置
Ehcache的基本功能,可以从配置文件中学习。详见Ehcache的文档

总的来说,缓存与HashMap的最大不同,就是缓存设想内存是有限的,缓存的时效性也是有限的,所以可以设定内存数量的大小,可以执行失效算法 ,可以在内存满了的时候,按照最少访问等算法将缓存直接移除或切换到硬盘上。

另外注意,ehcache的CacheManager本身有一定的默认值。而在没有指定ehcache.xml的情况下, 会使用ehcache.jar里自带的ehcache_failsafe.xml,

2.1 对象在内存中的最大数量
因为内存是有限的,所以必须用maxElementsInMemory(必填项)设置每类对象在内存中的最大数量。ehcache_failsafe.xml 中为10000。

2.2 到达内存中最大量时的过期/移出算法
过期算法: 如果缓存已经失效,人道毁灭之。失效算法由3个参数组成:

eternal(必填项):如果为true,则永不过期,忽略后两个参数的设置。ehcache_failsafe.xml 为false.
timeToIdleSeconds: 空闲无访问时间,默认为0,永不过时。ehcache_failsafe.xml 设为120秒。
timeToLiveSeconds: 空闲无访问时间,默认为0,永不过时。ehcache_failsafe.xml 设为120秒。
移出算法:如果经过失效算法后,还是有很多有效的缓存,则执行清除算法。清除算法由两个参数组成:

memoryStoreEvictionPolicy: 默认为LRU(最近最少访问),另有先进先出(FIFO),最少访问次数(LFU)
overflowToDisk(必填项) 为true,则将清除出来的缓存持久化到磁盘,否则人道毁灭之。

2.3 储存到硬盘
maxElementsOnDisk,默认为0,无限多。ehcache_failsafe.xml为10000000。
diskExpiryThreadIntervalSeconds:使用过期算法清除磁盘中失效对象的间隔,默认为120秒。
diskSpoolBufferSizeMB ,默认为30M。

2.4 重启时缓存持久化
diskPersistent 当应用重启时,可将缓存先持久化到硬盘,重启后再行载入,节省大量的重新从数据库载入。但只适合那些缓存不怎么变化,或者有特殊机制保证重启后应用能接收到重启这段时间里缓存变化信息的情况。


3. 分布式缓存
Ehcache 有传统的RMI,1.5版的JGroups,1.6版的JMS,随大流还是先用RMI的好些。

3.1设置自身
这里设置在localhost的40001端口上侦听。如果要互相同步的CahceManager不都在一台机器上的话,hostName应该是实际IP。)

<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=localhost, port=40001,socketTimeoutMillis=2000" />

3.2 设置需要同步的对方服务器及缓存对象,
这里设置与40002端口上的CacheManager同步User与Role对象,如果还有第三台机器,则继续用|分割,继续往下列。同理,在40002端口上的cacheManager的ehcache.xml里,就需要配置与400001,40003的互通)

也有自动发现,广播的简单配法,但对广播天然恐惧,还是辛苦一点一个个静态列表配置文件的写了,虽然有点烦。

<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,
rmiUrls=//localhost:40002/org.springside.examples.miniweb.entity.user.User|//localhost:40002/org.springside.examples.miniweb.entity.user.Role"/>

3.3 缓存对象的配置
往每一个需要缓存的对象加入子对象cacheEventListenerFactory

<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true,
replicatePuts=true,
replicateUpdates=true,
replicateUpdatesViaCopy=true,
replicateRemovals=true
asynchronousReplicationIntervalMillis=<number of milliseconds">
propertySeparator="," />

replicateAsynchronously 对象同步是否异步完成,默认为true。如果比较紧急就设为false。 在一致性时间性要求不强的时候,设为异步可大大提供性能,因为它是异步立即返回的,而且可以批量提交。

replicateUpdatesViaCopy 是否将对象变更复制到所有节点,还是只是发送一个失效信息,让对方该缓存失效,当对方需要该缓存时重新计算载入。
默认为true。鉴于对象复制的消耗挺大的,又有锁的问题,而且对方也未必需要该对象,所以此属性建议设为false。如果业务上真的需要设为true时,就可考虑使用Terracotta了。

replicatePuts、replicateUpdates、replicateRemovals 增删改是否同步,默认都为true。但因为我们前面选择了失效算法,所以replicatePuts 要设为false。

所以我们一般的设置如下:

<cache name="org.springside.examples.miniweb.entity.user.User" maxElementsInMemory="500" overflowToDisk="true"
eternal="true">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=false,replicatePuts=false,replicateUpdatesViaCopy=false" />
</cache>

3.4 Shutdown
在分布式环境或持久化硬盘时,需要调用CacheManager的shutdown操作,Hibernate会自动shutdown它自己的cacheManager,如果在hibernate之外使用,你需要增加:

<listener>
<listener-class>net.sf.ehcache.constructs.web.ShutdownListener</listener-class>
</listener>

4.JMS式并发同步
与其他同步方式相比,JMS同步支持了非Cache节点的程序对Cahce的修改。

在分布式缓存中有一种需求:应用节点更改数据库数据后,需要通知所有缓存集群的节点,通常大家都是自行通过JMS实现的,而Ehcache的JMS Replicator 提供了一种标准的方案,提供PUT,REMOVE,REMOVE_ALL的标准操作。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本工程用于研究如何借助Ehcache缓存框架实现对页面的缓存 本工程编码方式:UTF-8 本工程开发工具:MyEclipse 说明: 1、ehcache.xml和ehcache.xsd两个文件可以在下在下载下来的名为“ehcache-core-x.x.x-distribution.tar.gz”压缩文件中找到 2、由于要实现Ehcache缓存页面,所以必须要添加“ehcache-web-2.0.4.jar” jar,该jar主要用于辅助Ehcache实现页面缓存 注意: 本web工程的发布不要使用Tomcat7,否则会出现如下异常: 2015-3-25 9:53:50 org.apache.catalina.loader.WebappClassLoader loadClass 信息: Illegal access: this web application instance has been stopped already. Could not load net.sf.ehcache.store.disk.DiskStore$KeySet. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact. java.lang.IllegalStateException at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1600) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559) at net.sf.ehcache.store.disk.DiskStore.keySet(DiskStore.java:560) at net.sf.ehcache.store.disk.DiskStorageFactory$DiskExpiryTask.run(DiskStorageFactory.java:838) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441) at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:317) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:150) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(ScheduledThreadPoolExecutor.java:98) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(ScheduledThreadPoolExecutor.java:181) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:205) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:619) 相关jar下载地址: Ehcache 对象、数据缓存:http://ehcache.org/downloads/destination?name=ehcache-core-2.5.2-distribution.tar.gz&bucket=tcdistributions&file=ehcache
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值