OSCache 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. In addition by using the last modified header the transaction overhead and server load is reduced excellently which speed ups the server response time.

Besides bugs being fixed in the upcoming 2.2 release, major improvements have been made to the CacheFilter in many ways:

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

Cacheable Content

Cacheable content

Note that the filter will only cache content that has a status of 200 (HttpServletResponse.SC_OK).

Configuring the filter

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 for 10 minutes in session scope):

<filter>
    <filter-name>CacheFilter</filter-name>
    <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
    <init-param>
        <param-name>time</param-name>
        <param-value>600</param-value>
    </init-param>
    <init-param>
        <param-name>scope</param-name>
        <param-value>session</param-value>
    </init-param>
</filter>

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

The default duration is one hour and the default scope for the cache is application scope. You can change these settings using initialization parameters.

Parameter: time

The time parameter sets the cache time (in seconds) for the content.

Parameter: scope

The scope parameter lets you set the scope to cache content in. Valid values for the scope are application (default) and session.

Parameter: fragment (NEW! Since 2.2 RC)

Defines if the filter handles fragments of a page. Acceptable values are auto (-1 in 2.2 RC) for auto detect, no (0 in 2.2 RC) for false and yes (1 in 2.2 RC) for true. The default value is auto detect which checks the javax.servlet.include.request_uri request attribute. Fragments of a page shouldn't be gzipped or evaluate the last modified header.

Parameter: nocache (NEW! Since 2.2 RC)

Defines which objects shouldn't be cached. Acceptable values are off (default) for caching all objects and sessionIdInURL if the session id is contained in the URL.

Parameter: lastModified (NEW! Since 2.2 final)

Defines if the last modified header will be sent in the response. Acceptable values are off for don't sending the header, even it is set in the filter chain, on for sending it if it is set in the filter chain and initial the last modified information will be set based on current time.

Parameter: expires (NEW! Since 2.2 final)

Defines if the expires header will be sent in the response. Acceptable values are off for don't sending the header, even it is set in the filter chain, on (default) for sending it if it is set in the filter chain and time the expires information will be intialized based on the time parameter and creation time of the content.

Value time

The last parameter time would force the CacheFilter to send the expires header, because the value is set always. The developer must consider that some browsers evaluate the value and will use the cached content in the browsers cache, until the content is expired. Consequently a flush of the cache in the web application won't update a page in the browser cache. Hence different users may see see a different status of page.

Parameter: ICacheKeyProvider (NEW! Since 2.2 RC)

Specify a class which implements the interface ICacheKeyProvider. A developer can implement a class which provides cache keys based on the request, the servlect cache administrator and the cache.

Parameter: ICacheGroupsProvider (NEW! Since 2.2 final)

Specify a class which implements the interface ICacheGroupsProvider. A developer can implement a class which provides cache groups based on the request, the servlect cache administrator and the cache.