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, e.g. PDF files or images.

A tutorial describes how to cache entire pages of your website and what performance improvements can be done with the CacheFilter.

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, use the oscache.properties to configure the core settings of OSCache and add something like the following to your web.xml file:

<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>

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.

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

Parameter: time

The time parameter sets the cache time (in seconds) for the content. The default cache time is one hour.

Specifying -1 (indefinite expiry) as the cache time will ensure a content does not become stale until it is either explicitly flushed or the expires refresh policy causes the entry to expire.

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: cron (New in upcoming release 2.3)

A cron expression that determines when the page content will expire. This allows content to be expired at particular dates and/or times, rather than once a cache entry reaches a certain age. See Cron Expressions to read more about this attribute. Please consider that the (default) time value is still evaluated, hence the time value should be set to indefinite expiry.

Parameter: fragment (NEW! Since 2.2)

Defines if the filter handles fragments of a page. Acceptable values are auto for auto detect, no for false and yes 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)

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

Parameter: lastModified (NEW! Since 2.2)

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 (default) the last modified information will be set based on current time.

Parameter: expires (NEW! Since 2.2)

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)

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)

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.

Parameter: EntryRefreshPolicy (New! Since 2.3)

Specify a class which implements the interface EntryRefreshPolicy. A developer can implement a class which provides a different custom cache invalidation policy for a specific cache entry. If not specified, the default policy is timed entry expiry as specified with the time parameter described above.