Network Deployment (Distributed operating systems), v8.0 > End-to-end paths > Dynamic caching > Improve service with the dynamic cache service > Task overview: Using the dynamic cache service to improve performance
Configure command caching
Cacheable commands are stored in the cache for reuse with a similar mechanism for servlets and JSP files. In this case, however, the unique cache IDs are generated based on methods and fields present in the command as input parameters. For example, a GetStockQuote command can have a symbol as its input parameter.
A unique cache ID can generate from the name of the command, plus the value of the symbol.
To use command caching :
Procedure
Create a command.
- Define an interface. The Command interface specifies the most basic aspects of a command.
We must define the interface that extends one or more of the interfaces in the command package. The command package consists of three interfaces:
- TargetableCommand
- CompensableCommand
- CacheableCommand
In practice, most commands implement the TargetableCommand interface, which allows the command to run remotely. The code structure of a command interface for a targetable command follows:
... import com.ibm.websphere.command.*; public interface MyCommand extends TargetableCommand { // Declare application methods here }- Provide an implementation class for the interface. Write an interface that extends the CacheableCommandImpl class and implements your command interface. This class contains the code for the methods in your interface, the methods inherited from extended interfaces like the CacheableCommand interface, and the required or abstract methods in the CacheableCommandImpl class.
We can also override the default implementations of other methods provided in the CacheableCommandImpl class.
Avoid trouble: For command caching to operate properly, enable servlet caching.
Example
Cacheable commands are stored in the cache for reuse with a similar mechanism for servlets and JSP files.
The following code examples illustrate how to use command caching for a simple stock quote command.This code example illustrates a stock quote command bean. It accepts a ticker as an input parameter and produces a price as its output parameter:
public class QuoteCommand extends CacheableCommandImpl { private String ticker; private double price; // called to validate that command input parameters have been set public boolean isReadyToCallExecute() { return (ticker!=null); } // called by a cache-hit to copy output properties to this object public void setOutputProperties(TargetableCommand fromCommand) { QuoteCommand f = (QuoteCommand)fromCommand; this.price = f.price; } // business logic method called when the stock price must be retrieved public void performExecute()throws Exception {...} //input parameters for the command public void setTicker(String ticker) { this.ticker=ticker;} public String getTicker() { return ticker;} //output parameters for the command public double getPrice() { return price;}; }This code example illustrates how to use the cache policy to cache the stock quote command object using the stock ticker as the cache key and a 60-second time-to-live:
<cache> <cache-entry> <class>command </class> <sharing-policy>not-shared </sharing-policy> <name>QuoteCommand </name> <cache-id> <component type="method" id="getTicker"> <required>true </required> </component> <priority>3 </priority> <timeout>60 </timeout> </cache-id> </cache-entry> </cache>
Related
Command class
CacheableCommandImpl class
Example: Caching a command object
Task overview: Using the dynamic cache service to improve performanceDynamic cache service settings