7Web Object Cache Tags and API

This chapter describes the Web Object Cache, an application-level caching mechanism supplied with OC4J. For Web applications written in Java, you can use the Web Object Cache in conjunction with the Oracle9iAS Web Cache for increased speed and scalability.

Overview of the Web Object Cache

The OC4J Web Object Cache is a mechanism that allows Web applications written in Java to capture, store, reuse, post-process, and maintain the partial and intermediate results generated by a dynamic Web page, such as a JSP or servlet. For programming interfaces, it provides a tag library (for use in JSP pages) and a Java API (for use in servlets).

The Web Object Cache works at the Java level and is closely integrated with the HTTP environment of JSP and servlet applications. Cached objects might consist of HTML or XML fragments, XML DOM objects, or Java serializable objects.

Through the Web Object Cache programming interfaces, you can decide how to split Web pages into page blocks that define separate cache objects for finer control of caching. (The terms block and object are used somewhat interchangeably in this sense.) In this way, the application itself can control life span and other behavior of individual cache entities during runtime. Application developers have the best understanding of the life cycle patterns of their application Web pages, so are best suited to determine how to split pages into cache blocks. You can specify maintenance policies for partial results either declaratively in an external file, the cache policy descriptor, or programmatically within the application itself.

Benefits of the Web Object Cache

Note:

The Web Object Cache is useful in particular scenarios and does not replace the need for other caching mechanisms, including the Oracle9iAS Web Cache. For an overview of the Web Object Cache, and how it relates to the Oracle9iAS Web Cache and the Oracle9i Application Server Java Object Cache, including a discussion of when it is appropriate to use each one, see "Overview of Oracle Caching Support for Web Applications".

Using the Web Object Cache can significantly reduce the amount of time spent in constructing page blocks or Java objects in dynamic applications, such as those with expensive intermediate operations like querying a database and formatting or transforming the results. Subsequent queries pull the information out of the cache, so the query and formatting do not have to be repeated.

Furthermore, developers can closely control the cache programmatically, through API calls or custom JSP tags. This can include controlling when cache entries are created, what they are named, when they expire, which users can see which cached data, and what operations can be applied to cached data before the results are served to the user.

Some kinds of Web applications benefit more than others by using the Web Object Cache, depending on the nature and use of their data. For example, applications such as catalog and directory browsing, delayed stock quotes, and personalized portals would particularly benefit. Applications such as real-time stock trading or real-time stock quotes, however, would not benefit, because the data has to be updated so frequently that the overhead of the caching operations would outweigh the benefits. (In these circumstances, however, the Oracle9iAS Web Cache might still be useful because of its lighter overhead.)

In general, the Web Object Cache is most useful in the following situations:

for special post-processing on cached data objects, such as XSLT or XML DOM operations

for sharing data in a non-HTTP situation, such as reusing cached XML data or Java objects and sending the data to others through SMTP, JMS, AQ, or SOAP

for special storage needs, such as storing cached data in a file system or database for persistent storage of data with a long lifetime

for application-specific authorization, allowing different users to have different access rights to different data items, such as for a Web-based groupware application

The application can have its own authorization scheme. The Web Object Cache is embedded within Java authorization logic.

Using the Web Object Cache in JSP pages, instead of in servlets, is particularly convenient. JSP code generation can save much of the development effort.

Web Object Cache Components

The Web Object Cache consists of two main components:

the cache repository

the cache programming interfaces

This section also provides a brief introduction to the Oracle9i Application Server Java Object Cache, which is the default cache repository of the Web Object Cache.

Cache Repository

The cache repository is the component that is responsible for data storage, data distribution, and cache expiration. There can be multiple repository implementations for a programmable Web cache (such as the Web Object Cache), depending on the tier and platform. For example, the file system might be used for secondary storage in the middle tier, and database tables for primary storage in the database tier.

The Web Object Cache uses the Oracle9i Application Server Java Object Cache as its default repository. This is a general-purpose caching service and API designed for Java application use, with objects being accessible by name.

The Java Object Cache is a powerful and flexible programming facility. There are no restrictions on the types of objects that can be cached or the original source of the objects--the management of each object is easily customizable. Each object has a set of attributes such as the following:

Cache Programming Interfaces

The front-end caching interfaces are used through JSP pages and servlets to handle HTTP processing and to direct the semantics relating to the cache policy (rules and specifications determining how the cache works).

The OC4J Web Object Cache programming interfaces can be further divided as follows:

Web Object Cache tag library

This is a convenient wrapper, using JSP custom tag functionality, for the Web Object Cache API. Use custom tags in a JSP page to control the caching, with the API being called through the underlying tag handler classes.

Web Object Cache servlet API

This is the common layer across servlets and JSP pages, dealing with the HTTP semantics and cache policy. This layer communicates with the cache repository.

A cache policy object--an instance of the oracle.jsp.jwcache.CachePolicy class--is created with policy settings based on these inputs. Because the expiration policy is part of the cache policy, each CachePolicy object includes an attribute that is an instance of the oracle.jsp.jwcache.ExpirationPolicy class.

Cache data can be of either session scope, where it is available to only the current HTTP session, or application scope, where it is available to all users of the application.

For example, consider an online banking application that caches the account balance. Only the current user is interested in this information, so session scope is appropriate.

By contrast, consider an online store with a welcome page that issues the same general product recommendations to all users. In this case, it is appropriate for the page to use a cache that has application scope.

Key Functionality of the Web Object Cache

This section discusses key areas of functionality of the Web Object Cache, covering the following:

Cache Block Naming--Implicit Versus Explicit

