希望能帮助到你 2012-05-30
web caching
Ehcache Web 是 EhCache 缓存框架的一个组件,主要用于Java开发Web项目中的一些缓存功能。包括一个单页缓存过滤器:SimplePageCachingFilter;页面压缩(gzip)支持;页面片段缓存等功能。在某些情况下能够很好的提高web应用的性能。
能够缓存httpresponse的html,json,xml等输出的完整页面或者页面片段的缓存,也支持gzipping的页面缓存。页面片段缓存可以参考SimplePageFragmentCachingFilter类。
缓存的key是依赖的查询url和query查询参数串,比如/admin/SomePage.jsp?id=1234&name=Beagle。不依赖于url的域名和端口号,所以对于绑定多个域名的同一台机器也是有效地。但是如果url加了一些为了跟踪用户行为用的序列生成的id号,则无法使用缓存。在这种情况下你也可以重写calculateKey(javax.servlet.http.HttpServletRequest)这个方法来定义自己缓存的key。
为了避免多线程并发导致cache失效之类的问题,可以通过设置init-param的 blockingTimeoutMillis
参数,指定第一个获得锁的线程的超时时间,避免后续请求阻塞。
浏览器如果支持Accept-Encoding: gzip,则在缓存中直接取出gzip的response结果,如果浏览器不支持,则cache会通过高效的ungzipped之后把结果输出到response上。
SimpleCachingHeadersPageCachingFilter
这个类能够缓存http请求 headers的ETag, Last-Modified 和Expires字段,支持get请求。这样是为了让浏览器快速获取某个页面是否浏览器缓存失效问题。
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.跟simplePageCachingFilter差不多,不过不支持gzip,这样才能对多个页面进行合并。
<web-app xmlns=http://java.sun.com/xml/ns/javaee
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的配置
<Ehcache xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
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>
参考:http://www.ehcache.org/documentation/user-guide/web-caching