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

Ehcache web cahce 缓存改良版

时间: 2024-08-13 20:05:49

Ehcache web cahce 缓存改良版的相关文章

ehcache 页面整体缓存和局部缓存

页面缓存是否有必要?. 这样说吧,几乎所有的网站的首页都是访问率最高的,而首页上的数据来源又是非常广泛的,大多数来自不同的对象,而且有可能来自不同的db ,所以给首页做缓存是很必要的.那么主页的缓存策略应该怎样设计呢?我认为应该是某个固定时间之内不变的,比如说2分钟更新一次.那么这个缓存应该做在什么地方呢?让我们来看一下,当前我们的的应用的结构一般是是page-filter-action-service-dao-db ,这个过程中的- 的地方都是可以做缓存的地方,根据页面缓存的特征,应该把页面缓

基于Ehcache的Spring缓存详解

一 简介 缓存,通过将数据保存在缓冲区中,可以为以后的相同请求提供更快速的查询,同时可以避免方法的多次执行,从而提高应用的性能. 在企业级应用中,为了提升性能,Spring提供了一种可以在方法级别上进行缓存的缓存抽象.通过使用AOP原则,Spring对使用缓存的方法自动生成相应代理类,如果已经为提供的参数执行过该方法,那么就不必重新执行实际方法而是直接返回被缓存的结果.在基于Spring的Web应用中,为了启用缓存功能,需要使用缓存注解对待使用缓存的方法进行标记. Spring缓存仅仅提供了一种

WEB端缓存机制

什么是WEB缓存 Web缓存是指一个Web资源(如html页面,图片,js,数据等)存在于Web服务器和客户端(浏览器)之间的副本.缓存会根据进来的请求保存输出内容的副本:当下一个请求来到的时候,如果是相同的URL,缓存会根据缓存机制决定是直接使用副本响应访问请求,还是向源服务器再次发送请求.比较常见的就是浏览器会缓存访问过网站的网页,当再次访问这个URL地址的时候,如果网页没有更新,就不会再次下载网页,而是直接使用本地缓存的网页.只有当网站明确标识资源已经更新,浏览器才会再次下载网页 数据库数

windows全系列激活脚本-改良版

@ECHO OFFTITLE Windows 全版本系统激活cscript //nologo %Systemroot%\system32\slmgr.vbs -skms 10.1.1.12ECHO 检测激活...cscript //nologo %Systemroot%\system32\slmgr.vbs -atocscript //nologo %Systemroot%\system32\slmgr.vbs -dlvPAUSE 新建文本文档将上面的代码复制过去保存为windows全系列激活脚

office全系列激活脚本-改良版

@ECHO OFFTITLE office 全版本系统激活@echo offfor /l %%a in (8,1,16) do (for /f "tokens=*" %%i in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Office\%%a.0\Word\Options" /v "PROGRAMDIR"') do (if not "%%i"==""

mybatis(4)_二级缓存深入_使用第三方ehcache配置二级缓存

增删改对二级缓存的影响 1.增删改也会清空二级缓存 2.对于二级缓存的清空实质上是对value清空为null,key依然存在,并非将Entry<k,v>删除 3.从DB中进行select查询的条件是: 1.缓存中根本不存在这个key 2.存在key对应的Entry,但是value为null 二级缓存的配置 <cache eviction="FIFO" flushInterval="60000" size="512" readOn

Uva 11468 改良版AC自动机

改良版AC自动机 UVa 11468 题意:给一些字符和各自出现的概率,在其中随机选择L次,形成长度为L的字符串S,给定K个模板串,求S不包含任意一个串的概率. 首先介绍改良版的AC自动机: 传统的AC自动机,是当一个字符失配时,根据失配函数转移到指定地方,而这个失配函数,是通过一个宽搜的过程形成的,这时在匹配串的时候,就当匹配失败时,顺着失配指针走,直到可以匹配.然后匹配到单词结点,或者后缀链接是一个单词结点,这些前面的结点也是匹配单词.这就是传统的AC自动机. 现在将这个AC自动机改版优化:

[Asp.net web api]缓存

摘要 为了提高接口的性能,我们常做的优化就包括缓存,对经常访问但变化不大的数据进行缓存.或者使用http的缓存,减少请求的次数. web api缓存 在提供的api,我们也可以实现缓存,来减少访问的次数.有个比较好的组件,供我们使用. 通过Nuget安装WebApi.OutputCache.V2开源组件,项目地址:https://github.com/filipw/Strathweb.CacheOutput 使用 [RoutePrefix("api/user")] public cla

Hibernate+EhCache配置二级缓存

步骤: 第一步:加入ehcache.jar 第二步: 在src目录下新建一个文件,名为:ehcache.xml 第三步:在hibernate配置文件的<session-factory>下配置 配置的具体信息: ehcache.xml的具体配置: <?xml version="1.0" encoding="UTF-8"?> <!-- maxEntriesLocalHeap: 在内存中缓存的element的最大数目. maxEntriesL