A cache block is associated with a cache block name, which can be determined either implicitly by the caching policy (generally advisable), or explicitly by your application code. For retrieval, to avoid regenerating the page fragment in question, there is a lookup of the cache block name.

For most situations, implicit naming will result in names that are sufficiently informative, because the HTTP request usually includes all the inputs to the Web application (inputs that determine what the application should generate).

Explicit naming might be desirable in some cases, however, such as when a group of users needs to share the same data. In this case, because relevant identification information may not be available directly from the user's HTTP request, an implicit cache name would not be useful. Instead, you can write code to explicitly generate a cache name that identifies the group. Preferably, the name-generation logic should still use only request parameters as input, not other states existing inside the application. This makes the semantics easier to follow and the code easier to debug.

Following is an example of explicit naming. In the cache tag, note the name attribute with a JSP expression that calls someMethod() to set the cache block name:

Cache blocks can be nested. In this case, the logic of the inner cache block will be executed only when the content of the outer block must be regenerated.

Cloneable Cache Objects

The OC4J Web Object Cache provides an interface, oracle.jsp.jwcache.CloneableCacheObj, which you can implement in serializable cache objects that you want to be cloneable. For mutable objects that are cached without being serialized, cloning is useful in providing a complete and hierarchical copy of the cache object. This section explains the usefulness of cloneability, first covering some necessary background information.

Memory-Oriented Repositories Versus Secondary Storage Repositories

There are two categories of repositories that can be used as the back-end of the Web Object Cache:

A secondary storage repository requires Java serialization during cache operations. During storage to the cache, objects are serialized into the repository; during retrieval from the cache, they are deserialized into memory. Therefore, as a result of the serialization/deserialization process, a complete and distinct copy of the cache object is automatically created during each cache operation.

This is not the case when you store or retrieve cache objects to or from a memory-oriented repository. With a memory-oriented repository, the identical object in the user application will be stored to the cache, or the identical object in the cache will be retrieved for the user. By default, no copy is made. If there are multiple retrievals, all retrievals share the same object.

Advantages in Cloning Copies of Cache Objects

In many cases in your applications, you will want to ensure that different retrievals use different copies of a cache object. There are two key reasons for this:

If the identical cache object is shared across multiple retrievals, changes made to the data in one place may unintentionally affect values retrieved and used elsewhere.

If the identical cache object is shared across multiple retrievals, then multiple Java threads may access the same object simultaneously. This would result in thread safety issues if the original object design was not thread-safe. Perhaps, for example, the object was originally intended for page-scope or request-scope usage only, where there could be only one thread per object. This thread-behavior assumption would be violated.

To avoid these possible problems, use complete and hierarchical copies when you store and retrieve generic Java serializable data to or from a memory-oriented repository. "Complete and hierarchical" means copying not just the direct members referenced by the object, but also any indirect variables that are referenced. For example, assume an object Y has a java.util.Vector instance as a member variable. Cloning a complete and hierarchical copy involves copying not just the Vector instance itself, but also all mutable objects or elements referenced by the Vector instance.

Use of the CloneableCacheObject Interface

If you implement the CloneableCacheObject interface and its cloneCacheObj() method in your cache objects, then the Web Object Cache will automatically call cloneCacheObj() to make a complete and hierarchical copy of each cache object whenever it is stored to or retrieved from a memory-oriented cache repository.

One of the OC4J demos (using the useCacheObj tag to cache generic Java objects) demonstrates the use of a cloneable cache object.

Cache Block Runtime Functionality

During runtime, when a Web Object Cache cache tag is encountered, the tag handler checks whether a corresponding cache object exists and was created recently enough to reuse. If so, the code in the body of the tag is not executed; instead, the cache object is reused. But if the cache object does not exist or is too old, the tag body code will be executed to generate a new object (page fragment, XML DOM object, or Java serializable object). Then this freshly generated object will be captured, such as through special buffer writing or object passing, and stored into the cache.

If computations in content generation are costly, such as for a complicated database query, and the life span of the cache is appropriate, so that the cached data is reusable, then the Web Object Cache can save significant amounts of time and system resources. Application speed and throughput will be greatly improved.

Data Invalidation and Expiration

You can set up cache blocks to expire after a specified duration or at a specified time, or they can be invalidated explicitly by a method call or tag invocation.

Cache Block Expiration

Because cache blocks mainly consist of semi-static fragments of information, the Oracle implementation does not require a tightly coherent expiration model. A looser model typically provides acceptable results and requires less synchronization overhead.

There are two categories of expiration for data in Web Object Cache blocks:

duration (time-to-live)--expiration occurs after data has been in the cache for a specified amount of time

fixed time/day--expiration occurs regularly at a set time, such as at a specified time each day or on a specified day each week

Expiration details are determined by the settings of attributes in an instance of the oracle.jsp.jwcache.ExpirationPolicy class. This ExpirationPolicy object is an attribute of the CachePolicy object associated with the cache block. See "Expiration Policy Attributes".

In JSP pages, you can set ExpirationPolicy attributes through attributes of the Web Object Cache cache tags (such as cache, cacheXMLObj, or useCacheObj). In servlets, you can use methods of the ExpirationPolicy object directly. (See "ExpirationPolicy Methods".) Alternatively, you can set ExpirationPolicy attributes through a cache policy descriptor. (See "Cache Policy Descriptor".)

Cache Block Invalidation

Instead of depending on expiration to invalidate a cache, you can invalidate it explicitly in one of the following ways:

Use the overloaded invalidateCache(), invalidateCacheLike(), or invalidateCacheOtherPathLike() method of a CachePolicy instance to explicitly invalidate one or more cache blocks. See "CachePolicy Methods".

