Skip to content
Michael Sanford edited this page May 18, 2017 · 6 revisions

CacheFilter is a Java Servlet filter that allows you to enable browser caching for requested resources.

Filter parameters

Option Required Default Since Description
expiration Yes -- 2.2.0 Cache directive to set an expiration time, in seconds, relative to the current date. Used for both Cache-Control and Expires HTTP headers.
private No false 2.0.0 Cache directive to control where the response may be cached.
  • false indicates that the response MAY be cached by any cache.
  • true indicates that the response is intended for a single user and MUST NOT be cached by a shared cache, a private (non-shared) cache MAY cache the response.
must-revalidate No false 2.2.0 Cache directive to define whether conditional requests(1) are required or not for stale responses. When the must-revalidate directive is present in a response received by a cache, that cache MUST NOT use the entry after it becomes stale to respond to a subsequent request without first revalidating it with the origin server.
vary No -- 2.3.0 Cache directive to instruct proxies to cache different versions of the same resource based on specific request-header fields. Some possible Vary header values include:
  • Accept-Encoding instructs proxies to cache two versions of a compressible resource: one compressed, and one uncompressed.
  • Accept-Language instructs proxies to cache different versions of a resource based on the language of a given request.
  • Accept instructs proxies to cache different versions of a resource based on the response format of a given request (e.g. Accept: application/xml or Accept: application/json).
Warn: if your server does vary responses but does not indicate so via the Vary header it may result in cache corruption: Vary Header for RESTful Applications.

(1) If a component is already in the browser's cache and is being re-requested, the browser will pass the Last-Modified date in the request header. This is called a conditional GET request and if the component has not been modified, the server will return a 304 Not Modified response.

Sample configuration

Declare the filter in your web descriptor file web.xml:

<filter>
    <filter-name>imagesCache</filter-name>
    <filter-class>com.samaxes.filter.CacheFilter</filter-class>
    <init-param>
        <param-name>expiration</param-name>
        <param-value>2592000</param-value>
    </init-param>
</filter>

<filter>
    <filter-name>cssCache</filter-name>
    <filter-class>com.samaxes.filter.CacheFilter</filter-class>
    <init-param>
        <param-name>expiration</param-name>
        <param-value>604800</param-value>
    </init-param>
    <init-param>
        <param-name>vary</param-name>
        <param-value>Accept-Encoding</param-value>
    </init-param>
</filter>

<filter>
    <filter-name>jsCache</filter-name>
    <filter-class>com.samaxes.filter.CacheFilter</filter-class>
    <init-param>
        <param-name>expiration</param-name>
        <param-value>216000</param-value>
    </init-param>
    <init-param>
        <param-name>private</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

Map the filter to serve your static resources:

<filter-mapping>
    <filter-name>imagesCache</filter-name>
    <url-pattern>/img/*</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>cssCache</filter-name>
    <url-pattern>*.css</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>jsCache</filter-name>
    <url-pattern>*.js</url-pattern>
</filter-mapping>
Clone this wiki locally