Source

oscache / docs / wiki / CacheFilter Tutorial.html

Full commit
<html>
    <head>
        <title>OSCache - 
        CacheFilter Tutorial
         </title>
	    <link rel="stylesheet" href="styles/site.css" type="text/css" />
        <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>

    <body>
	    <table class="pagecontent" border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#ffffff">
		    <tr>
			    <td valign="top" class="pagebody">
				    <h3><a name="CacheFilterTutorial-Introduction">Introduction </a></h3>

<p><b>OSCache</b> comes with a servlet filter that enables you to transparently cache entire pages of your website, and even binary files. Caching of binary files is extremely useful when they are generated dynamically, e.g. PDF files or images. In addition by using the last modified header the transaction overhead and server load is reduced excellently which speed ups the server response time.</p>

<p>How to configure OSCache to cache entire servlet responses is described in the <a href="CacheFilter.html" title="CacheFilter">configuration page of the CacheFilter</a>.</p>

<p>Besides bugs being fixed in the 2.2 release, major improvements have been made to the CacheFilter in many ways:</p>

<ul>
	<li>Default initialization of the last modified header which reduces transaction overhead and server load</li>
	<li>Preserving more http headers, e.g. the expires header</li>
	<li>Special handling for fragments of a page</li>
	<li>Custom cache key generation by subclassing CacheFilter or by implementing a special interface</li>
	<li>Custom cache groups generation by subclassing CacheFilter or by implementing a special interface</li>
	<li>Support of GZip filters in the filter chain</li>
	<li>Avoids session creation for application scope pages</li>
	<li>Multiple matching cache filters won't dead-lock the response anymore</li>
	<li>The cache won't be serve the same response twice before the client begins to cache it anymore</li>
</ul>


<p>The upcoming 2.3 release will include further improvements to the CacheFilter:</p>

<ul>
	<li>CRON expressions to expire content at specific dates and/or times</li>
	<li>Reduced memory consumption</li>
</ul>


<h3><a name="CacheFilterTutorial-CacheableContent">Cacheable Content</a></h3>

<div class="information-block" align='center'><div class='informationMacroPadding'><table cellpadding='5' width='85%' cellspacing='0' class='noteMacro' border='0'><tr><td width='16' valign='top'><img src="/images/icons/emoticons/warning.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b class="strong">Cacheable content</b><br />
<p>Note that the filter will only cache content that has a status of 200 (HttpServletResponse.SC_OK).</p></td></tr></table></div></div>

<h3><a name="CacheFilterTutorial-Configuringthefilter">Configuring the filter</a></h3>

<h4><a name="CacheFilterTutorial-Example1">Example 1</a></h4>

<p>To configure the filter, add something like the following to your <tt>web.xml</tt> file (obviously you will want to set the URL pattern to match only the content you want to cache; this example will cache all JSP pages for 10 minutes in session scope):</p>

<div class="code"><div class="codeContent">
<pre class="code-xml"><span class="code-tag">&lt;filter&gt;</span>
    <span class="code-tag">&lt;filter-name&gt;</span>CacheFilter<span class="code-tag">&lt;/filter-name&gt;</span>
    <span class="code-tag">&lt;filter-class&gt;</span>com.opensymphony.oscache.web.filter.CacheFilter<span class="code-tag">&lt;/filter-class&gt;</span>
    <span class="code-tag">&lt;init-param&gt;</span>
        <span class="code-tag">&lt;param-name&gt;</span>time<span class="code-tag">&lt;/param-name&gt;</span>
        <span class="code-tag">&lt;param-value&gt;</span>600<span class="code-tag">&lt;/param-value&gt;</span>
    <span class="code-tag">&lt;/init-param&gt;</span>
    <span class="code-tag">&lt;init-param&gt;</span>
        <span class="code-tag">&lt;param-name&gt;</span>scope<span class="code-tag">&lt;/param-name&gt;</span>
        <span class="code-tag">&lt;param-value&gt;</span>session<span class="code-tag">&lt;/param-value&gt;</span>
    <span class="code-tag">&lt;/init-param&gt;</span>
<span class="code-tag">&lt;/filter&gt;</span>

<span class="code-tag">&lt;filter-mapping&gt;</span>
    <span class="code-tag">&lt;filter-name&gt;</span>CacheFilter<span class="code-tag">&lt;/filter-name&gt;</span>
    <span class="code-tag">&lt;url-pattern&gt;</span>*.jsp<span class="code-tag">&lt;/url-pattern&gt;</span>
<span class="code-tag">&lt;/filter-mapping&gt;</span></pre>
</div></div>

<p>The default duration is one hour and the default scope for the cache is application scope. You can change these settings using <a href="CacheFilter.html" title="CacheFilter">initialization parameters</a>.</p>

<h4><a name="CacheFilterTutorial-Example2">Example 2</a></h4>

<p>The initialization of the last modified header based on the current time reduces transaction overhead and server load, because the browser can ask the server if the cached content in the browser cache was changed on the server since the last request. If the content wasn't changed , the server will response with the status 304 (not modified).</p>

<p>Furthermore if the <a href="CacheFilter.html#CacheFilter-expires" title="expires on CacheFilter">expires parameter</a> is the set to <em>time</em>, the server will send the date and time after which the content is considered stale. Then common browsers won't request the server anymore until the cached content is considered stale. The example will cache the content for one hour by default and the expires date and time will be calculated based on the creation time and the <em>time</em> parameter (default is one hour).</p>

<div class="code"><div class="codeContent">
<pre class="code-xml"><span class="code-tag">&lt;filter&gt;</span>
    <span class="code-tag">&lt;filter-name&gt;</span>CacheFilterStaticContent<span class="code-tag">&lt;/filter-name&gt;</span>
    <span class="code-tag">&lt;filter-class&gt;</span>com.opensymphony.oscache.web.filter.CacheFilter<span class="code-tag">&lt;/filter-class&gt;</span>
    <span class="code-tag">&lt;init-param&gt;</span>
        <span class="code-tag">&lt;param-name&gt;</span>expires<span class="code-tag">&lt;/param-name&gt;</span>
        <span class="code-tag">&lt;param-value&gt;</span>time<span class="code-tag">&lt;/param-value&gt;</span>
    <span class="code-tag">&lt;/init-param&gt;</span>
<span class="code-tag">&lt;/filter&gt;</span>

<span class="code-tag">&lt;filter-mapping&gt;</span>
    <span class="code-tag">&lt;filter-name&gt;</span>CacheFilterStaticContent<span class="code-tag">&lt;/filter-name&gt;</span>
    <span class="code-tag">&lt;url-pattern&gt;</span>*.jsp<span class="code-tag">&lt;/url-pattern&gt;</span>
<span class="code-tag">&lt;/filter-mapping&gt;</span></pre>
</div></div>

                    			    </td>
		    </tr>
	    </table>
    </body>
</html>