Attributes for Policy Specification and Use

This section describes cache policy attributes--specifically, attributes of the CachePolicy and ExpirationPolicy classes. You can set these attributes through custom tags in JSP pages, directly through the provided Java API in servlets, or through a cache policy descriptor file.

Cache Policy Attributes

Cache policies, introduced in "Cache Policy and Scope", consist of the details that determine how cache blocks behave. You can set cache policy attributes in several ways, as described in subsequent sections:

Specification of cache policy settings results in the creation of a cache policy object, which includes an expiration policy object as one of its attributes. Following is abbreviated code for the CachePolicy class (in package oracle.jsp.jwcache), for illustration purposes only, showing the names of the cache policy attributes.

Attribute Descriptions

Table 7-1 Cache Policy Attribute Descriptions

Attribute

Type

Description

ignoreCache

boolean

This is for use during development only. When making frequent code changes, set this to true to disable the cache, typically so that results that were generated prior to your changes will not be returned.

default: false

scope

int

Specifies the scope of the cache. Use the integer constant SCOPE_SESSION for the cache block to be accessible only to the current HTTP session, or SCOPE_APP for the cache block to be accessible to all HTTP sessions of the application.

default: application

autoType

int

Specifies whether the cache block is named explicitly or implicitly, and how properties of the HTTP request are used in cache block naming (for implicit naming). The name is relevant in determining when the cache is reused for subsequent requests. See "More About Cache Block Naming and the autoType Attribute".

default: implicitly, according to the URI plus all parameters plus selected cookies (TYPE_URI_ALLPARAM)

It can be expressed as milliseconds between midnight, January 1, 1970 and the desired absolute time limit, or as a java.util.Date instance. Additional convenient formats are available through the cache tag--see "Web Object Cache Tag Descriptions".

It takes precedence over reusableDeltaTime.

If its value is set as the integer constant REUSABLE_ALWAYS or the string constant REUSABLE_IGNORED, then cache entries are always reusable, for as long as they remain in the cache.

It is not available through the XML cache policy descriptor file.

default: always reusable

reusableDeltaTime

long

A relative time limit for cache usability, where a cache block is not reused if the difference between cache block creation time and current time is greater than reusableDeltaTime. Instead, data is regenerated, but the cache block is unaltered. See "More About reusableTimeStamp and reusableDeltaTime".

Note the following regarding reusableDeltaTime:

It is specified in seconds.

The reusableTimeStamp attribute overrides it.

If its value is set as the integer constant REUSABLE_ALWAYS or the string constant REUSABLE_IGNORED, then cache entries are always reusable, for as long as they remain in the cache.

default: always reusable

expirationPolicy

ExpirationPolicy

An expiration policy object (an instance of oracle.jsp.jwcache.ExpirationPolicy), which specifies circumstances under which the repository will remove cache blocks from storage.

More About Cache Block Naming and the autoType Attribute

More specifically, there are six ways for cache blocks to be named. Explicit naming is the first way. Specify this with an autoType setting of TYPE_USERSPECIFIED (an integer constant).

The other five ways are variations of implicit naming:

implicit naming with only the request URI being used in the name

Specify this with an autoType setting of TYPE_URI_ONLY.

implicit naming according to the following:

request URI + query string + selected cookies

Specify this with an autoType setting of TYPE_URI_QUERYSTR. Specify the cookies in the selectedCookies[] attribute.

implicit naming according to the following:

request URI + all parameters + selected cookies (default)

Specify this with an autoType setting of TYPE_URI_ALLPARAM. Specify the cookies in the selectedCookies[] attribute.

implicit naming according to the following:

request URI + selected parameters + selected cookies

Specify this with an autoType setting of TYPE_URI_SELECTEDPARAM. Specify the parameters in the selectedParameters[] attribute and the cookies in the selectedCookies[] attribute.

implicit naming according to the following:

request URI + all but excluded parameters + selected cookies

Specify this with an autoType setting of TYPE_URI_EXCLUDEDPARAM. Specify the cookies in the selectedCookies[] attribute, and specify the excluded parameters in the selectedParameters[] attribute.

As an example, assume that you have developed a JSP page, welcome.jsp, with a personalized greeting for each user. The data with the personalized greeting is the only cache block in the page. Further assume that you have specified "request URI + selected parameters + selected cookies" naming, with user as the only selected parameter for cache block naming and no selected cookies for naming.

Now assume the page is requested as follows:

http://host:port/a.jsp?user=Amy

In this case, a.jsp?user=Amy becomes the cache block name.

Now assume that the page is later requested by another user, as follows:

http://host:port/a.jsp?user=Brian

This will not reuse the "Amy" cache, because the value of user is different. Instead, a new cache block is created with a.jsp?user=Brian as the name.

Now assume a later request by the first user, as follows:

http://host:port/a.jsp?mypar=3&user=Amy

Because the user is again Amy, this request will reuse the first cache, displaying Amy's customized information without having to regenerate it. The mypar parameter is irrelevant to the caching mechanism because you did not include it in the selectedParameters[] list of the cache policy object, presumably because you determined that the value of mypar is not relevant in terms of cachable page output.

Now assume the following subsequent request:

http://host:port/a.jsp?yourpar=4&user=Brian&hello=true&foo=barfly

Because the user is again Brian, this request will reuse the second cache, displaying Brian's customized information without having to regenerate it. The yourpar, hello, and foo parameters are irrelevant to the caching mechanism because you did not include them in the selectedParameters[] list of the cache policy object.

