Configuring CacheFilter in WebLogic

At times there could be scenarios where you  might want to cache the attributes and results from standard requests and responses to enhance the performace of your application by using the Dynamic Content Caching feature.

It can be achieved by two ways.

1. Using the CacheFilter Servlet Filter

2. Using the wl:cache custom tag.

It is always always recommended to cache the contents that change infrequently such as headers, footers etc. You can also cache the repetitive database queries which change infrequently.

CacheFilter:

—————–

WebLogic Server includes a filter called CacheFilter that provides page-level response caching for webapplications. This filter operates at the complete page level rather than surrounding and caching only a section of JSP content in a page.

Steps to configure the CacheFilter servlet.


1. Define the cachefilter servlet in the application deployment descriptor i.e. web.xml:


It is registered as any other servlet filters. It is defined as weblogic.cache.filter.CacheFilter in the <filter-class> attribute. Specify the <url-pattern> of the page or pages to cache.
A sample entry looks like below

 

<filter>

<filter-name>CacheFilter1</filter-name>

<filter-class>weblogic.cache.filter.CacheFilter</filter-class>

<init-param>

<param-name>timeout</param-name>

<param-value>60</param-value>

</init-param>

</filter>

...

<filter-mapping>

<filter-name>CacheFilter1</filter-name>

<url-pattern>CacheFilterTest1.jsp</url-pattern>

</filter-mapping>

 

Note:   The CacheFilterTest1.jsp page will execute the first time the URL is accessed by any client, and the content of the HTTP response will be cached by the filter and used for all subsequent access requests for60 seconds.
2. Define the cachefilter properties.

Use initialization parameters in the filter registration to define timeout criteria and other cache control values. For example, to cache theresponse from a specific JSP page for 60 seconds, register the CacheFilter using elements similar tothe following:

 

<init-param>

<param-name>timeout</param-name>

<param-value>60</param-value>

</init-param>

 

Note:  By default cached entries never timeout. max-cache-size attribute can be used to specify the maximum size of an element that can be cached, by default MAX_CACHED_SIZE size the size of the cache.

A sample web.xml would look like below.

 

<?xml version='1.0' encoding='UTF-8'?>

<web-app>

<display-name>TestApplication</display-name>

<welcome-file-list>

<welcome-file>CacheFilterTest1.jsp</welcome-file>

</welcome-file-list>

<filter>

<filter-name>CacheFilter1</filter-name>

<filter-class>weblogic.cache.filter.CacheFilter</filter-class>

<init-param>

<param-name>timeout</param-name>

<param-value>60</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>CacheFilter1</filter-name>

<url-pattern>CacheFilterTest1.jsp</url-pattern>

</filter-mapping>

</web-app>

 

For further reading you can refer the below link.

http://download.oracle.com/docs/cd/E13222_01/wls/docs92/javadocs/weblogic/cache/filter/CacheFilter.html

There is an alterative solution for this caching by using the wl:cache element. However the CacheFilter may also be used with servlets and static content,unlike the related wl:cache custom tag, which works only in JSP pages.

http://download.oracle.com/docs/cd/E13222_01/wls/docs103/webapp/customtags.html#wp56944

Cheers,

Wonders Team. 🙂