Using the servlet Java cache  Class CacheManager

Chapter 5: Web Application Tuning

Using partial page caching

Partial page caching allows you to cache parts of a response. This mechanism is useful when pages contain volatile content, such as calculation results, but otherwise have static content such as headers and footers. Partial page caching is supported by a tag library for use in JSPs, and a public API for use in servlets.


Using the caching tag library

The tag library implementation is provided in CacheTags.jar, installed in the extensions subdirectory of your EAServer installation. To use the library in a JSP, add the following directive:

<%@ taglib uri="http://www.sybase.com/EAServer/cachetags.tld" prefix="ct"%>

The library includes the tags described below.


The cache tag

To cache a portion of a page, surround it with this tag, as in:

<prefix:cache attributes>
... page content ...
</prefix:cache>

Where prefix is the tag prefix that you assigned the tag library when declaring it in the taglib directive in your page source, and attributes is a list of attribute-value pairs to set the attributes described in Table 5-2.

Table 5-2: Cache tag attributes

Attribute

Comments

parameters

A comma-delimited list of request parameters to include in the key. A value of “*” includes all parameters in the key. If not specified, all parameters are included in the key.

attributes

A comma-delimited list of session attributes to include in the key. A value of “*” includes all session attributes. If not specified, no session attributes are included in the key.

messageTopics

A comma-delimited list of message topics to include in the key. If not specified, no message topics are included in the key.

This option is useful when a servlet constructs its response based on the values in a database table. A database trigger can be used to call the message service and publish a message to the topic each time the database table is updated. When a servlet or JSP is requested, EAServer can call the message service’s getStatistics method to get the total number of messages published to the topic and compare the value to those in the cache entries.

To use this as a key parameter, the EAServer message service must be configured and running.

localeSensitive

Set this attribute to true if locale-sensitive headers are to be included as part of the key. The default is false, which omits locale-sensitive headers from the key.

headers

A comma-delimited list of request headers to include in the key. The default is to include no headers in the key.

timeout

Specifies how long, in seconds, an entry in the cache remains valid. The default value is 600.

name

Allows you to specify a unique name, so that a cache can be shared across multiple pages. If you do not specify a name, the default value is computed so that each page has one cache for all the tags within that page, and each occurrence of the cache tag is assigned an ID that is unique within the page. You can specify a name to cache parts of a response that occur on several pages: data computed on one page can be read from cache and used in another page.

namespace

Specifies what name space the cache is part of. EAServer tracks usage to determine which entry is the least recently used when entries must be removed. Caches in the same name space share the same use statistics, and EAServer evicts the least recently used entry from all the caches in the name space. The default value is “jspfragmentcache” which means unless otherwise specified, every cache is part of the same name space.

scope

Specifies the scope in which data is stored in the cache. Can be either session or application. The value session indicates that only pages in the same session can view the cached data. The default, application, indicates that all pages in the Web application have access to the cached data.

size

Specifies the size for this cache. Once the cache is full, entries are evicted based on a least recently used policy. Size is specified as a string using either Mb for megabytes or Kb for kilobytes, for example “10Mb” means 10 megabytes. If not specified, the default is 1Mb.

When recompiling a JSP, EAServer flushes any cache entries that are used in the page. When refreshing the Web application, EAServer refreshes all caches that are scoped to the application. You can also flush caches programmatically using the flushCacheByKey or flushCacheByScope tags.


The flushCacheByKey tag

You can use this tag to flush caches for which you have specified a name. You can specify a name, scope, and key parameters as described in Table 5-3. The entry that matches the specified key values and scope is flushed when the tag executes.

Table 5-3: flushCacheBykey tag attributes

Attribute

Comments

name

The cache name.

scope

The cache scope. If not specified, the default is application.

parameters

Same as for the cache tag.

attributes

Same as for the cache tag.

messageTopics

Same as for the cache tag.

localeSensitive

Same as for the cache tag.

headers

Same as for the cache tag.


The flushCacheByScope tag

You can use this tag to flush all entries from all caches in the specified scope. Specify the scope as the scope attribute. The default is application, which flushes all caches in the application. Specify session to flush all caches that are scoped to the user’s session. Specify page to flush all cache entries that are used in the current page.


Portability considerations

The J2EE specifications do not address HTTP response caching. Therefore, any caching implementation is proprietary. To allow portability of JSPs that use the caching tag library, EAServer includes a nonoperational implementation that you can include when exporting WAR files for deployment to other servers. This JAR file is EmptyCacheTags.jar in the java/taglibs directory of your EAServer installation. To include it in a WAR file, select the Export Empty Cache Tags option when exporting the Web application as a WAR file or within an EAR file. If you are using jagtool, specify -emptycachetags true in the jagtool options.


Using the caching API

You can call the caching API to cache response parts in servlets. The API is implemented by class CacheManager, described below.





Copyright © 2005. Sybase Inc. All rights reserved. Class CacheManager