More About reusableTimeStamp and reusableDeltaTime

Be aware that the concept of reusable is different than the concept of time-to-live (TTL) and is intended for more advanced use. Time-to-live, which controls the general lifetime of a cache, is described in "Expiration Policy Attributes". Usually time-to-live is all that is required to appropriately limit the use of cached data.

The attributes for reusability--reusableTimeStamp and reusableDeltaTime--are intended for more specialized use and do not affect the expiration or invalidation of cached data. As an example, consider a situation where different users have different requirements for how up-to-date a Web report is. Assume that most users can accept a report produced anytime within the past day, and that they all want to be looking at the same version so they can compare figures. An appropriate TTL value, then, would be "one day".

Also presume, however, that there is a small group of privileged users for whom the data is much more time-sensitive. They want to have information that is no more than one hour old.

In this case, although TTL is set to "one day" for all users, there can be a reusableDeltaTime setting of "one hour" for the privileged users, which will result in the cache not being used for them if the data is more than one hour old. Remember, though, that reusableTimeStamp and reusableDeltaTime do not expire the cache or otherwise affect it--the cached data can still be used for non-privileged users, according to the time-to-live.

It is up to the application logic to set appropriate values of reusableTimeStamp and reusableDeltaTime for the privileged user group.

Expiration Policy Attributes

Expiration policies are introduced in "Data Invalidation and Expiration". Expiration policies contain the details that determine when cache blocks expire, at which point their data should no longer be used and the data should be regenerated instead. (Note that for most discussion, you can think of the expiration policies as being part of the cache policies.) ExpirationPolicy attributes, as with CachePolicy attributes, can be set in any of the following ways:

The names documented below for integer constants are for servlet usage. Different names may be used for the Web Object Cache tags. See "Web Object Cache cache Tag".

Table 7-2 Expiration Policy Attribute Descriptions

Attribute

Type

Description

expirationType

int

The type of expiration policy--one of the following (the TYPE_XXX values are integer constants):

time-to-live, specified with an expirationType setting of TYPE_TTL (also see the TTL attribute below)

daily, to expire within a day at a specified time, specified with an expirationType setting of TYPE_DAILY (also see the timeInaDay attribute below)

weekly, to expire within a week on a specified day at a specified time, specified with an expirationType setting of TYPE_WEEKLY (also see the timeInaDay and dayInaWeek attributes below)

monthly, to expire within a month on a specified date at a specified time, specified with an expirationType setting of TYPE_MONTHLY (also see the timeInaDay and dayInaMonth attributes below)

default: time-to-live

TTL

long

Time-to-live--the amount of time the cache block is good for, expressed in seconds.

default: 300 (5 minutes)

timeInaDay

long

The time of day used for daily, weekly, or monthly expiration, expressed in seconds from midnight--0 is 00:00:00 (midnight); 86399 is 23:59:59.

default: 300 (00:05:00); ignored if expirationType=TYPE_TTL

dayInaWeek

int

The day of the week for weekly expiration, at the specified timeInaDay--WEEKLY_SUNDAY, WEEKLY_MONDAY, WEEKLY_TUESDAY, WEEKLY_WEDNESDAY, WEEKLY_THURSDAY, WEEKLY_FRIDAY, or WEEKLY_SATURDAY (integer constants).

default: Wednesday; ignored unless expirationType=TYPE_WEEKLY

dayInaMonth

int

The date of the month for monthly expiration, such as 10 for the 10th of each month, at the specified timeInaDay. The maximum setting is the number of days in the month when the cache block is created. For example, if a cache block is created in June and dayInaMonth has a setting of 31, then its effective value will be 30.

default: 10; ignored unless expirationType=TYPE_MONTHLY

writeThrough

boolean

A flag specifying whether the cache repository should treat the cache entry as a write-through cache, writing it immediately into secondary storage such as a file system or database. Set this to true for write-through mode. A write-through cache will survive a server restart or power failure.

With a false setting, the cache entry is treated as a delayed-write cache, which is appropriate for caches that have a short life span, such as 5 or 10 minutes, and are not overly expensive to recompute.

default: true

Note: some cache repositories may not support write-through mode; others may always use write-through mode.

Web Object Cache Tag Descriptions

From JSP pages, you can specify cache policy settings, expiration policy settings, and explicit invalidation through custom tags provided with OC4J. Discussion is organized into the following categories:

The Web Object Cache classes are in the file ojsputil.jar, which is supplied with OC4J. Verify that this file is installed and in your classpath. Also, to use the Oracle9i Application Server Java Object Cache as the back-end repository, the file cache.jar must be installed and in your classpath. This file also comes with OC4J.

To use the Web Object Cache tags, the tag library description file, jwcache.tld, must be deployed with the application in the location specified in the taglib directives of your JSP pages, such as in the following example:

<%@ taglib uri="/WEB-INF/jwcache.tld" prefix="ojsp" %>

In an Oracle9i Application Server installation, the tag library description file is located in the [Oracle_Home]/j2ee/tlds directory.

Notes:

The prefix "ojsp:" is used in the tag syntax here. This is by convention but is not required. You can specify any desired prefix in your taglib directive.

Cache Tag Descriptions

This section describes the following tags:

cache

This tag is for general character-based caching (HTML or XML fragments).

cacheXMLObj

This tag is for caching XML objects; its parameters comprise a superset of the cache tag parameters. Because the Web Object Cache is particularly useful when post-processing XML documents, you will likely use the cacheXMLObj tag more often than the cache tag.

useCacheObj

This tag is for general caching of Java serializable objects. Some of the semantics and syntax are patterned after the standard jsp:useBean tag.

