分享

EhCache介绍

 昵称21365845 2015-07-06

ehcache是一个非常轻量级的缓存实现,而且从1.2之后就支持了集群,而且是hibernate默认的缓存provider。EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。

 

Ehcache的分布式缓存有传统的RMI,1.5版的JGroups,1.6版的JMS。分布式缓存主要解决集群环境中不同的服务器间的数据的同步问题。

 

使用Spring的AOP进行整合,可以灵活的对方法的返回结果对象进行缓存。

 

CachingFilter功能可以对HTTP响应的内容进行缓存。

 

1、主要特性
     1. 快速.
     2. 简单.
     3. 多种缓存策略
     4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题
     5. 缓存数据会在虚拟机重启的过程中写入磁盘
     6. 可以通过RMI、可插入API等方式进行分布式缓存
     7. 具有缓存和缓存管理器的侦听接口
     8. 支持多缓存管理器实例,以及一个实例的多个缓存区域
     9. 提供Hibernate的缓存实现
     10. 等等

 

2、配置文件介绍(普通缓存) 

Xml代码  收藏代码
  1. <ehcache>  
  2.     <!-- 指定一个文件目录,当EHCache把数据写到硬盘上时,将把数据写到这个文件目录下 -->  
  3.     <diskStore path="java.io.tmpdir"/>  
  4.   
  5.     <!-- 设定缓存的默认数据过期策略 -->  
  6.     <defaultCache  
  7.             maxElementsInMemory="10000"  
  8.             eternal="false"  
  9.             overflowToDisk="true"  
  10.             timeToIdleSeconds="0"  
  11.             timeToLiveSeconds="0"  
  12.             diskPersistent="false"  
  13.             diskExpiryThreadIntervalSeconds="120"/>  
  14.       
  15.     <!--    
  16.         设定具体的命名缓存的数据过期策略  
  17.   
  18.         cache元素的属性:  
  19.             name:缓存名称  
  20.               
  21.             maxElementsInMemory:内存中最大缓存对象数  
  22.               
  23.             maxElementsOnDisk:硬盘中最大缓存对象数,若是0表示无穷大  
  24.               
  25.             eternal:true表示对象永不过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false  
  26.               
  27.             overflowToDisk:true表示当内存缓存的对象数目达到了maxElementsInMemory界限后,会把溢出的对象写到硬盘缓存中。注意:如果缓存的对象要写入到硬盘中的话,则该对象必须实现了Serializable接口才行。  
  28.               
  29.             diskSpoolBufferSizeMB:磁盘缓存区大小,默认为30MB。每个Cache都应该有自己的一个缓存区。  
  30.               
  31.             diskPersistent:是否缓存虚拟机重启期数据  
  32.               
  33.             diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认为120秒  
  34.   
  35.             timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,如果处于空闲状态的时间超过了timeToIdleSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清空。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地处于空闲状态  
  36.               
  37.             timeToLiveSeconds:设定对象允许存在于缓存中的最长时间,以秒为单位。当对象自从被存放到缓存中后,如果处于缓存中的时间超过了 timeToLiveSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清除。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地存在于缓存中。timeToLiveSeconds必须大于timeToIdleSeconds属性,才有意义  
  38.   
  39.             memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。可选策略有:LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)。  
  40.     -->  
  41.     <cache name="CACHE1"  
  42.            maxElementsInMemory="1000"  
  43.            eternal="true"  
  44.            overflowToDisk="true"/>    
  45.              
  46.     <cache name="CACHE2"  
  47.         maxElementsInMemory="1000"  
  48.         eternal="false"  
  49.         timeToIdleSeconds="200"  
  50.         timeToLiveSeconds="4000"  
  51.         overflowToDisk="true"/>  
  52. </ehcache>  

 

3、配置文件介绍(分布式缓存) 

     1)RMI集群模式

          A、手工发现

               需要指定节点发现模式peerDiscovery值为manual,rmiUrls设置为另一台服务器的IP、端口和缓存名等信息。

Xml代码  收藏代码
  1. <cacheManagerPeerProviderFactory   
  2.     class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"   
  3.     properties="peerDiscovery=manual,  
  4.     rmiUrls=//192.168.0.12:4567/oschina_cache|//192.168.0.13:4567/oschina_cache"  
  5. />  

  

          B、自动发现

                需要指定节点发现模式peerDiscovery值为automatic自动,同时组播地址可以指定D类IP地址空间,范围从 224.0.1.0 到 238.255.255.255 中的任何一个地址。

Xml代码  收藏代码
  1. <cacheManagerPeerProviderFactory  
  2.     class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"  
  3.     properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,  
  4.     multicastGroupPort=4446, timeToLive=32"  
  5. />  

 

           需要在每个cache属性中加入
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>

Xml代码  收藏代码
  1. <cache name="demoCache"  
  2.     maxElementsInMemory="10000"  
  3.     eternal="true"  
  4.     overflowToDisk="true">  
  5.     <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>  
  6. </cache>   

         

4、通过编程方式使用EhCache

Java代码  收藏代码
  1. //从classes目录查找ehcache.xml配置文件  
  2. CacheManager cacheManager = CacheManager.getInstance();  
  3.   
  4. //从classes目录查找指定名称的配置文件  
  5. //CacheManager cacheManager = CacheManager.create(getClass().getResource("/ehcache.xml"));  
  6.   
  7. //根据配置文件获得Cache实例  
  8. Cache cache = cacheManager.getCache("CACHE1");  
  9.   
  10. //清空Cache中的所有元素  
  11. cache.removeAll();  
  12.   
  13. //往Cache中添加元素  
  14. cache.put(new Element("s1", "11111"));  
  15. cache.put(new Element("s2", "22222"));  
  16. cache.put(new Element("s3", "33333"));  
  17.   
  18. //从Cache中取得元素  
  19. Element e = cache.get("s3");  
  20. System.out.println(e.getValue());  
  21.   
  22. //卸载缓存管理器  
  23. cacheManager.shutdown();  

 

5、页面缓存

     在web.xml文件中配置过滤器。此处对test_tag.jsp页面进行缓存。

Xml代码  收藏代码
  1. <filter>   
  2.     <filter-name>testPageCachingFilter</filter-name>   
  3.     <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter</filter-class>   
  4. </filter>  
  5. <filter-mapping>   
  6.     <filter-name>testPageCachingFilter</filter-name>   
  7.     <url-pattern>/test_tag.jsp</url-pattern>  
  8. </filter-mapping>  

  

    在ehcache.xml文件中配置Cache节点。注意:cache的name属性必需为SimplePageCachingFilter。

Xml代码  收藏代码
  1. <cache name="SimplePageCachingFilter"   
  2.    maxElementsInMemory="10"   
  3.    overflowToDisk="true"   
  4.    eternal="false"   
  5.    timeToIdleSeconds="100"   
  6.    timeToLiveSeconds="100"  
  7.    memoryStoreEvictionPolicy="LFU" />  

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多