您的位置:首页 > 其它

Ehcache的简单介绍和使用

2017-04-15 09:54 316 查看
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的缓存实现

   

 

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

<?xml version="1.0" encoding="UTF-8"?>

<ehcache>

<!--timeToIdleSeconds 当缓存闲置n秒后销毁 -->

<!--timeToLiveSeconds 当缓存存活n秒后销毁 -->

<!--

缓存配置

       name:缓存名称。

       maxElementsInMemory:缓存最大个数。

       eternal:对象是否永久有效,一但设置了,timeout将不起作用。

       timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。

       timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。

       overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。

       diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。

       maxElementsOnDisk:硬盘最大缓存个数。

       diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.

       diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。

       memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。

       clearOnFlush:内存数量最大时是否清除。
-->

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

     1)RMI集群模式

          A、手工发现

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

 

 <cacheManagerPeerProviderFactory   
      class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"   
      properties="peerDiscovery=manual,  
      rmiUrls=//192.168.0.12:4567/oschina_cache|//192.168.0.13:4567/oschina_cache"  />
          B、自动发现

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

  <cacheManagerPeerProviderFactory  
     class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"  
     properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,  
     multicastGroupPort=4446, timeToLive=32" />

 

           需要在每个cache属性中加入

         

<cacheEventListenerFactory
<cache name="demoCache"  
    maxElementsInMemory="10000"  
    eternal="true"  
    overflowToDisk="true">  
    <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>  
</cache>
4、通过编程方式使用EhCache

Java代码 

  //从classes目录查找ehcache.xml配置文件 

  CacheManager cacheManager = CacheManager.getInstance();  
  
//从classes目录查找指定名称的配置文件  
//CacheManager cacheManager = CacheManager.create(getClass().getResource("/ehcache.xml"));  
//根据配置文件获得Cache实例  
Cache cache = cacheManager.getCache("CACHE1");  
  
//清空Cache中的所有元素  
cache.removeAll();  
//往Cache中添加元素  
cache.put(new Element("s1", "11111"));  
cache.put(new Element("s2", "22222"));  
cache.put(new Element("s3", "33333"));  
  
//从Cache中取得元素  
Element e = cache.get("s3");  
System.out.println(e.getValue());  
  
//卸载缓存管理器  
cacheManager.shutdown();  

 

5、页面缓存

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

<filter>   
    <filter-name>testPageCachingFilter</filter-name>   
    <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter</filter-class>   
</filter>  
<filter-mapping>   
    <filter-name>testPageCachingFilter</filter-name>   
    <url-pattern>/test_tag.jsp</url-pattern>  
</filter-mapping>

  

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

  <cache name="SimplePageCachingFilter"   
    maxElementsInMemory="10"   
    overflowToDisk="true"   
    eternal="false"   
    timeToIdleSeconds="100"   
    timeToLiveSeconds="100"  
    memoryStoreEvictionPolicy="LFU" />  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ehcache 缓存技术