Oracle ADF Faces Cache Tag Library

An Oracle Technical Document
May 2005

Index

Introduction

The Oracle ADF Faces Cache tag library enables fragment caching inside a J2EE container, such as Oracle Container for Java (OC4J) or Tomcat. As a component of the Oracle ADF Faces, the Oracle ADF Faces Cache tag library works with any implementation of the JavaServer Faces JSF 1.1 specification (or later).

The Oracle ADF Faces Cache library enables caching of the presentation layer of any JSF based applications in memory and thus avoids content regeneration by the J2EE container. The results are an increase in throughput and a reduction on the total response time, especially for responses that require formatting of results from database queries. Because the Oracle ADF Faces Cache operates within the J2EE container, pages that are generally not cacheable, such as those require authentication, in an external cache would become cacheable. This is particularly beneficial for financial applications, HR applications, or any business applications that have complex application logic.

To use the Oracle ADF Faces Cache tag library, add the library into your desired J2EE container. As you build your application, simply mark fragments with a beginning <afc:cache> and ending </afc:cache> tag.

How Caching Works

The execution of a page containing fragments is as follows:

1. The browser client requests a page containing fragments delineated with <afc:cache> tag. A page can contain one or more cacheable fragments.

2. The application server receives the request. In the J2EE container, the JSF view processor looks for the fragment in the cache. If the fragment is not present or invalid, JSF view processor generates fragments responses and stores a copy of the responses in the cache.

3. The application server returns the responses from the JSF view processor to the browser client.

4. When another browser client requests for the same fragment next time, the JSF view processor retrieves the fragment from the memory and assembles the page with the cached fragment content.

<afc:cache> Tag

The <afc:cache> tag is for specifying the caching policy of page fragments. For maximum caching and thus optimized performance results, use the <afc:cache> tag for page fragments that can be reused multiple times. Content that change frequently may still be suitable for caching if there are many requests for it.

The general syntax of <afc:cache> is as follows:

                                 
<afc:cache [duration="
                                  
seconds"]
[varyBy="
                                  
varyBy_param.
                                  
varyBy_param_value [
                                  
varyBy_param.
                                  
varyBy_param_value...]"]
[searchKeys="
                                  
searchKey [
                                  
searchKey...]"]
[cacheMissOnRePost="true|false"]>
                                   
cacheable content
</afc:cache>
                                
                              

To set cache properties for an entire page fragment, make the <afc:cache> tag a direct child of the <f:view> tag, and enclose the page's content within it:

                                 
                                   
page.jsp
<f:view>
  <afc:cache ...>
   
                                  
cacheable page content
  </afc:cache>
</f:view>
                                
                              

To set cache properties for fragments contained within a main page, enclose each fragment within a beginning <afc:cache> tag and ending </afc:cache> tag:

                                 
                                   
page.jsp
<f:view>
  ...
  <afc:cache ...>
   
                                  
cacheable fragment content
  </afc:cache>
  ...
  <afc:cache ...>
   
                                  
cacheable fragment content
  </afc:cache>
  ...
</f:view>
                                
                              

To set cache properties for a fragment that exists in its own subpage and is included (<jsp:include>) from the main page, make the <afc:cache> tag a direct child of the <f:subview> tag, and enclose the fragment's content within it. In this manner, you can share a cacheable fragment among different pages.

                                 
                                   
page.jsp
<f:view>
  ...
  <afc:cache ...>
   
                                  
cacheable fragment content
  </afc:cache>  
                                  
 <jsp:include page="/fragment.jsp" />
  <afc:cache ...>
   
                                  
cacheable fragment content
  </afc:cache>
  ...
</f:view>


                                   
fragment.jsp
<f:subview>
  <afc:cache ...>
   
                                  
cacheable fragment content
  </afc:cache>
</f:subview>
                                
                              

The <afc:cache> tag contains four attributes.

Invalidation

Content invalidation takes place as part of the application logic. In Oracle ADF Faces Cache, the invalidation request is based on the search keys of the objects. You can associate a single search key with many objects, or associate multiple search keys with a single object. The search key is to describe the relationship between pages or fragments. For example, the following assigns a search key of business_news to a page:

                                 
<f:view>
  <afc:cache searchKeys="business_news">
   
                                  
page content
  </afc:cache>
</f:view>
                                
                              

To invalidate content, determine which seach keys to assign to objects, then run the invalidation code to submit an invalidation request. The invalidation code resembles the following:

                                 
SearchKeySelector selector = new SearchKeySelector ("search_key_name"),
Invalidator.invalidate(selector);
try
{
   
                                  
/* Wait for the completion of the invalidation. The maximum time to wait is 5 seconds. */
  OperationResults result=selector.getResult(5000);
   
                                  
/* Do whatever you want with the result. */
  System.err.println("# of documents invalidated = " + result.getDocumentCount());
}
catch (TimeoutException ex)
{
  
                                  
/* Deal with time out. */
 System.err.println("Timed out.");
}
                                
                              

Download Developer Preview and Example

Download the developer preview to see the Oracle ADF Faces Cache tag library in action. This zip file also contains a sample application. JDeveloper 10g (10.1.3) is the recommended IDE.

Summary

The Oracle ADF Faces Cache tag library enables fragment caching of the JavaServer Faces pages within the J2EE container. By simply inserting the <afc:cache> tag inside the JSF view tag, application developers can optimize application performance by incorporating caching. A developer preview of the Oracle ADF Faces Cache tag library is available now.