hibernate二级缓存 Ehcache配置详解

来源:互联网 发布:美柚如何恢复数据 编辑:程序博客网 时间:2024/05/16 14:17

一、hibernate缓存简介

一级缓存(session):内部缓存
事务范围:缓存只能被当前事务访问。缓存的生命周期依赖于事务的生命周期,当事务结束时,缓存也就结束生命周期

二级缓存(sessionFactory):

缓存被应用范围内的所有事务共享。 这些事务有可能是并发访问缓存,因此必须对缓存进行更新。缓存的生命周期依赖于应用的生命周期,应用结束时, 缓存也就结束了生命周期,二级缓存存在于应用范围。集群范围:在集群环境中,缓存被一个机器或者多个机器的进程共享。缓存中的数据被复制到集群环境中的每个进程节点,进程间通过远程通信来保证缓存中的数据的一致性, 缓存中的数据通常采用对象的松散数据形式,二级缓存也存在与应用范围。

注意:对大多数应用来说,应该慎重地考虑是否需要使用集群范围的缓存,再加上集群范围还有数据同步的问题,所以应当慎用。多种范围的缓存处理过程持久化层可以提供多种范围的缓存。如果在事务范围的缓存中没有查到相应的数据,还可以到应用范围或集群范围的缓存内查询,如果还是没有查到,那么只有到数据库中查询了。

使用二级缓存的原则:
◆数据不会被第三方修改
◆同一数据系统经常引用
◆数据大小在可接受范围之内
◆关键数据或不会被并发更新的数据

二、EhCache简介

EHCache 是一个非常轻量级的缓存实现,是一个纯Java的进程内缓存框架,而且从1.2 之后就支持了集群,是Hibernate中默认的CacheProvider。

具有快速、精干等特点,Ehcache可以直接使用。

也可以和Hibernate对象/关系框架结合使用。可以将对象、数据、jsp、Servlet进行缓存。

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


三、配置

1、首先到官网下载ehcache-core.jar、ehcache-web.jar最新版本,然后加入所在工程的lib中

Ehcache 对象、数据缓存:http://ehcache.org/downloads/destination?name=ehcache-core-2.5.2-distribution.tar.gz&bucket=tcdistributions&file=ehcache-core-2.5.2-distribution.tar.gz

Web页面缓存:http://ehcache.org/downloads/destination?name=ehcache-web-2.0.4-distribution.tar.gz&bucket=tcdistributions&file=ehcache-web-2.0.4-distribution.tar.gz

2、在hibernate的相关配置中添加如下:

               <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                <prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>

3、需要在映射文件*.hbm.xml中<class name="" table="" > 节点下添加如下:

     <!-- 缓存策略 -->
     <cache usage="read-write"/>

4、在src根目录下加入ehcache.xml文件,具体内容如下:

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

 <!-- 数据缓存存放目录 -->
  <diskStore path="/jcms_cache_data/ehcache"/>
  <!-- 
   页面缓存
   三种缓存算法:LRU-最近最少使用、LFU-较少频率使用和FIFO-先进先出。

  参数详解:

   simplePageCachingFilter 缓存的名称
   maxElementsInMemory 缓存中元素的最大数量
   maxElementsOnDisk 持久化到硬盘的缓存元素的最大数量
   eternal="false"  如果为true,表示对象永远不会过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false;
   overflowToDisk="true" 当缓存中元素数量超过限制时,将这些元素持久化到硬盘,为false时,设置没意义。
   timeToIdleSeconds 多长时间不访问缓存,那么就清除该缓存
   timeToLiveSeconds 缓存的存活时间
   -->
  <cache name="SimplePageCachingFilter"  
         maxElementsInMemory="10000"  
         maxElementsOnDisk="1000"  
         eternal="false"  
         overflowToDisk="true"  
         timeToIdleSeconds="5"  
         timeToLiveSeconds="30"  
         memoryStoreEvictionPolicy="LFU"/> 
  
  <!-- Ehcache 对象、数据缓存用以下配置 -->
  <defaultCache  maxElementsInMemory="10000" 
        eternal="false" 
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        diskSpoolBufferSizeMB="30" 
        maxElementsOnDisk="10000000" 
        diskPersistent="false" 
        diskExpiryThreadIntervalSeconds="120"/>
</ehcache>

5、web.xml中加入以下配置:

