1 /**
2 * Copyright 2003-2007 Greg Luck
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package net.sf.ehcache.constructs.web.filter;
18
19 import net.sf.ehcache.CacheManager;
20
21 import javax.servlet.http.HttpServletRequest;
22
23 /**
24 * A simple page fragment {@link CachingFilter} suitable for most uses.
25 * <p/>
26 * It uses a Singleton CacheManager created with the default factory method. Override to use a different CacheManager
27 * <p/>
28 * The meaning of <i>page fragment</i> is:
29 * <ul>
30 * <li>An include into an outer page.
31 * <li>A content type suitable for suitable for inclusion into the outer page. e.g. text or text/html
32 * </ul>
33 * For full page see {@link SimplePageCachingFilter}.
34 * <h3>Keys</h3>
35 * Pages are cached based on their key. The key for this cache is the URI followed by the query string. An example
36 * is <code>/admin/SomePage.jsp?id=1234&name=Beagle</code>.
37 * <p/>
38 * This key technique is suitable for a wide range of uses. It is independent of hostname and port number, so will
39 * work well in situations where there are multiple domains which get the same content, or where users access
40 * based on different port numbers.
41 * <p/>
42 * A problem can occur with tracking software, where unique ids are inserted into request query strings. Because
43 * each request generates a unique key, there will never be a cache hit. For these situations it is better to
44 * parse the request parameters and override {@link #calculateKey(javax.servlet.http.HttpServletRequest)} with
45 * an implementation that takes account of only the significant ones.
46 * <h3>Configuring Caching with ehcache</h3>
47 * A cache entry in ehcache.xml should be configured with the name {@link #NAME}.
48 * <p/>
49 * Cache attributes including expiry are configured per cache name. To specify a different behaviour simply
50 * subclass, specify a new name and create a separate cache entry for it.
51 * <h3>Gzipping</h3>
52 * Page fragments should never be gzipped.
53 * <p/>
54 * Page fragments are stored in the cache ungzipped.
55 *
56 * @author Greg Luck
57 * @version $Id: SimplePageFragmentCachingFilter.java 512 2007-07-10 09:18:45Z gregluck $
58 */
59 public class SimplePageFragmentCachingFilter extends PageFragmentCachingFilter {
60
61 /**
62 * This filter's name
63 */
64 public static final String NAME = "SimplePageFragmentCachingFilter";
65
66
67 /**
68 * CachingFilter works off a key.
69 * <p/>
70 * This test implementation has a single key.
71 *
72 * @param httpRequest
73 * @return the key, generally the URL plus request parameters
74 */
75 protected String calculateKey(HttpServletRequest httpRequest) {
76 StringBuffer stringBuffer = new StringBuffer();
77 stringBuffer.append(httpRequest.getRequestURI()).append(httpRequest.getQueryString());
78 String key = stringBuffer.toString();
79 return key;
80 }
81
82 /**
83 * Gets the CacheManager for this CachingFilter. It is therefore up to subclasses what CacheManager to use.
84 * <p/>
85 * This method was introduced in ehcache 1.2.1. Older versions used a singleton CacheManager instance created with
86 * the default factory method.
87 *
88 * @return the CacheManager to be used
89 * @since 1.2.1
90 */
91 protected CacheManager getCacheManager() {
92 return CacheManager.getInstance();
93 }
94
95 /**
96 * Returns the name of the cache to use for this filter.
97 */
98 protected String getCacheName() {
99 return NAME;
100 }
101 }
102