Tutorials > Dynamic caching > Invalidate cache

< Previous | Next >


Invalidate 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.


Procedure

  1. Add a <cache-entry> value to cachespec.xml for the MiniShopCartDisplay.

    1. Open the cachespec.xml file located at WC_EAR\Stores.war\WEB-INF.

    2. 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 type="parameter">
                  <required>true</required>
              </component>
              <component type="attribute">
                  <required>false</required>
              </component>
              <component type="parameter">
                  <required>false</required>
              </component>
              <component type="attribute">
                  <required>false</required>
              </component>
              <priority>1</priority>
              <timeout>3600</timeout> 
          </cache-id>
          
          <dependency-id>storeId
          <component type="parameter">
          <required>true</required>
          </component>
          </dependency-id>
                 
          <dependency-id>DC_userId
          <component type="attribute">
          <required>false</required>
          </component>
          </dependency-id>
                 
          <dependency-id>MiniCart</dependency-id>
          
          <dependency-id>MiniCart:storeId
          <component type="parameter">
          <required>true</required>
          </component>
          </dependency-id>
                 
          <dependency-id>MiniCart:DC_userId
          <component type="attribute">
          <required>false</required>
          </component>
          </dependency-id>
                 
          <dependency-id>MiniCart:storeId:DC_userId
          <component type="parameter">
          <required>true</required>
          </component>
          <component type="attribute">
          <required>true</required>
          </component>
          </dependency-id>
      
      </cache-entry>
      

  2. Test the cachespec.xml.

    1. Use the cache monitor, view the cache policy to ensure the MiniShopCartDisplay is being cached.

    2. Hit the TopCategoriesDisplay page.

    3. Use the cache monitor to verify that there is an entry in the cache for the MiniShopCart.

    4. 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.

  3. 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.

    1. 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" >
                  <method>getStoreId</method>
                  <required>true</required>
              </component>
              <component type="method" >
                  <method>getUserId</method>
                  <required>true</required>
              </component>
          </invalidation>
      </cache-entry>
      

  4. Test that the invalidation rules are functioning correctly.

    1. Use the cache monitor to clear the cache entries.

    2. Hit the TopCategoriesDisplay page.

    3. 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:

< Previous | Next >


+

Search Tips   |   Advanced Search