net.sf.ehcache.constructs.web.filter
Class SimplePageCachingFilter
java.lang.Object
net.sf.ehcache.constructs.web.filter.Filter
net.sf.ehcache.constructs.web.filter.CachingFilter
net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter
- All Implemented Interfaces:
- javax.servlet.Filter
public class SimplePageCachingFilter
- extends CachingFilter
A simple page CachingFilter
suitable for most uses.
It uses a Singleton CacheManager created with the default factory method. Override to use a different CacheManager
The meaning of page is:
- A complete response i.e. not a fragment.
- A content type suitable for gzipping. e.g. text or text/html
For jsp:included page fragments see SimplePageFragmentCachingFilter
.
Keys
Pages are cached based on their key. The key for this cache is the URI followed by the query string. An example
is /admin/SomePage.jsp?id=1234&name=Beagle
.
This key technique is suitable for a wide range of uses. It is independent of hostname and port number, so will
work well in situations where there are multiple domains which get the same content, or where users access
based on different port numbers.
A problem can occur with tracking software, where unique ids are inserted into request query strings. Because
each request generates a unique key, there will never be a cache hit. For these situations it is better to
parse the request parameters and override calculateKey(javax.servlet.http.HttpServletRequest)
with
an implementation that takes account of only the significant ones.
Configuring Caching with ehcache
A cache entry in ehcache.xml should be configured with the name NAME
.
Cache attributes including expiry are configured per cache name. To specify a different behaviour simply
subclass, specify a new name and create a separate cache entry for it.
Gzipping
Significant network efficiencies can be gained by gzipping responses.
Whether a response can be gzipped depends on:
- Whether the user agent can accept GZIP encoding. This feature is part of HTTP1.1.
If a browser accepts GZIP encoding it will advertise this by including in its HTTP header:
All common browsers except IE 5.2 on Macintosh are capable of accepting gzip encoding. Most search engine
robots do not accept gzip encoding.
- Whether the user agent has advertised its acceptance of gzip encoding. This is on a per request basis. If they
will accept a gzip response to their request they must include the following in the HTTP request header:
Accept-Encoding: gzip
Responses are automatically gzipped and stored that way in the cache. For requests which do not accept gzip
encoding the page is retrieved from the cache, ungzipped and returned to the user agent. The ungzipping is
high performance.
- Version:
- $Id: SimplePageCachingFilter.java 512 2007-07-10 09:18:45Z gregluck $
- Author:
- Greg Luck
Field Summary |
static java.lang.String |
NAME
The name of the filter. |
Method Summary |
protected java.lang.String |
calculateKey(javax.servlet.http.HttpServletRequest httpRequest)
Pages are cached based on their key. |
protected CacheManager |
getCacheManager()
Gets the CacheManager for this CachingFilter. |
protected java.lang.String |
getCacheName()
A meaningful name representative of the JSP page being cached. |
Methods inherited from class net.sf.ehcache.constructs.web.filter.CachingFilter |
buildPage, buildPageInfo, checkNoReentry, doDestroy, doFilter, doInit, setContentType, setCookies, setHeaders, setStatus, writeContent, writeResponse |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
NAME
public static final java.lang.String NAME
- The name of the filter. This should match a cache name in ehcache.xml
- See Also:
- Constant Field Values
SimplePageCachingFilter
public SimplePageCachingFilter()
getCacheName
protected java.lang.String getCacheName()
- A meaningful name representative of the JSP page being cached.
The name must match the name of a configured cache in ehcache.xml
- Specified by:
getCacheName
in class CachingFilter
- Returns:
- the name of the cache to use for this filter.
getCacheManager
protected CacheManager getCacheManager()
- Gets the CacheManager for this CachingFilter. It is therefore up to subclasses what CacheManager to use.
This method was introduced in ehcache 1.2.1. Older versions used a singleton CacheManager instance created with
the default factory method.
- Specified by:
getCacheManager
in class CachingFilter
- Returns:
- the CacheManager to be used
- Since:
- 1.2.1
calculateKey
protected java.lang.String calculateKey(javax.servlet.http.HttpServletRequest httpRequest)
- Pages are cached based on their key. The key for this cache is the URI followed by the query string. An example
is
/admin/SomePage.jsp?id=1234&name=Beagle
.
This key technique is suitable for a wide range of uses. It is independent of hostname and port number, so will
work well in situations where there are multiple domains which get the same content, or where users access
based on different port numbers.
A problem can occur with tracking software, where unique ids are inserted into request query strings. Because
each request generates a unique key, there will never be a cache hit. For these situations it is better to
parse the request parameters and override calculateKey(javax.servlet.http.HttpServletRequest)
with
an implementation that takes account of only the significant ones.
The key should be unique
- Specified by:
calculateKey
in class CachingFilter
- Parameters:
httpRequest
-
- Returns:
- the key, generally the URI plus request parameters
Copyright © 2003-2011. All Rights Reserved.