Ehcache 整合Spring 使用页面


【模仿 测试 收藏】

ehcache-web-2.0.4.jar

ehcache-core-2.4.6.jar

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

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


转载于:https://my.oschina.net/xwl1990/blog/159141

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值