Enabling command result caching
Cacheable commands are stored in the cache for re-use with a similar mechanism as used for servlets and Java Server Pages (JSP) files. However, in this case, the unique cache IDs are generated based on methods and fields present in the command as input parameters.
1. Developers need to implement the following steps during commands development: a. Create a command and define an interface. The Command interface specifies the most basic aspects of a command.
You 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 execute remotely. The code structure of a command interface for a targetable command is shown in Example 14-5:
Example 14-5 TargetableCommand interface
...import com.ibm.websphere.command.*;public interface MyCommand extends TargetableCommand {// Declare application methods here}
b. Provide an implementation class for the interface. Write a class 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.
You can also override the default implementations of other methods provided in the CacheableCommandImpl class.
2. cachespec.xml must be configured. The Trade3 home page uses the following commands. The command results (raw data) can be cached:
Market summary command
Account command
Closed order command
Holdings command The MarketSummaryCommand is cached and invalidated on a timeout or by ResetTradeCommand, which uses the MarketSummary dependency ID as shown in Example 14-6.
Example 14-6 Cache entry for MarketSummaryCommand
<cache-entry><class>command</class><sharing-policy>not-shared</sharing-policy><name>com.ibm.websphere.samples.trade.command.MarketSummaryCommand</name><cache-id><priority>3</priority><timeout>10</timeout></cache-id><dependency-id>MarketSummary</dependency-id></cache-entry>}
The AccountCommand is used to cache a user's account information. AccountCommands are invalidated for each individual user when that users' account information, such as their account balance, are updated by a trading operation. This is explained in Example 14-7:
Example 14-7 Cache entry for AccountCommand
<cache-entry><class>command</class><sharing-policy>not-shared</sharing-policy><name>com.ibm.websphere.samples.trade.command.AccountCommand</name><cache-id><component type="method" id="getUserID"><required>true</required></component><priority>3</priority></cache-id><dependency-id>Account_UserID<component id="getUserID" type="method"><required>true</required></component></dependency-id><dependency-id>AllUsers</dependency-id></cache-entry>
The OrderCompletedCommand signifies that a buy or a sell order has completed for an individual user. When an order completes, a user's holdings and account balance have been modified. This invalidates the AccountCommand, HoldingsCommand and OrdersCommand for that user. See Example 14-8.
Example 14-8 Cache entry for OrderCompletedCommand
<cache-entry><class>command</class><sharing-policy>not-shared</sharing-policy><name>com.ibm.websphere.samples.trade.command.OrderCompletedCommand</name><invalidation>Account_UserID<component id="getUserID" type="method"><required>true</required></component></invalidation><invalidation>Holdings_UserID<component id="getUserID" type="method"><required>true</required></component></invalidation><invalidation>Orders_UserID<component id="getUserID" type="method"><required>true</required></component></invalidation><invalidation>ClosedOrders_UserID<component id="getUserID" type="method"><required>true</required></component></invalidation></cache-entry>
The HoldingsCommand is used to cache a users' stock holdings information. HoldingsCommands are invalidated for each individual user when that user's holdings change due to an OrderCompletedCommand. It uses the Holdings_UserID dependency ID to identify user Accounts by the userID. It also uses the AllUsers dependency ID to identify all user Accounts for commands which invalidate all Account Holdings cache entries. Refer to Example 14-9.
Example 14-9 Cache entry for HoldingsCommand
<cache-entry><class>command</class><sharing-policy>not-shared</sharing-policy><name>com.ibm.websphere.samples.trade.command.HoldingsCommand</name><cache-id><component type="method" id="getUserID"><required>true</required></component><priority>3</priority></cache-id><dependency-id>Holdings_UserID<component id="getUserID" type="method"><required>true</required></component></dependency-id><dependency-id>AllUsers</dependency-id></cache-entry>
Prev | Home | Next WebSphere is a trademark of the IBM Corporation in the United States, other countries, or both.
IBM is a trademark of the IBM Corporation in the United States, other countries, or both.