2010-04-14 - Jakarta Taglibs has been retired.

For more information, please explore the Attic.

Jakarta Project: Cache Taglib

Version: 1.0

Table of Contents

Overview

The Cache Taglib lets you cache fragments of your JSP pages. In a large web application where performance is important, caching might be appropriate at many levels within your architecture. Caching fragments of JSP pages is the option that's simultaneously the most flexible and the most tedious (that is, the most noticeable to JSP page authors). Using the Cache Taglib, you can mark individual sections of your JSP page as cacheable.

Currently, the default cache lifetime is 5 minutes, and the default cache size is 64k. These are both configurable by back-end Java developers using the CacheUtil class included with this library.

Caching is a discipline whose details are probably best left unexposed to page authors in large environments. Therefore, the Cache Taglib's interface has been designed to be as simple as possible. The tag attributes let page authors "factor out" the dynamic nature of a page fragment into a simple key; fragments with the same key have interchangeable content. For example, in a fragment whose content depends only on who the current user is, ${user} might be a good key. Keys can be formed from more complex expressions too. For instance, if a fragment's content depends on the current user and the user's chosen verbosity level, you might use an attribute like key="${user}.${verbosity}"

Note that the Cache Taglib currently uses the expression language (EL) from Beta1 of the JSTL reference implementation.

Requirements

This custom tag library requires no software other than a servlet container that supports the JavaServer Pages (JSP) Specification, version 1.1 or higher.

Configuration

Follow these steps to configure your web application with this tag library:

To use the tags from this library in your JSP pages, add the following directive at the top of each page:

<%@ taglib uri="http://jakarta.apache.org/taglibs/cache-1.0" prefix="cache" %>

where "cache" is the tag name prefix you wish to use for tags from this library. You can change this value to any prefix you like.

Tag Summary

Cache Tags
cacheCaches a fragment of a JSP page.
invalidateCaches a fragment of a JSP page.
 

Tag Reference

cache Availability: 1.0

Caches a fragment of a JSP page.

Tag Bodyjsp    
Restrictions

None

AttributesNameRequired Runtime Expression Evaluation Availability
 scope  No   No  1.0
 

The scope (or extent) of the cache: page, request, session, or application. For instance, page-scoped caches let you cache a value to be reused in a single JSP page. The default scope is application.

 name  No   No  1.0
 

The identifier of the cache to use. Each cache's size and status are independent of the others. You may use an unlimited number of caches, but keep in mind that 10 different 64k caches require a total of 640k of memory. You may leave out the cache attribute, in which case a cache with the default name ("") is used.

 key  No   No  1.0
 

The key under which to index the cached value. Tags that use the same key within the same cache should have bodies that are interchangeable.

VariablesNone
Examples Caches the user's birthday.  
 


            

		<cache:cache scope="application"
                   name="birthdays" key="${user}">
		   ... code that retrieves the user's birthday ...
                </cache:cache>
	    
          

invalidate Availability: 1.0

Invalidates a cache entry or a whole cache.

Tag Bodyjsp    
Restrictions

None

AttributesNameRequired Runtime Expression Evaluation Availability
 scope  No   No  1.0
 

The scope (or extent) of the cache: page, request, session, or application. For instance, page-scoped caches let you cache a value to be reused in a single JSP page. The default scope is application.

 name  No   No  1.0
 

The identifier of the cache on which to operate. Note: if the key attribute is not specified, or if it evalutes to null, then the entire cache is destroyed.

 key  No   No  1.0
 

The identifier for the particular cache entry to remove.

VariablesNone
Examples Invalidates a cache entry  
 


            

		<cache:invalidate scope="application"
                   name="birthdays" key="${user}" />
	    
          

Examples

See the example application cache-examples.war for examples of the usage of the tags from this custom tag library.

Java Docs

Java programmers can view the java class documentation for this tag library as javadocs.

Revision History

Review the complete revision history of this tag library.