Ehcache学习

来源:互联网 发布:淘宝心选在哪里设置 编辑:程序博客网 时间:2024/05/23 11:54
  • 参考文档:

部分都是网络上查询的资料,很多,不列举了。

EHCache 简介:

http://apps.hi.baidu.com/share/detail/7491847

http://wangjicn.cn/data/read/9082403332378.html

http://blog.csdn.net/mgoann/archive/2009/04/16/4083179.aspx

http://yuanyong.javaeye.com/blog/691499

Spring 整合 EHCache 

http://wangjicn.cn/data/read/909291257438.html

http://www.yybean.com/ehcache-getting-started-series-5-a-distributed-cache-clus

ter-environment-configuration

http://zhyt710.javaeye.com/blog/333213

http://tech.ddvip.com/2010-04/1270187299149502.html

http://blog.csdn.net/goodboylllll/archive/2010/04/01/5442329.aspx

 

  • 术语及缩写解释

EHCache:EHCache 是一个快速的、轻量级的、易于使用的、进程内的缓存。它支持 read-only 和 read/write 缓存,内存和磁盘缓存。是一个非常轻量级的缓存实现,而且从 1.2 之后就支持了集群,即分布式。

  • 背景

系统缓存是位于应用程序与物理数据源之间,用于临时存放复制数据的内存区域,目的是为了减少应用程序对物理数据源访问的次数,从而提高应用程序的运行性能. 缓存设想内存是有限的,缓存的时效性也是有限的,所以可以设定内存数量的大小,可以执行失效算法,可以在内存满了的时候,按照最少访问等算法将缓存直接移除或切换到硬盘上。

Ehcache 从 Hibernate 发展而来,逐渐涵盖了 Cahce 界的全部功能,是目前发展势头最好的一个项目。具有快速,简单,低消耗,依赖性小,扩展性强,支持对象或序列化缓存,支持缓存或元素的失效,提供 LRU、LFU 和 FIFO 缓存策略,支持内存缓存和磁盘缓存,分布式缓存机制等等特点。

Cache 存储方式 :内存或磁盘。

官方网站  http://ehcache.sourceforge.net/

 

  • 主要特性

1.          快速 .

2.          简单 .

3.          多种缓存策略

4.          缓存数据有两级:内存和磁盘,因此无需担心容量问题

5.          缓存数据会在虚拟机重启的过程中写入磁盘

6.          可以通过 RMI 、可插入 API 等方式进行分布式缓存

7.          具有缓存和缓存管理器的侦听接口

8.          支持多缓存管理器实例,以及一个实例的多个缓存区域

9.          提供 Hibernate 的缓存实现

  • Ehcache 缓存配置

Cache 的配置很灵活,官方提供的Cache 配置方式有好几种。你可以通过声明配置、在xml 中配置、在程序里配置或者调用构造方法时传入不同的参数,具体的Cache 的获取后续讲到。你可以将Cache 的配置从代码中剥离出来,也可以在使用运行时配置,所谓的运行时配置无非也就是在代码中配置。以下是运行时配置的好处:

Ø   在同一个地方配置所有的Cache ,这样很容易管理Cache 的内存和磁盘消耗。

Ø   发布时可更改Cache 配置。

Ø   可再安装阶段就检查出配置错误信息,而避免了运行时错误。

本文将会对ehcache.xml 配置文件进行详细的阐述。如果你调用了CacheManager 默认构造方法去创建CacheManager 的实例,此方法会到classpath 中找ehcache.xml 文件,否则它会到类路径下找ehcache-failsafe.xml 文件。而ehcache-failsafe.xml 被包含在jar 包中,所有它肯定能找的到。

ehcache-failsafe.xml 提供了一个非常简单的默认配置,这样可以使用户在没有创建ehcache.xml 的情况下使用Ehcache 。不过这样做Ehcache 会提醒用户创建一个正确的Ehcache 配置。

ehcache.xml 片段:

Xml代码  收藏代码
  1. <ehcache>  
  2.     <diskStore path="java.io.tmpdir"/>  
  3.     <defaultCache  
  4.             maxElementsInMemory="10000"  
  5.             eternal="false"  
  6.             timeToIdleSeconds="120"  
  7.             timeToLiveSeconds="120"  
  8.             overflowToDisk="true"  
  9.             maxElementsOnDisk="10000000"  
  10.             diskPersistent="false"  
  11.             diskExpiryThreadIntervalSeconds="120"  
  12.             memoryStoreEvictionPolicy="LRU"  
  13.             />  
  14. </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

l   CacheManager 配置

DmulticastGroupPort=4446 ,这样可以配置监听端口。

l   DiskStore 配置

如果你使用的DiskStore (磁盘缓存),你必须要配置DiskStore 配置项。如果不配置,Ehcache 将会使用java.io.tmpdir 。diskStroe 的“path” 属性是用来配置磁盘缓存使用的物理路径的,Ehcache 磁盘缓存使用的文件后缀名是.data 和.index 。

<disStore path=”java.io.tmpdir”/>

l   CacheManagerEventListener 配置

我们通过CacheManagerEventListenerFactory 可以实例化一个CacheManagerPeerProvider ,当我们从CacheManager 中added 和removed Cache 时,将通知CacheManagerPeerProvider ,这样一来,我们就可以很方面的对CacheManager 中的Cache 做一些统计。

注册到CacheManager 的事件监听类名有: adding a Cache 和removing a Cache <cacheManagerEventListenerFacotory class=”” properties=””/>

l   CacheManagerPeerProvider 配置

在集群中CacheManager 配置CacheManagerPeerProviderFactory 创建CacheManagerPeerProvider 。具体的实例如下:

 

