OSCache - CacheFilter

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. Beginning with release 2.4 you are be able to set/override the CacheFilter initialization parameters at runtime. #### 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. If the ICacheKeyProvider parameter isn't set, the CacheFilter will use the HTTP request URI and the QueryString to create the cache key. You can change the 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! Since 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: max-age (NEW! Since 2.3.1) Specifies the maximum amount of time in seconds that the cache content will be considered new in the browser's cache. The browser will retrieve the content from it's own cache for the amount of time without requesting the web server again. The default max-age time is 60 seconds. Combined with the last modified header the transaction overhead and server load is reduced excellently which speed ups the server response time. Further parameters are no init for don't initializing the max-age cache control and time to set max-age based on the time parameter and creation time of the content (expiration timestamp minus current timestamp) by each request. #### 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 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. #### Parameter: disableCacheOnMethods (New! Since 2.4) Specify HTTP method names in a comma separated list for which cacheing should be disabled. The default value is <code>null</code> for cacheing all requests without regarding the method name. See HttpServletRequest#getMethod, e.g.:
    <init-param>
            <param-name>disableCacheOnMethods</param-name>
            <param-value>POST,PUT,DELETE</param-value>
        </init-param>
#### Parameter: oscache-properties-file (New! Since 2.4) By specifying a OSCache properties file for a CacheFilter, the developer can run multiple caches each with different configurations tailored to the requirements of the application. In each properties file the developer has to define a unique cache.key otherwise the default properties file is used. If the parameter is not specified, the default properties file will be used. The file has to be put into the classpath, e.g. WEB-INF/classes.