首页 > 代码库 > Ehcache web cahce 缓存改良版
Ehcache web cahce 缓存改良版
java 代码:
package net.sf.ehcache.constructs.web.filter;
import java.util.Enumeration;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
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;
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, ",");
}
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 > 0) {
for(String cacheURL : cacheURLs) {
if (url.contains(cacheURL.trim())) {
flag = true;
break;
}
}
}
if(!flag)
{
//压缩所有js与css文件及所有静态页
if(url != null && (url.endsWith(".js") || url.endsWith(".css") || url.endsWith(".html") || url.endsWith(".htm")))
{
flag = true;
}
}
// 若是包含我们要缓存的url 就缓存该页面,不然履行正常的页面转向
if (flag) {
String query = request.getQueryString();//初始化要缓存的
if (query != null){
query = "?" + query;
log.info("请求被存放缓存:" + url + query);
}else{
log.info("请求被存放缓存:" + url);
}
super.doFilter(request,response, chain);
} else {
chain.doFilter(request, response);
}
}
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 = http://www.mamicode.com/(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)
* <b>function:</b> 兼容ie6/7 gzip紧缩
*/
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;
}
}
web.xml配置:
<filter>
<filter-name>PageEhCacheFilter</filter-name>
<filter-class>net.sf.ehcache.constructs.web.filter.PageEhCacheFilter</filter-class>
<init-param>
<param-name>patterns</param-name>
<!-- 设置要缓存的url -->
<param-value>/index.jsp,/index_top.jsp,/index_foot.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PageEhCacheFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
ehcache.xml 配置:
<cache name="SimplePageCachingFilter"
maxElementsInMemory="100"
maxElementsOnDisk="10000"
eternal="false"
overflowToDisk="false"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="10"
timeToLiveSeconds="10"
memoryStoreEvictionPolicy="LFU"
/>