Xml代码  收藏代码
  1. <cacheManagerPeerProviderFactoryclasscacheManagerPeerProviderFactoryclass="net.sf.ehcache.distribution. RMICacheManagerPeerProviderFactory"  
  2.   
  3. properties="peerDiscovery=manual, rmiUrls=//server1:40000/sampleCache1|//server2:40000/sampleCache1| //server1:40000/sampleCache2|//server2:40000/sampleCache2"  
  4.   
  5. propertySeparator="," />  
  6.   
  7. l  CacheManagerPeerListener配置  
  8.   
  9. CacheManagerPeerListener配置是用来监听集群中缓存消息的分发的。  
  10. <cacheManagerPeerListenerFactory  
  11.     class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"  
  12.     properties="hostName=fully_qualified_hostname_or_ip,  
  13.                 port=40001,  
  14.                socketTimeoutMillis=120000"  
  15.                 propertySeparator="," />   
 

l   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 配置

Xml代码  收藏代码
  1. <defaultCache  
  2.         maxElementsInMemory="10000"  
  3.         eternal="false"  
  4.         timeToIdleSeconds="120"  
  5.         timeToLiveSeconds="120"  
  6.         overflowToDisk="true"  
  7.         diskSpoolBufferSizeMB="30"  
  8.         maxElementsOnDisk="10000000"  
  9.         diskPersistent="false"  
  10.         diskExpiryThreadIntervalSeconds="120"  
  11.         memoryStoreEvictionPolicy="LRU"  
  12.         />   

 Ø   SampleCache1 配置

 

简单配置,在ehcache.xml 文件中有此配置,在使用Ehcache 前最好将其删除掉,自己配置。 
缓存名sampleCache1 ,内存中最多可缓存10000 个Element ,其中的element 会在闲置5 分钟或是存活10 分钟之后失效。 
超过10000element 时,element 将会输出到磁盘中,输出路径是java.io.tmpdir 。

 

Xml代码  收藏代码
  1. <cache name="sampleCache1"  
  2.        maxElementsInMemory="10000"  
  3.        maxElementsOnDisk="1000"  
  4.        eternal="false"  
  5.        overflowToDisk="true"  
  6.        diskSpoolBufferSizeMB="20"  
  7.        timeToIdleSeconds="300"  
  8.        timeToLiveSeconds="600"  
  9.        memoryStoreEvictionPolicy="LFU"  
  10.         />   

 

Ø   SampleCache2 配置

 

Cache 名为SampleCache2 ,内存中最多可以缓存1000 个element ,超出1000 不能输出到磁盘中。缓存是永久有效的。

Xml代码  收藏代码
  1. <cache name="sampleCache2"  
  2.        maxElementsInMemory="1000"  
  3.        eternal="true"  
  4.        overflowToDisk="false"  
  5.        memoryStoreEvictionPolicy="FIFO"  
  6.         />   
 

Ø   SampleCache3 配置

 

Cache 名为SampleCache3 。可缓存到磁盘。磁盘缓存将会缓存虚拟机重启期的数据。磁盘缓存失效线程运行间隔时间是10 分钟。

 

Xml代码  收藏代码
  1. <cache name="sampleCache3"  
  2.        maxElementsInMemory="500"  
  3.        eternal="false"  
  4.        overflowToDisk="true"  
  5.        timeToIdleSeconds="300"  
  6.        timeToLiveSeconds="600"  
  7.        diskPersistent="true"  
  8.        diskExpiryThreadIntervalSeconds="1"  
  9.        memoryStoreEvictionPolicy="LFU"  
  10.         />   

 

Ø   sampleDistributedCache1 配置
Cache 名为sampleDistributedCache1 。

Xml代码  收藏代码
  1. <cache name="sampleDistributedCache1"  
  2.        maxElementsInMemory="10"  
  3.        eternal="false"  
  4.        timeToIdleSeconds="100"  
  5.        timeToLiveSeconds="100"  
  6.        overflowToDisk="false">  
  7.     <cacheEventListenerFactory  
  8.             class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>  
  9.     <bootstrapCacheLoaderFactory  
  10.             class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>  
  11. </cache>   

 

 

Ø   sampleDistributedCache2 配置

 

Xml代码  收藏代码
  1. <cache name="sampleDistributedCache2"  
  2.        maxElementsInMemory="10"  
  3.        eternal="false"  
  4.        timeToIdleSeconds="100"  
  5.        timeToLiveSeconds="100"  
  6.   
  7.        overflowToDisk="false">  
  8.   
  9.     <cacheEventListenerFactory  
  10.             class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"  
  11.             properties="replicateAsynchronously=falsereplicatePuts=false,  
  12.                         replicateUpdates=truereplicateUpdatesViaCopy=true,  
  13.                        replicateRemovals=false"/>  
  14.   
  15. </cache>   

 

Ø   sampleDistributedCache3 配置

 

Xml代码  收藏代码
  1. <!--  
  2. Sample distributed cache named sampleDistributedCache3.  
  3. This cache replicates using defaults except that the asynchronous replication  
  4. interval is set to 200ms.  
  5. -->  
  6. <cache name="sampleDistributedCache3"  
  7.        maxElementsInMemory="10"  
  8.        eternal="false"  
  9.        timeToIdleSeconds="100"  
  10.        timeToLiveSeconds="100"  
  11.        overflowToDisk="false">  
  12.     <cacheEventListenerFactory  
  13.             class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"  
  14.             properties="asynchronousReplicationIntervalMillis=200"/>  

  1. </cache>  


文章出处:http://ligf06.iteye.com/blog/1710878

0 0
原创粉丝点击