Home | 2.7.1 Caching servlets and JSPs | 2.7.3 Command interface
2.7.2 Java objects and the command cache
DynaCache provides support for caching the returned results from Java object calls. DynaCache provides a very straightforward mechanism that allows you to cache the results from these Java command objects; it is called command caching.
Commands written to the WebSphere command architecture access databases, file systems, and connectors to perform business logic, and often execute remotely on another server. Significant performance gains can be achieved by caching command results and avoiding their repeated execution.
To take advantage of command caching, applications must be written to the WebSphere command framework API. This API is based on a common programming model, and uses a series of getters(), setters() and execute() methods to retrieve its results. To use a command, the client creates an instance of the command and calls the execute() method for the command. Depending on the command, calling other methods could be necessary. The specifics will vary with the application.
Implementing command caching involves software development. However, the development requirements for command caching are not difficult. You can cache the output from calls to:
- Java classes (POJO)
- Java Beans
- EJBs
- Web services
As an application architect you should establish a coding pattern that will allow applications to use caching at a later stage in development without having to go back and re-implement the code again.
For example, say you have a system that uses a Java Bean to query a database and that bean reads in all of the names of the states, counties, or provinces within your country, including tax rates per region. Rather than have each call to the bean hit the database, you could do the following:
- Modify the bean by adding the necessary TargetableCommand methods.
- Update your cachespec.xml and add a new cache entry that includes this new command.
By implementing the previous steps, DynaCache is now aware of your bean, and can intercept calls made to that bean and serve responses from the cache instead.
Example 2-8 Example of specifying a command cache entry
<?xml version="1.0"?> <!DOCTYPE cache SYSTEM "cachespec.dtd"> <cache> <cache-entry> <class>command</class> <sharing-policy>shared</sharing-policy> <name>com.ibm.myapp.statetaxation.StateTaxCacheCommand.class</name> <cache-id> <component type="method" id="getStateTaxList"> <required>true</required> </component> <priority>1</priority> </cache-id> </cache-entry> </cache>Note that the <sharing-policy> tag is for enabling clustered data replication support.
Commands cache the data before it is transformed into HTML. In order to take advantage of command caching, make simple code changes to your Java objects so that DynaCache can call them to retrieve the data. Command objects inherit from com.ibm.websphere.command.cachableCommandImpl and must provide certain methods to function properly.