Chinaunix首页 | 论坛 | 博客
  • 博客访问: 724126
  • 博文数量: 225
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2722
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-03 17:32
文章分类

全部博文(225)

文章存档

2019年(7)

2018年(16)

2017年(1)

2016年(26)

2015年(41)

2014年(15)

2013年(119)

我的朋友

分类: Java

2014-01-23 11:16:56

1.保存对象和属性

    1.1 添加jar包

        maven方式:
            pom.xml添加:
            2.4.3
            2.0.4
            
                 net.sf.ehcache
                  ehcache-core
                  ${ehcache-core.version}
         

           
      net.sf.ehcache
      ehcache-web 
      ${ehcache-web.version}
   

     普通方式:
       下载相应jar包,ehcache-core.jar,ehcache-web.jar放到WEB/lib目录下面。

    1.2 spring配置文件添加bean。

         xmlns:xsi="" xmlns:p=""
xmlns:context=""
xmlns:cache=""
xmlns:mvc=""
xmlns:aop=""
xsi:schemaLocation=" 
    /spring-beans-3.1.xsd   
        
       /spring-aop-3.1.xsd   
       
          
        
       /spring-context-3.1.xsd
         
       /spring-mvc-3.1.xsd
        
  /spring-cache.xsd       
       ">

          
           
            classpath:ehcache.xml    
       
  
   
 
        
          
            
     
    
          
          userCache     
     
    
   
   

    1.3 添加ehcache配置文件

        在上面指定路径中添加文件,ehcache.xml,内容如下。
          
            maxElementsInMemory="1000"  
        eternal="false"  
        timeToIdleSeconds="120"  
        timeToLiveSeconds="120"  
        overflowToDisk="false"  
        />  
          maxElementsInMemory="10000"  
        eternal="false"  
        timeToIdleSeconds="300000"  
        timeToLiveSeconds="600000"  
        overflowToDisk="false"  
        />  
            maxElementsInMemory="10000" 
        eternal="false"
        overflowToDisk="false" 
        timeToIdleSeconds="5" 
        timeToLiveSeconds="10"
        memoryStoreEvictionPolicy="LFU" />
 

    1.4 测试使用

        public class TestEhCache {
        public static void main(String[] args) {
            ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring-servlet.xml");    
            Cache ehCache = (Cache) ctx.getBean("ehCache");
            List list = new ArrayList();
            list.add("123456");
            Element element = new Element("key", list);
            ehCache.put(element);
            System.out.println(((List)ehCache.get("key").getValue()).get(0));
            ehCache.remove("key");
            ehCache.removeAll();
            System.out.println(("xxx"+ehCache.get("key")));
}
}

2.页面静态化

    2.1 在ehcache.xml文件添加

                 maxElementsInMemory="10000" 
        eternal="false"
        overflowToDisk="false" 
        timeToIdleSeconds="5" 
        timeToLiveSeconds="10"
        memoryStoreEvictionPolicy="LFU" />

    2.2 添加filter类

        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, ",");
    }
    
    @Override
    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();
        //struts里面需要改动,如果用的springMVC则不需要添加。
//        String method = request.getParameter("method");
//        if(method!=null&&!"".equals(method))
//        {
//         url = url+"?act="+method;
//        }
        boolean flag = false;
        if (cacheURLs != null && cacheURLs.length > 0) {
            for (String cacheURL : cacheURLs) {
            System.out.println(cacheURL);
                if (url.contains(cacheURL.trim())) {
                    flag = true;
                    break;
                }
            }
        }
        // 如果包含我们要缓存的url 就缓存该页面,否则执行正常的页面转向
        if (flag) {
            String query = request.getQueryString();
            if (query != null) {
                query = "?" + query;
            }
            log.info("当前请求被缓存:" + url + query);
            super.doFilter(request, response, chain);
        } else {
            chain.doFilter(request, response);
        }
    }
    
    @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;
    }
    
    /**
     * @see net.sf.ehcache.constructs.web.filter.Filter#acceptsGzipEncoding(javax.servlet.http.HttpServletRequest)
     * function: 兼容ie6/7 gzip压缩
     * @author hoojo
     * @createDate 2012-7-4 上午11:07:11
     */
    @Override
    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;
    }
}

    2.3 web.xml配置filter拦截规则

        PageEhCacheFilter
    com.mvc.filter.PageEhCacheFilter
   
        patterns
       
        publicArticle.jsp,article_publicArticle.do
   

   

   PageEhCacheFilter
   *.do


   PageEhCacheFilter
   *.jsp

    2.4 测试

        在publicArticle.jsp页面里面添加 <%=new Date()%>刷新页面,如果页面时间没有变化说明配置成功。

        
阅读(2314) | 评论(0) | 转发(1) |
0

上一篇:php页面静态化

下一篇:git常用命令

给主人留下些什么吧!~~