---
title: "ehcache缓存页面(2)-扩展SimplePageCachingFilter "
author: "lastsoul"
date: "2015-09-25T17:32:17Z"
tags: []
categories: ["编程语言"]
slug: "ehcache-2-simplepagecachingfilter"
draft: false
lastmod: "2015-09-25T17:33:40Z"
id: 2246232
url: "https://lastsoul.iteye.com/blog/2246232"
---

页面缓存主要用Filter过滤器对请求的url进行过滤，如果该url在缓存中出现。那么页面数据就从缓存对象中获取，并以gzip压缩后返回。其速度是没有压缩缓存时速度的3-5倍，效率相当之高！其中页面缓存的过滤器有CachingFilter，一般要扩展filter或是自定义Filter都继承该CachingFilter。 CachingFilter功能可以对HTTP响应的内容进行缓存。这种方式缓存数据的粒度比较粗，例如缓存整张页面。它的优点是使用简单、效率高，缺点是不够灵活，可重用程度不高。 EHCache使用SimplePageCachingFilter类实现Filter缓存。该类继承自CachingFilter，有默认产生cache key的calculateKey()方法，该方法使用HTTP请求的URI和查询条件来组成key。也可以自己实现一个Filter，同样继承CachingFilter类,然后覆写calculateKey()方法，生成自定义的key。 CachingFilter输出的数据会根据浏览器发送的Accept-Encoding头信息进行Gzip压缩。 在使用Gzip压缩时，需注意两个问题： 1. Filter在进行Gzip压缩时，采用系统默认编码，对于使用GBK编码的中文网页来说，需要将操作系统的语言设置为：zh\_CN.GBK，否则会出现乱码的问题。 2. 默认情况下CachingFilter会根据浏览器发送的请求头部所包含的Accept-Encoding参数值来判断是否进行Gzip压缩。虽然IE6/7浏览器是支持Gzip压缩的，但是在发送请求的时候却不带该参数。为了对IE6/7也能进行Gzip压缩，可以通过继承CachingFilter，实现自己的Filter，然后在具体的实现中覆写方法acceptsGzipEncoding。 具体实现参考： 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; } 在ehcache.xml中加入如下配置 \[code="java"] \[/code] filter代码： \[code="java"]package com.xx.cache.ehcache.filter; 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.commons.lang.StringUtils; import org.apache.log4j.Logger; public class PageEhCacheFilter extends SimplePageCachingFilter { private final static Logger log = Logger.getLogger(PageEhCacheFilter.class); private final static String FILTER\_URL\_PATTERNS = "patterns"; private static String\[] cacheURLs; private void init() throws CacheException { String patterns = filterConfig.getInitParameter(FILTER\_URL\_PATTERNS); cacheURLs = StringUtils.split(patterns, ","); } @Override protected void doFilter(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) throws AlreadyGzippedException, AlreadyCommittedException, FilterNonReentrantException, LockTimeoutException, Exception { if (cacheURLs == null) { init(); } String url = request.getRequestURI(); boolean flag = false; if (cacheURLs != null && cacheURLs.length &gt; 0) { for (String cacheURL : cacheURLs) { if (url.contains(cacheURL.trim())) { flag = true; break; } } } // 如果包含我们要缓存的url 就缓存该页面，否则执行正常的页面转向 if (flag) { String query = request.getQueryString(); if (query != null) { query = "?" + query; } log.info("当前请求被缓存：" + url + query); super.doFilter(request, response, chain); } else { chain.doFilter(request, response); } } @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; } /\** * @see net.sf.ehcache.constructs.web.filter.Filter#acceptsGzipEncoding(javax.servlet.http.HttpServletRequest) * **function:** 兼容ie6/7 gzip压缩 * @author hoojo * @createDate 2012-7-4 上午11:07:11 \*/ @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; } } \[/code] web.xml \[code="java"] contextConfigLocation classpath:spring-\*.xml PageEhCacheFilter com.temobi.ehcache.filter.PageEhCacheFilter patterns /cache.jsp, product.action, market.action PageEhCacheFilter \*.action PageEhCacheFilter \*.jsp org.springframework.web.context.ContextLoaderListener \[/code] 当第一次请求这些页面后，这些页面就会被添加到缓存中，以后请求这些页面将会从缓存中获取。你可以在cache.jsp页面中用小脚本来测试该页面是否被缓存。&lt;%=new Date()%&gt;如果时间是变动的，则表示该页面没有被缓存或是缓存已经过期，否则则是在缓存状态了。 cache.jsp代码 \[code="java"]&lt;%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%&gt; &lt;%@page import="java.util.Date"%&gt; Insert title here &lt;%=new Date()%&gt; hello ecache!! \[/code]