zoukankan      html  css  js  c++  java
  • j2ee页面静态化方案encache web cache框架详解1

    web caching

     

    1.介绍

    Ehcache Web 是 EhCache 缓存框架的一个组件,主要用于Java开发Web项目中的一些缓存功能。包括一个单页缓存过滤器:SimplePageCachingFilter;页面压缩(gzip)支持;页面片段缓存等功能。在某些情况下能够很好的提高web应用的性能。

     

    2.simplePageCachingFilter

       能够缓存httpresponse的html,json,xml等输出的完整页面或者页面片段的缓存,也支持gzipping的页面缓存。页面片段缓存可以参考SimplePageFragmentCachingFilter类。

     

    3.keys 缓存的key值

       缓存的key是依赖的查询url和query查询参数串,比如/admin/SomePage.jsp?id=1234&name=Beagle。不依赖于url的域名和端口号,所以对于绑定多个域名的同一台机器也是有效地。但是如果url加了一些为了跟踪用户行为用的序列生成的id号,则无法使用缓存。在这种情况下你也可以重写calculateKey(javax.servlet.http.HttpServletRequest)这个方法来定义自己缓存的key。

     

    4.多线程并发缓存失效问题

       为了避免多线程并发导致cache失效之类的问题,可以通过设置init-param的 blockingTimeoutMillis参数,指定第一个获得锁的线程的超时时间,避免后续请求阻塞。

     

    5.gzipping

      浏览器如果支持Accept-Encoding: gzip,则在缓存中直接取出gzip的response结果,如果浏览器不支持,则cache会通过高效的ungzipped之后把结果输出到response上。

     

    6.caching headers

      SimpleCachingHeadersPageCachingFilter 这个类能够缓存http请求 headers的ETag, Last-Modified 和Expires字段,支持get请求。这样是为了让浏览器快速获取某个页面是否浏览器缓存失效问题。

     

    7.web。xml中可以设置的 init-params
    • cacheName -ehcache.xml 用户设置的filter的cache名称
    • blockingTimeoutMillis - the time, in milliseconds, to wait for the filter chain to return with a response on a cache miss. This is useful to fail fast in the event of an infrastructure failure.
    • varyHeader - set to true to set Vary:Accept-Encoding in the response when doing Gzip. This header is needed to support HTTP proxies however it is off by default.
     8.SimplePageFragmentCacheingFilter

     

    跟simplePageCachingFilter差不多,不过不支持gzip,这样才能对多个页面进行合并。

     

    9.web.xml的配置
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    version="2.5">
    
     <filter>
    <filter-name>CachePage1CachingFilter</filter-name>
    <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter
    </filter-class>
    <init-param>
     <param-name>suppressStackTrace</param-name>
     <param-value>false</param-value>
    </init-param>
    <init-param>
     <param-name>cacheName</param-name>
     <param-value>CachePage1CachingFilter</param-value>
    </init-param>
     </filter>
    
     <filter>
    <filter-name>SimplePageFragmentCachingFilter</filter-name>    <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageFragmentCachingFilter
    </filter-class>
    <init-param>
     <param-name>suppressStackTrace</param-name>
     <param-value>false</param-value>
    </init-param>
    <init-param>
     <param-name>cacheName</param-name>
     <param-value>SimplePageFragmentCachingFilter</param-value>
    </init-param>
     </filter>
    
     <filter>
    <filter-name>SimpleCachingHeadersPageCachingFilter</filter-name>    <filter-class>net.sf.ehcache.constructs.web.filter.SimpleCachingHeadersPageCachingFilter
    </filter-class>
    <init-param>
     <param-name>suppressStackTrace</param-name>
     <param-value>false</param-value>
    </init-param>
    <init-param>
     <param-name>cacheName</param-name>
     <param-value>CachedPage2Cache</param-value>
    </init-param>
     </filter>
    
     <!-- This is a filter chain. They are executed in the order below.
          Do not change the order. -->
    
     <filter-mapping>
    <filter-name>CachePage1CachingFilter</filter-name>
    <url-pattern>/CachedPage.jsp</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>FORWARD</dispatcher>
     </filter-mapping>
    
     <filter-mapping>
    <filter-name>SimplePageFragmentCachingFilter</filter-name>
    <url-pattern>/include/Footer.jsp</url-pattern>
     </filter-mapping>
    
     <filter-mapping>
    <filter-name>SimplePageFragmentCachingFilter</filter-name>
    <url-pattern>/fragment/CachedFragment.jsp</url-pattern>
     </filter-mapping>
    
     <filter-mapping>
    <filter-name>SimpleCachingHeadersPageCachingFilter</filter-name>
    <url-pattern>/CachedPage2.jsp</url-pattern>
     </filter-mapping>

    10.ehcache.xml的配置

    xsi:noNamespaceSchemaLocation="../../main/config/ehcache.xsd">
    <diskStore path="java.io.tmpdir"/>
     <defaultCache
       maxEntriesLocalHeap="10"
       eternal="false"
       timeToIdleSeconds="5"
       timeToLiveSeconds="10"
       overflowToDisk="true"
       />
      <!-- Page and Page Fragment Caches -->
    <cache name="CachePage1CachingFilter"
      maxEntriesLocalHeap="10"
      eternal="false"
      timeToIdleSeconds="10000"
      timeToLiveSeconds="10000"
      overflowToDisk="true">
    </cache>
    <cache name="CachedPage2Cache"
      maxEntriesLocalHeap="10"
      eternal="false"
      timeToLiveSeconds="3600"
      overflowToDisk="true">
    </cache>
    <cache name="SimplePageFragmentCachingFilter"
      maxEntriesLocalHeap="10"
      eternal="false"
      timeToIdleSeconds="10000"
      timeToLiveSeconds="10000"
      overflowToDisk="true">
    </cache>
    <cache name="SimpleCachingHeadersTimeoutPageCachingFilter"
      maxEntriesLocalHeap="10"
      eternal="false"
      timeToIdleSeconds="10000"
      timeToLiveSeconds="10000"
      overflowToDisk="true">
    </cache>
    </ehcache>

    11.caching filter的异常

    FilterNonReentrantException 当同一个线程再次重入caching filter处理时抛出异常,因为当第一个请求还未block时,同一个线程再次进入该filter就会block

    ResponseHeadersNotModifiableException类似FilterNonReentrantException 

    AlreadyGzippedException 如果已经对一个页面进行gzip处理,再次gzip时就抛出该异常

    ResponseHeadersNotModifiableException如果对页面进行gzip处理,那么需要重新设置setheader的值,如果在设置过程中出错了,则抛出该异常。

     

    参考:http://www.ehcache.org/documentation/user-guide/web-caching

     

    对源码实现欢迎参考 http://zhwj184.iteye.com/blog/1545157


  • 相关阅读:
    为IIS站点启用SSL加密
    SQL Server Analysis Service身份验证
    安装规划服务器(PPS 2007)
    用SQL Server Compact Edition创建移动应用程序 【转载】
    在Web Service中使用Windows验证的方式
    巧用Excel去除数据表中的重复行
    如何动态切换报表中的图表类型
    使用链接维度
    如何配置订阅以使用 Web 同步(RMO 编程)【转载】
    如何对数据进行合并及分组
  • 原文地址:https://www.cnblogs.com/secbook/p/2655166.html
Copyright © 2011-2022 走看看