<!-- Ehcache页面缓存配置 -->
   <filter>  
     <filter-name>PageCacheFilter</filter-name>  
        <filter-class>net.cnki.tpi.cms.util.PageCacheFilter</filter-class>  
        <!-- 初始化参数为无需缓存的URL,多个以逗号分隔 -->
        <init-param>
         <param-name>notCacheUrlList</param-name>
         <param-value>/jcms/pcons/getUserManager.action</param-value>
        </init-param>
   </filter>  
  <filter-mapping>  
     <filter-name>PageCacheFilter</filter-name>  
     <url-pattern>/*</url-pattern>  
  </filter-mapping>

5、写一个Filter,继承SimplePageCachingFilter,如下:

[java] view plaincopy
  1.    
  2.   
  3. package net.cnki.tpi.cms.util;  
  4. import java.util.Enumeration;  
  5.   
  6. import javax.servlet.FilterChain;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9.   
  10. import net.sf.ehcache.CacheException;  
  11. import net.sf.ehcache.constructs.blocking.LockTimeoutException;  
  12. import net.sf.ehcache.constructs.web.AlreadyCommittedException;  
  13. import net.sf.ehcache.constructs.web.AlreadyGzippedException;  
  14. import net.sf.ehcache.constructs.web.filter.FilterNonReentrantException;  
  15. import net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter;  
  16.   
  17. import org.apache.log4j.Logger;  
  18.   
  19. public class PageCacheFilter extends SimplePageCachingFilter {     
  20.     private final static Logger log = Logger.getLogger(PageCacheFilter.class);         
  21.     private final static String NOT_CACHE_URL_LIST = "notCacheUrlList";    
  22.     private static String[] notCacheURLs;  
  23.       
  24.     private void init() throws CacheException {  
  25.          String notCacheUrlList = filterConfig.getInitParameter(NOT_CACHE_URL_LIST);  
  26.          if(!MyStringUtil.isNullOrEmpty(notCacheUrlList)){  
  27.              notCacheURLs = notCacheUrlList.split(",");      
  28.          }  
  29.     }  
  30.       
  31.      @Override     
  32.      protected void doFilter(final HttpServletRequest request,final HttpServletResponse response, final FilterChain chain)throws AlreadyGzippedException, AlreadyCommittedException,FilterNonReentrantException, LockTimeoutException, Exception  
  33.      {  
  34.          if (notCacheURLs == null) {   
  35.              init();  
  36.          }  
  37.          String request_url = request.getRequestURI();       
  38.          boolean flag = false;  
  39.            
  40.          if (notCacheURLs != null && notCacheURLs.length > 0) {       
  41.              for (String notCacheURL : notCacheURLs) {            
  42.                  if (request_url.contains(notCacheURL.trim())) {            
  43.                      flag = true;             
  44.                      break;               
  45.                  }            
  46.              }        
  47.          }  
  48.          //如果请求的url为不需要缓存的,则执行正常页面转向;否则缓存该页面  
  49.          if (flag) {           
  50.              chain.doFilter(request, response);    
  51.         }else{            
  52.              String query = request.getQueryString();    
  53.              if (query != null) {          
  54.                  query = "?" + query;            
  55.              }              
  56.              log.info("当前请求被缓存:" + request_url + query);         
  57.              super.doFilter(request, response, chain);  
  58.         }  
  59.      }  
  60.        
  61.      @SuppressWarnings("unchecked")  
  62.       private boolean headerContains(final HttpServletRequest request, final String header, final String value) {  
  63.          logRequestHeaders(request);         
  64.          final Enumeration accepted = request.getHeaders(header);   
  65.          while (accepted.hasMoreElements()) {            
  66.              final String headerValue = (String) accepted.nextElement();     
  67.              if (headerValue.indexOf(value) != -1) {               
  68.                  return true;            
  69.                  }        
  70.              }          
  71.          return false;     
  72.      }  
  73.        
  74.      @Override      
  75.      protected boolean acceptsGzipEncoding(HttpServletRequest request) {    
  76.          boolean ie6 = headerContains(request, "User-Agent""MSIE 6.0");     
  77.          boolean ie7 = headerContains(request, "User-Agent""MSIE 7.0");   
  78.          return acceptsEncoding(request, "gzip") || ie6 || ie7;      
  79.      }  
  80. }  
  81.       

 







0 0
原创粉丝点击