cacheInclude

This tag combines the functionality of the cache tag with that of the standard jsp:include tag.

This section also describes conditional execution of code within the cache tags, possible resulting problems, and the workaround of dividing cache blocks into individual JSP pages and, optionally, using the cacheInclude tag to combine the pages together appropriately.

Web Object Cache cache Tag

This section documents the syntax and attributes of the cache tag, which you can use to set up general caching in a JSP application, in contrast to the caching of XML objects or Java serializable object.

Attributes

Most of the parameters of the cache tag correspond to attributes in the CachePolicy or ExpirationPolicy class, described earlier in this chapter (as referenced below).

policy--Optionally use this to specify a cache policy descriptor, the settings of which would be used in defining the cache policy. You can use a cache policy descriptor instead of using the various individual cache tag attribute settings, or to establish default values that you can optionally override through tag attribute settings.

invalidateCache--Enable this flag for the corresponding cache block (any pre-existing cache block with the same name) to first be invalidated. This is particularly useful where implicit cache block naming is used, but can also be used for explicit names by specifying the cache block name in the name attribute of the cache tag. The default setting is "false".

Note:

Do not confuse this attribute with the more general-purpose invalidateCache tag. See "Web Object Cache invalidateCache Tag". The invalidateCache attribute is for more specialized or advanced use to invalidate individual cache blocks.

printCacheBlockInfo (for debugging)--Enabling this parameter results in printing of the internal cache name, creation time, and expiration time of the cache block, within HTML/XML comment constructs. The default setting is "false".

printCachePolicy (for debugging)--Enabling this parameter results in printing of the values of all cache policy attributes for this cache block, within HTML/XML comment constructs. The default setting is "false".

Syntax (in addition to that of the cache tag)

This tag can optionally be in the form of a single tag with no body: <ojsp:cacheXMLObj ... />

For convenience, this tag is duplicated in the XML tag library, defined in the xml.tld tag library description file.

This tag can act as both an XML producer and an XML consumer. Do not use fromXMLObjName and toXMLObjName if the XML object is being passed implicitly. (See "XML Producers and XML Consumers".)

Attributes (in addition to those of the cache tag)

fromXMLObjName--For explicit passing, specify the name of the XML input object being passed to the cache (from the pageContext object).

toXMLObjName--For explicit passing, specify the name of the XML output object being passed from the cache (to the pageContext object).

toWriter--Set this to true to write the XML object to a JSP writer to output directly to the user's browser. The default value is "false".

Note:

The cacheXMLObj tag is one of several custom tags supplied with OC4J that are XML-related, meaning these tags sometimes (or always) take an XML object as input or create one as output. Other such tags include the SQL library dbQuery tag, which can output query results as an XML DOM object, and the XML library transform and styleSheet tags, which can take an XML object as input and use XSLT transformation to create another XML object or a JSP writer as output. These tags are consistent in having a fromXMLObjName attribute and a toXMLObjName attribute for explicit passing of XML data. For general information, see "XML Producers and XML Consumers".

The SQL dbOpen and SQL dbQuery tags connect to the database and execute a query. The cacheXMLObj tag caches the XML DOM object produced by the query--in subsequent executions (for output through different stylesheets, for example) the query does not have to be re-executed, because the DOM object can be retrieved from the Web Object Cache. The XML transform tag outputs the query results according to an XML stylesheet (specified through a variable). The JESI fragment tag encloses HTML output to be cached (which does not require application-level caching). The JESI template tag disables caching outside the fragment (through the cache="no" setting).

If you are using the cache tag (not cacheXMLObj or useCacheObj), it might be helpful to break your cache blocks into separate JSP pages so that you would be less likely to fall into this type of situation. In this case, each cache block would be represented by its own URI, and you could use dynamic include functionality to combine the pages together as desired.

There is no flush parameter (unlike for the standard jsp:include tag).

If any of these limitations presents a problem, then use separate cache and include tags.

Also be aware of an important difference between the cacheInclude tag and the JESI include tag. (See "JESI include Tag" for information about that tag.) Because the Oracle9iAS Web Cache is in a different caching layer than the Web Object Cache, the including page and included page for a JESI include tag cannot share the same request object. There is no such limitation with the cacheInclude tag, however--the including page and included page share the same request object, so beans and attributes of request scope can be passed between the two pages.

Syntax

For the cacheInclude tag, because policy and page are not request-time attributes, you do not have the option of determining their values through JSP expressions. (Be aware that policyis a request-time attribute for the cache, cacheXMLObj, and useCacheObj tags.)

Attributes

policy (required)--You must use a cache policy descriptor file to specify cache policy settings; individual parameter settings are not supported.

page (required)--Use the page attribute to specify the URI of the page to dynamically include, as with a standard jsp:include tag.

name--Use this with invalidateNameLike to invalidate one or more cache blocks that were named through explicit cache-block naming, according to the instructions in "Use of name and invalidateNameLike" below.

invalidateNameLike--Use this with name to invalidate one or more cache blocks that were named through explicit cache-block naming, according to the instructions in "Use of name and invalidateNameLike" below. The default setting is "false".

page--Specify a page-relative or application-relative URI. Use this with autoInvalidateLevel to invalidate one or more cache blocks that were named through implicit cache-block naming, according to the instructions in "Use of page and autoInvalidateLevel" below.

autoInvalidateLevel--Use this with page to invalidate one or more cache blocks that were named through implicit cache-block naming, according to the instructions in "Use of page and autoInvalidateLevel" below.

Use of name and invalidateNameLike

