一、hibernate缓存简介
二、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的相关配置中添加如下:
3、需要在映射文件*.hbm.xml中
4、在src根目录下加入ehcache.xml文件,具体内容如下:
maxElementsOnDisk="1000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="5"
timeToLiveSeconds="30"
memoryStoreEvictionPolicy="LFU"/>
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskSpoolBufferSizeMB="30"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"/>
5、web.xml中加入以下配置:
5、写一个Filter,继承SimplePageCachingFilter,如下:
package net.cnki.tpi.cms.util; import java.util.Enumeration; import javax.servlet.FilterChain; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.ehcache.CacheException; import net.sf.ehcache.constructs.blocking.LockTimeoutException; import net.sf.ehcache.constructs.web.AlreadyCommittedException; import net.sf.ehcache.constructs.web.AlreadyGzippedException; import net.sf.ehcache.constructs.web.filter.FilterNonReentrantException; import net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter; import org.apache.log4j.Logger; public class PageCacheFilter extends SimplePageCachingFilter { private final static Logger log = Logger.getLogger(PageCacheFilter.class); private final static String NOT_CACHE_URL_LIST = "notCacheUrlList"; private static String[] notCacheURLs; private void init() throws CacheException { String notCacheUrlList = filterConfig.getInitParameter(NOT_CACHE_URL_LIST); if(!MyStringUtil.isNullOrEmpty(notCacheUrlList)){ notCacheURLs = notCacheUrlList.split(","); } } @Override protected void doFilter(final HttpServletRequest request,final HttpServletResponse response, final FilterChain chain)throws AlreadyGzippedException, AlreadyCommittedException,FilterNonReentrantException, LockTimeoutException, Exception { if (notCacheURLs == null) { init(); } String request_url = request.getRequestURI(); boolean flag = false; if (notCacheURLs != null && notCacheURLs.length > 0) { for (String notCacheURL : notCacheURLs) { if (request_url.contains(notCacheURL.trim())) { flag = true; break; } } } //如果请求的url为不需要缓存的,则执行正常页面转向;否则缓存该页面 if (flag) { chain.doFilter(request, response); }else{ String query = request.getQueryString(); if (query != null) { query = "?" + query; } log.info("当前请求被缓存:" + request_url + query); super.doFilter(request, response, chain); } } @SuppressWarnings("unchecked") private boolean headerContains(final HttpServletRequest request, final String header, final String value) { logRequestHeaders(request); final Enumeration accepted = request.getHeaders(header); while (accepted.hasMoreElements()) { final String headerValue = (String) accepted.nextElement(); if (headerValue.indexOf(value) != -1) { return true; } } return false; } @Override protected boolean acceptsGzipEncoding(HttpServletRequest request) { boolean ie6 = headerContains(request, "User-Agent", "MSIE 6.0"); boolean ie7 = headerContains(request, "User-Agent", "MSIE 7.0"); return acceptsEncoding(request, "gzip") || ie6 || ie7; } }