Source

oscache / docs / filter.html

Full commit
<html>
<head>
<title>Caching Content with the CacheFilter</title>
</head>

<body bgcolor="#FFFFFF">

<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, eg PDF files or images.</p>

<p>To configure the filter, add something like the following to your web.xml 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):

<pre><code>
   &lt;filter&gt;
      &lt;filter-name&gt;CacheFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;com.opensymphony.oscache.web.filter.CacheFilter&lt;/filter-class&gt;
   &lt;/filter&gt;
   &lt;filter-mapping&gt;
      &lt;filter-name&gt;CacheFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;*.jsp&lt;/url-pattern&gt;
   &lt;/filter-mapping&gt;
</code></pre>

<p>The default duration is one hour and the default scope for the cache is application scope. You
can change these settings using initialization parameters. The <code>time</code> parameter sets
the cache time (in seconds), while the <code>scope</code> parameter lets you set the scope. Valid
values for the scope are <code>application</code> and <code>session</code>.</p>

<p>As an example, the following settings would cache content for 10 minutes in session scope:
<pre><code>
   &lt;filter&gt;
      &lt;filter-name&gt;CacheFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;com.opensymphony.oscache.web.filter.CacheFilter&lt;/filter-class&gt;
      &lt;init-param&gt;
         &lt;param-name&gt;time&lt;/param-name&gt;
         &lt;param-value&gt;600&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
         &lt;param-name&gt;scope&lt;/param-name&gt;
         &lt;param-value&gt;session&lt;/param-value&gt;
      &lt;/init-param&gt;
   &lt;/filter&gt;
</code></pre></p>

<p>Note that the filter will only cache content that has a status of 200 (<code>HttpServletResponse.SC_OK</code>).</p>
</body>
</html>