To invalidate one or more cache blocks that were named through explicit cache-block naming, use the name and invalidateNameLike attributes together, as follows:

If invalidateNameLike="false", then use the name parameter to specify the name of a single cache block to invalidate.

If invalidateNameLike="true", and the underlying cache repository supports wild card characters, then you can use the wildcard "*" character in the name parameter to invalidate multiple cache blocks whose names fit the criteria. (The Oracle9i Application Server Java Object Cache currently does not support wild card characters.)

Use of page and autoInvalidateLevel

To invalidate one or more cache blocks that were named through implicit cache-block naming, use the page and autoInvalidateLevel attributes together, as follows:

Use the page attribute to specify the appropriate URI of the Web page. (With implicit naming, cache block names are based on Web page URIs.)

If autoInvalidateLevel="application", then all cache blocks associated with the application that the page belongs to will be invalidated.

For example, if there is an application under the /mycontext context path, and autoInvalidateLevel="application", then all cache entries of all pages under http://host:port/mycontext will be invalidated.

Here is a corresponding usage example:

<ojsp:invalidateCache page="/" autoInvalidateLevel="application" />

If autoInvalidateLevel="page", then all cache block entries associated with the page will be invalidated.

For example, if autoInvalidateLevel="page" and the request is the following:

http://host:port/mycontext/mypage01.jsp?foo=bar

then all cache entries of mypage01.jsp will be invalidated, regardless of what request parameters and cookies they are associated with. This includes cache blocks associated with the following, for example:

In this case, cache blocks associated with the following, for example, will not be invalidated:

http://host:port/mycontext/mypage01.jsp?foo=bar2

However, cache blocks associated with the following will be invalidated, regardless of what cookies they are associated with:

http://host:port/mycontext/mypage01.jsp?foo=bar

Continuing this example, consider the following:

http://host:port/mycontext/mypage01.jsp?foo=bar&p1=v1

Cache blocks associated with this request will be invalidated if c1.cpd selects the foo HTTP request parameter only, and the cache blocks are stored under the same cache policy, c1.cpd. However, the cache objects will not be invalidated if they were not stored under c1.cpd, or if c1.cpd also selects the p1 parameter.

If autoInvalidateLevel="cookie", then the only cache entries invalidated are those associated with the same page, same selected parameters and values, and same cookies.

Note:

If the page URI includes a question mark, then the default autoInvalidateLevel is param. If there is no question mark, then the default is page.

Example--Use of Cache Invalidation Tag

This section provides a brief example of cache invalidation. For complete sample applications, including cache invalidation, refer to the OC4J demos.

Example: invalidateCache Tag

The following page adds an item to a list of items previously cached, then invalidates the cache. The list will presumably be re-cached later with the new item.

Web Object Cache Servlet API Descriptions

From servlets, you can use CachePolicy methods to modify cache policy settings or to invalidate a cache block, and ExpirationPolicy methods to modify expiration settings. This requires creating a cache policy object and retrieving its expiration policy object attribute (which the JSP cache tag handlers do automatically).

The Web Object Cache classes are in the file ojsputil.jar, which is supplied with OC4J. Verify that this file is installed and in your classpath. Also, to use the Oracle9i Application Server Java Object Cache as the back-end repository, the file cache.jar must be installed and in your classpath. This file also comes with OC4J.

For more information about the classes, interfaces, and methods described in this section, see the Javadoc that is supplied with OC4J.

Cache Policy Object Creation

There are two approaches to creating a CachePolicy object:

Use the static lookupPolicy() method of the CacheClientUtil class.

Use one of the public CachePolicy constructors.

Note:

Cache policy objects are not resource objects, such as database connections or cursors, so you can manipulate them without life-cycle or resource management concerns.

Using the lookupPolicy() Method

In most situations, the most convenient way to create a CachePolicy object is through the static lookupPolicy() method of the CacheClientUtil class, provided with OC4J, as in the following example:

Using a CachePolicy Constructor

The oracle.jsp.jwcache.CachePolicy class has the following public constructors--a simple constructor requiring only a servlet configuration object, a "copy" constructor that copies another CachePolicy object, and a "copy" constructor with a given servlet configuration object:

This is an array of new cookies. If you pass in new cookies, they are used in cache operations that use the otherPath parameter (such as the putAutoCacheForOtherPath() method), assuming the cache policy selects some cookies, and invalidation is at the cookie level. If you do not pass in new cookies, then cookies of the current HTTP request are used instead.

specifiedName, a Java string

For explicit cache-block naming, this is the name--either the desired cache block name if you are creating a new cache block, or the existing cache block name if you are retrieving an existing cache block.

For implicit cache-block naming, this is a counter that is used in tracking cache blocks. In JSP pages it is used, incremented, and maintained by JSP cache tag handlers. It is stored in the JSP pageContext object.

SectionId is an interface that is implemented by two classes--StringSectionId and NumberSectionId. Where StringSectionId is specified in a method signature, you must use an instance of that class. Where SectionId is specified, you can use an instance of either class. Typically you should use StringSectionId, however. NumberSectionId is primarily intended for use by tag handlers in JSP pages.

When you construct a StringSectionId instance, the string must begin with an alphabetic (not numeric) character.

otherPath, a Java string

The URI of another JSP page that has an associated cache block that you want to store, retrieve, or invalidate.

autoInvalidateLevel, an integer

For implicit cache-block naming, you can use this to specify a level of invalidation--application, page, parameter, or cookie. Use the CachePolicy integer constant AUTO_INVALIDATE_APP_LEVEL, AUTO_INVALIDATE_PAGE_LEVEL, AUTO_INVALIDATE_PARAM_LEVEL, or AUTO_INVALIDATE_COOKIE_LEVEL.

