博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ehcache web cahce 缓存改良版
阅读量:6822 次
发布时间:2019-06-26

本文共 3318 字,大约阅读时间需要 11 分钟。

  hot3.png

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 = (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"
            />

转载于:https://my.oschina.net/farces/blog/294055

你可能感兴趣的文章
(转)前端面试题
查看>>
进销存管理系统——可行性分析
查看>>
git指南
查看>>
SQL-Delete Duplicate Emails
查看>>
前端页面与Nodejs使用websocket通信
查看>>
BeanUtils组件的使用
查看>>
js添加菜单栏之后停留在那里
查看>>
iOS NSLog各种打印
查看>>
硬盘知识总结和一块硬盘在正式使用前,需要经历的3个步骤
查看>>
SQL - 语法收集
查看>>
PHP echo() 函数
查看>>
Arrays.sort学习(jdk7)
查看>>
Fragment的使用
查看>>
快速排序
查看>>
程序员面试题100题第09题——查找链表中倒数第K个结点
查看>>
132. Palindrome Partitioning II
查看>>
WCF批量打开服务
查看>>
Python脚本运行出现语法错误:IndentationError:unexpected indent
查看>>
ios开发--KVO浅析
查看>>
【Android】6.3 ProgressDialog
查看>>