Tutorial: Invalidating cache >
< Previous | Next >
Invalidating cache using command invalidation
Command-based invalidation means invalidation rules are executed upon execution of a command that extends from the WebSphere Commerce Command Framework API. Invalidation IDs for command-based invalidation are constructed based on methods and fields provided by the commands.
To allow a command call to be intercepted by the dynamic cache, the command must be written to the WebSphere Command Framework with its implementation class extending from CacheableCommandImpl (in the com.ibm.websphere.command package). To simplify command writing for command-based invalidation, WebSphere Commerce has updated the abstract classes, ControllerCommandImpl and TaskCommandImpl. They extend from CacheableCommandImpl so that any commands extend from these abstract classes would also extend from CacheableCommandImpl, and therefore, be eligible for command-based invalidation.
- Add a <cache-entry> value to cachespec.xml for the MiniShopCartDisplay.
- Open the cachespec.xml file located at WC_eardir\Stores.war\WEB-INF.
- Create a cache-entry for the MiniShopCartDisplay. Your completed cache-entry for MiniShopCartDisplay should look like this:
<cache-entry> <class>servlet</class> <name>/ConsumerDirect/include/MiniShopCartDisplay.jsp</name> <property name="save-attributes">false</property> <property name="do-not-consume">true</property> <property name="store-cookies">false</property> <property name="consume-subfragments">true</property> <cache-id> <component id="storeId" type="parameter"> <required>true</required> </component> <component id="DC_userId" type="attribute"> <required>false</required> </component> <component id="langId" type="parameter"> <required>false</required> </component> <component id="DC_curr" type="attribute"> <required>false</required> </component> <priority>1</priority> <timeout>3600</timeout> </cache-id> <dependency-id>storeId <component id="storeId" type="parameter"> <required>true</required> </component> </dependency-id> <dependency-id>DC_userId <component id="DC_userId" type="attribute"> <required>false</required> </component> </dependency-id> <dependency-id>MiniCart</dependency-id> <dependency-id>MiniCart:storeId <component id="storeId" type="parameter"> <required>true</required> </component> </dependency-id> <dependency-id>MiniCart:DC_userId <component id="DC_userId" type="attribute"> <required>false</required> </component> </dependency-id> <dependency-id>MiniCart:storeId:DC_userId <component id="storeId" type="parameter"> <required>true</required> </component> <component id="DC_userId" type="attribute"> <required>true</required> </component> </dependency-id> </cache-entry>
- Test the cachespec.xml.
- Use the cache monitor, view the cache policy to ensure the MiniShopCartDisplay is being cached.
- Hit the TopCategoriesDisplay page.
- Use the cache monitor to verify that there is an entry in the cache for the MiniShopCart.
- Add a product to the shopping cart. Return to the TopCatagoriesDisplay page. Notice that the shopping cart in the sidebar does not update. This is because the cache entry for the shopping cart is persisting, even though it should be invalidated.
- Now, add the invalidation rules for the shopping totals to ensure that the mini shop cart is invalidated at the correct time from the cache. This way, the shopping total will always be correct when users modify their cart.
- Copy the cache-entry for the Shopping Totals Invalidation rules to the cachespec.xml:
<cache-entry> <class>command</class> <sharing-policy>not-shared</sharing-policy> <name>com.ibm.commerce.orderitems.commands.OrderItemAddCmdImpl</name> <name>com.ibm.commerce.orderitems.commands.OrderItemDeleteCmdImpl</name> <name>com.ibm.commerce.order.commands.OrderCalculateCmdImpl</name> <name>com.ibm.commerce.orderitems.commands.OrderItemUpdateCmdImpl</name> <name>com.ibm.commerce.orderitems.commands.OrderItemMoveCmdImpl</name> <name>com.ibm.commerce.order.commands.OrderCancelCmdImpl</name> <name>com.ibm.commerce.usermanagement.commands.UserRegistrationAddCmdImpl</name> <name>com.ibm.commerce.usermanagement.commands.UserRegistrationUpdateCmdImpl</name> <name>com.ibm.commerce.order.commands.OrderProcessCmdImpl</name> <invalidation>MiniCart:storeId:DC_userId <component type="method" id="getCommandContext"> <method>getStoreId</method> <required>true</required> </component> <component type="method" id="getCommandContext"> <method>getUserId</method> <required>true</required> </component> </invalidation> </cache-entry>
- Test that the invalidation rules are functioning correctly.
- Use the cache monitor to clear the cache entries.
- Hit the TopCategoriesDisplay page.
- Add a product to the cart. Return to the TopCategoriesDisplay page. The cart will properly update to reflect the item that has been added.
When building the command-based invalidation polices in cachespec.xml, keep in mind the following restrictions:
- Only the methods invoked by the command that return the input instance variables can be used in the method component.
- You must provide and implement all the methods that are used to construct the invalidation IDs in the command interface.
- The methods you use in the method component must only return single value.
- You cannot use the request attributes component type.