CachePolicy Method Descriptions

The CachePolicy methods function as follows:

isRecent()

This method checks the timestamp of the specified cache block and determines whether it is recent enough, given the current time and the values of the cache policy reusableTimeStamp and reusableDeltaTime attributes.

putCache(...)

Use this method to place an object into the cache repository. The data parameter is any serializable Java object you want to cache that will not require any further modification or mutation. In JSP pages, the JSP cache tag handler calls putCache() to cache a BodyContent instance. The cacheXMLObj tag handler calls it to cache an XML DOM object. In a servlet or useCacheObj tag, the cache target object can be any Java serializable object.

You must also provide an HTTP request object and a cache block name (for explicit naming) or a section ID (for implicit naming).

Note:

The putCache() method does nothing if the cache policy ignoreCache attribute is true.

putAutoCacheForOtherPath(...)

Place the specified object into the cache repository according to a specified string-based section ID and a specified page path, optionally using specified cookies as well. You must also input an HttpServletRequest object. The cache policy must not use explicit naming (in other words, must not have autoType=TYPE_USERSPECIFIED).

getCache(...)

Use this method to retrieve a cached item from the repository, in the form of an oracle.jsp.jwcache.CacheBlock instance. You can specify the cache block name (for explicit naming) or the section ID (for implicit naming). You must also provide an HTTP request object.

Note:

The getCache() method does nothing if the cache policy ignoreCache attribute is true.

getAutoCacheForOtherPath(...)

Retrieve a cached item from the repository according to a specified string-based section ID and a specified page path, optionally using specified cookies as well. You must also input an HttpServletRequest object. The cache policy must not use explicit naming (in other words, must not have autoType=TYPE_USERSPECIFIED)--otherwise, an exception is thrown.

invalidateCache(...)

Use this method to invalidate a single cache block, according to the HTTP request object and the specified cache block name (for explicit naming) or the section ID (for implicit naming).

invalidateCacheLike(...)

Use this method to invalidate multiple cache blocks. If you use explicit cache-block naming and the cache repository supports wild-card naming, you can input the specifiedName parameter with "*" wild card characters. (The Oracle9i Application Server Java Object Cache currently does not support wild card characters.)

If you use implicit cache-block naming, you must specify the autoInvalidateLevel parameter to determine, in combination with the HttpServletRequest object and optionally the specifiedName parameter, what cache blocks are invalidated. The autoInvalidateLevel parameter has the same functionality as in a JSP invalidateCache tag, as explained in "Web Object Cache invalidateCache Tag" (using information from the request object, instead of using information from the page parameter of the invalidateCache tag).

invalidateCacheOtherPathLike(...)

Use this method to invalidate cache blocks associated with the URI you provide in the otherPath parameter. In the signature taking only a request object and the URI, the autoInvalidateLevel parameter is set automatically according to the URI--to param level if there is a question mark ("?") in the URI; to page level otherwise.

The detailed signature of this method allows you to specifically control the autoInvalidateLevel setting and the cookies used in invalidation.

getCurrentTime()

Retrieve the current time value, as a java.util.Date instance, of the underlying cache repository specified in this cache policy.

CachePolicy Getter and Setter Methods

You can use the following methods to retrieve or alter CachePolicy object attributes. See "Cache Policy Attributes" for a discussion of these attributes.

boolean getIgnoreCache()

void setIgnoreCache(boolean ignoreCache)

void setIgnoreCache(String ignoreCacheStr)

int getScope()

void setScope(int scope)

For scope values, the integer constants SCOPE_APP and SCOPE_SESSION are available.

For reusableTimeStamp values, the integer constant REUSABLE_ALWAYS is available, indicating that the cache is always reusable.

long getReusableDeltaTime()

void setReusableDeltaTime(long reusableDeltaTime)

For reusableDeltaTime values, the integer constant REUSABLE_ALWAYS is available, indicating that the cache is always reusable.

ExpirationPolicy getExpirationPolicy()

void setExpirationPolicy(ExpirationPolicy expirationPolicy)

String getCacheRepositoryName()

void setCacheRepositoryName(String repoName)

boolean getReportException()

void setReportException (boolean reportException)

void setReportException (String reportExceptionStr)

The following methods are also available, but are primarily intended for use by the Web Object Cache tag handlers:

void setScope(String scopeStr)

For scope values, the string constants SCOPE_APP_STR and SCOPE_SESSION_STR are available.

void setAutoType(String autoTypeStr)

void setReusableTimeStamp(String reusableTimeStampStr)

For reusableTimeStamp values, the string constant REUSABLE_IGNORED is available, indicating that the cache is always reusable.

void setReusableDeltaTime(String reusableDeltaTimeStr)

For reusableDeltaTime values, the string constant REUSABLE_IGNORED is available, indicating that the cache is always reusable.

Expiration Policy Object Retrieval

Each CachePolicy object has an ExpirationPolicy attribute. If you want to set expiration policies for a cache block, you can use the getExpirationPolicy() method of its CachePolicy object, as in the following example:

ExpirationPolicy Methods

The ExpirationPolicy class has getter and setter methods for its attributes, as follows. For descriptions of these attributes, see "Expiration Policy Attributes".

int getExpirationType()

void setExpirationType(int expirationType)

void setExpirationType(String expirationTypeStr)

long getTTL()

void setTTL(long ttl)

long getTimeInaDay()

void setTimeInaDay(long timeInaDay)

void setTimeInaDay(String timeInaDayStr)

