【模仿 测试 收藏】
ehcache-web-2.0.4.jar
ehcache-core-2.4.6.jar
1
package com.common;
import java.io.UnsupportedEncodingException;
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;
/**
* Page EhCache Filter
* @createDate 2013-9-5
*
*/
public class PageEhCacheFilter extends SimplePageCachingFilter {
private Logger logger = Logger.getLogger(PageEhCacheFilter.class);
private final static String FILTER_URL_PATTERNS = "patterns";
private final static String FILTER_URL_METHOD = "method";
private static String ACTION;
private static String METHOD;
/**
* <b>function</b>
* <br/>init get web.xml config parameter
*
* @throws CacheException
*/
private void init() throws CacheException {
ACTION = filterConfig.getInitParameter(FILTER_URL_PATTERNS);
METHOD = ACTION.split(",")[1];
ACTION = ACTION.split(",")[0];
}
/**
* <b>function</b>
* <br/>Request filter action,method url save cache
*
* @throws AlreadyGzippedException, AlreadyCommittedException,
* FilterNonReentrantException, LockTimeoutException, Exception
*/
@Override
protected void doFilter(final HttpServletRequest request,final HttpServletResponse response,
final FilterChain chain) throws AlreadyGzippedException, AlreadyCommittedException,
FilterNonReentrantException, LockTimeoutException, Exception {
boolean flag = false;
String url = null, method = null;
request.setCharacterEncoding("GBK");
if (ACTION == null || METHOD == null) {
init();
}
if (ACTION == null && METHOD != null) {
url = request.getRequestURI();
if (url.contains(ACTION)) {
method = request.getParameter(FILTER_URL_METHOD);
if (METHOD.equals(method)) {
flag = true;
}
}
}
if (flag) {
String query = request.getQueryString();
if (query != null) {
query = "?" + query;
}
logger.info("Request Ehcache:" + url + query);
super.doFilter(request, response, chain);
} else {
chain.doFilter(request, response);
}
}
/**
* @param request
* @param header
* @param value
* @return
*/
@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;
}
/**
* <b>function:</b> Compatibility ie8/9 gzip
* @see net.sf.ehcache.constructs.web.filter.Filter#acceptsGzipEncoding(javax.servlet.http.HttpServletRequest)
*/
@Override
protected boolean acceptsGzipEncoding(HttpServletRequest request) {
try {
request.setCharacterEncoding("GBK");
} catch (UnsupportedEncodingException e) {
logger.warn("request set character encoding GBK error.", e);
}
boolean ie8 = headerContains(request, "User-Agent", "MSIE 8.0");
boolean ie9 = headerContains(request, "User-Agent", "MSIE 9.0");
return acceptsEncoding(request, "gzip") || ie8 || ie9;
}
}
2 ehcache.xml
<?xml version="1.0" encoding="gbk"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStore path="/data/ehcache" />
<defaultCache maxElementsInMemory="10000" eternal="false"
overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600"
diskPersistent="false" diskExpiryThreadIntervalSeconds="1200" />
<cache name="SimplePageCachingFilter"
maxElementsInMemory="1000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU" />
</ehcache>
3 web.xml filter
<filter>
<filter-name>PageEhCacheFilter</filter-name>
<filter-class>com.common.PageEhCacheFilter</filter-class>
<init-param>
<param-name>patterns</param-name>
<param-value>institution.do,ruleLeft</param-value>
</init-param>
</filter>
<!-- 过滤条件映射 -->
<filter-mapping>
<filter-name>PageEhCacheFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
4
5
6
7