Moss

Java Components

 

Cache Filter

The Caching Filter is a JSP filter designed to cache requests for better performance. It is especially suited to caching pages which require much processing time and / or memory usage, such as converted images.

How to Use this Component

Dependencies

This filter relies on the following libraries:

Installation

To use this filter, you must include the .jar file (plus any dependencies) in the web application's classpath (either in WEB-INF/lib or in the server's lib folder). You must then add the following code to the web.xml file:

<filter>
 <filter-name>CacheFilter</filter-name>
 <filter-class>org.homeunix.thecave.moss.jsp.cache.CacheFilter</filter-class>
</filter>

<filter-mapping>
 <filter-name>CacheFilter</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>

You should place this filter definition before any resources you want to be cached.

Configuration

This module looks to the config parameter 'config' to specify the location of the configuration XML file, relative to /WEB-INF. If the config parameter is not set, it defaults to 'cache.xml'.

The cache.xml file specifies which persistence backings to use (both disk and memory backings are included in the cache module), what URLs to cache, and how long to cache them for. A sample configuration file is included below: <?xml version="1.0" encoding="UTF-8"?>
<cache>
  <persistence-backing class-name="org.homeunix.thecave.moss.jsp.cache.persistence.impl.MemoryPersistence">
    <parameter name="item-count" value="2"/>
  </persistence-backing>
  <persistence-backing class-name="org.homeunix.thecave.moss.jsp.cache.persistence.impl.DiskPersistence">
   <parameter name="cache-path" value="/var/cache/moss"/>
  </persistence-backing>
  
  <cache-mapping pattern=".*images/.*" expiryTime="60000"/>
  <cache-mapping pattern=".*\.jsp" expiryTime="3600"/>
  <cache-mapping pattern=".*\.css" expiryTime="60"/>  
</cache>
The cache mapping patterns are regular expressions, matched against the request URL. The patterns are matched in the order they appear in the file. The persistence backing parameters are both optional; the memory persistence defaults to 32, and the disk persistence defaults to '/tmp/ca.digitalcave.moss.jsp.cache'.