int getDayInaWeek()

void setDayInaWeek(int dayInaWeek)

void setDayInaWeek(String dayInaWeekStr)

int getDayInaMonth()

void setDayInaMonth(int dayInaMonth)

boolean getWriteThrough()

void setWriteThrough(boolean writeThrough)

void setWriteThrough(String writeThroughStr)

Additionally, the ExpirationPolicy class has the following utility method:

long getExpirationTime(long createTime)

Given the creation time of a cache block expressed in milliseconds since midnight January 1, 1970, this method calculates and returns the expiration time, also in milliseconds since midnight January 1, 1970. That is, the timestamp when expiration should occur, according to the expiration policy.

The ExpirationPolicy class also defines the following integer constants for the expirationType attribute:

TYPE_TTL

TYPE_DAILY

TYPE_WEEKLY

TYPE_MONTHLY

And the following integer constants are defined for the dayInaWeek attribute:

Code Notes

The following notes describe some of the key functionality of the preceding example:

The cache policy object is created in the lookupPolicy() call (Note A), with attribute settings according to the cache policy descriptor test-policy.cpd.

The section ID is created for each cache block (Note B), as required for implicit cache-block naming. See "CachePolicy Methods" for information about section IDs.

The cache block is retrieved from the repository through the getCache() method of the cache policy object (Note C), and placed into the repository through the putCache() method, according to the section ID in each case.

The isRecent() call determines if the cache block is recent enough to use (Note D). If so, the cached data is retrieved through the getData() method of the cache block. (See "CacheBlock Methods".) If not, a special PrintWriter object is created to buffer the output and save it back to the cache repository. If the cache block object is not found (is null, Note E), then the putCache() method of the cache policy object is called to create a new cache block (Note F).

Tag Code Versus API Code

This example presents code for three approaches to an application that caches and presents timestamp output from two cache fragments:

The first approach, tagcode.jsp, is a simple JSP page that uses the Oracle Web Object Cache tags.

The second approach, servletcode.jsp, is a more involved JSP page that uses the Web Object Cache servlet API (instead of the cache tags) inside a Java scriptlet.

Following the three code samples is a listing of the cache policy descriptor, test-policy.cpd.

In each approach, the application will cache the two fragments it displays. You can reload repeatedly, but the times displayed in the fragments will not change until the cached fragments expire. The first fragment takes 25 seconds to expire, getting the 25-second time-to-live value from the TTL setting in the cache policy descriptor (test-policy.cpd). The second fragment takes 15 seconds to expire, overriding the cache policy descriptor time-to-live value with a value set directly in the page code.

Output for the sample applications looks something like the following:

Cache Policy Descriptor

You can optionally use an XML-style cache policy descriptor to specify attribute settings for the CachePolicy and ExpirationPolicy objects. In any JSP pages or servlets that you use, you would then specify the cache policy descriptor through the policy attribute of a cache, cacheXMLObj, useCacheObj, cacheInclude, or invalidateCache tag.

This section provides the cache policy descriptor DTD, a sample cache policy descriptor, and information about loading and refreshing the cache policy descriptor.

Cache Policy Descriptor DTD

This section provides a listing of the Web Object Cache cache policy descriptor DTD, cachepolicy.dtd. For an example of a cache policy descriptor, see "Sample Cache Policy Descriptor".

Cache Policy Descriptor Loading and Refreshing

To create a CachePolicy object from an XML cache policy descriptor file, there must be a call to the static lookupPolicy() method of the oracle.jsp.jwcache.CacheClientUtil class. For JSP pages, this is handled automatically. For servlets, you must include the lookupPolicy() call in your code--see "Sample Servlet Using the Web Object Cache API".

If the caching policy has not been previously loaded, then the lookupPolicy() method results in the XML descriptor being parsed and used in constructing a new CachePolicy object (and an ExpirationPolicy attribute of this object). See "Cache Policy Object Creation" for information about the lookupPolicy() method.

The CachePolicy object is stored indirectly under the ServletContext object associated with your application. When the same caching policy is requested again, the stored policy object will be returned without the descriptor being re-read or re-parsed. For performance reasons, because the cache policy descriptor files are seldom changed, as well as for security reasons, OC4J does not provide descriptor auto-reloading functionality. The resulting cache policy object is stored in the middle-tier JVM for faster access.

The CachePolicy object will be valid until the servlet context is destroyed or someone calls the static refreshPolicy() method of the CacheClientUtil class. This method has the same calling sequence as the lookupPolicy() method. For example:

When you alter and refresh the caching policy, active cache blocks are not affected.

Cache Repository Descriptor

Use an XML-style cache repository descriptor to specify what to use as the back-end cache repository for the Web Object Cache, and how to configure it. This section supplies the DTD for cache repository descriptors, as well as a sample cache repository descriptor.

The Java Object Cache cache.jar file must be available in the <your_install_root>/lib directory.

The misc-file directory is created automatically when you extract the demo programs.

Update OCS4J.properties as appropriate. To set a root directory for the Java Object Cache, update the diskPath entry.

For a UNIX system, do this as in the following example:

diskPath = /mydir/ocs4jdir

or, for a Windows NT system (note that you have to specify a drive letter):

diskPath = c:\mydir\ocs4jdir

Restart the Web server.

Configuration Notes for File System Cache

To use a file system as the back-end repository, edit the cache repository descriptor (wcache.xml) to set reporoot to specify a root directory for the file system cache. (This file is located in the WEB-INF directory where the OC4J samples are installed. See "Cache Repository Descriptor" for general information and for an example of a cache repository descriptor that sets a reporoot value.)