IntroductionOSCache 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. In addition by using the last modified header the transaction overhead and server load is reduced excellently which speed ups the server response time. How to configure OSCache to cache entire servlet responses is described in the configuration page of the CacheFilter. Besides bugs being fixed in the 2.2 release, major improvements have been made to the CacheFilter in many ways:
The upcoming 2.3 release will include further improvements to the CacheFilter:
Cacheable Content
Configuring the filterExample 1To 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. Example 2The initialization of the last modified header based on the current time reduces transaction overhead and server load, because the browser can ask the server if the cached content in the browser cache was changed on the server since the last request. If the content wasn't changed , the server will response with the status 304 (not modified). Furthermore if the expires parameter is the set to time, the server will send the date and time after which the content is considered stale. Then common browsers won't request the server anymore until the cached content is considered stale. The example will cache the content for one hour by default and the expires date and time will be calculated based on the creation time and the time parameter (default is one hour). <filter> <filter-name>CacheFilterStaticContent</filter-name> <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class> <init-param> <param-name>expires</param-name> <param-value>time</param-value> </init-param> </filter> <filter-mapping> <filter-name>CacheFilterStaticContent</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> |