Tutorial: Building cache identifiers >
< Previous

 

Building cache identifiers using method calls into the CommandContext


It is possible to access parameters available in the CommandContext. Specifically, you can invoke methods on the CommandContext object so that certain values are accessed that would not be available.
To achieve this, set the type of the component to attribute, and in addition to the required child element, there is an additional child element called method. The method element identifies which method is invoked. You can have nested method tags. In this example, you will add a component to the cache-id of TopCategoriesDisplay that indicates if a user is a guest or is registered.

  1. Add a component for a session parameter, called CommandContext to the TopCategoriesDisplay page.

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

    2. Add the following component to the <cache-id> for the TopCategoriesDisplay page
      <component  id="CommandContext" type="attribute">
              <method>getUser
              <method>getRegisterType</method>
              </method>
              <required>true</required>
              </component>
      
      
      

    3. Use the cache monitor, look at the cache policies to verify that CommandContext is a required component of the cache-id.

  2. Test the cachespec.xml as a guest user.

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

    2. Hit the TopCategoriesDisplay page.

    3. Look at the cached entries in the cache monitor. You should see a single entry for TopCategoriesDisplay, with CommandContext=G.

  3. Test the cachespec.xml as a registered user.

    1. Register a user for the store.

    2. Visit the TopCategoriesDisplay page.

    3. Look at the cached entries in the cache monitor. You now see two entries for TopCategoriesDisplay, each with a different value for the CommandContext component.

      Close up of the two values for Command Context

  4. Reset your cachespec.xml.

    1. Remove the CommandContext component from cachespec.xml. An important CommandContext method is isSecure(). It is important to cache separate entries for the browse pages in SSL and non-SSL to ensure that the links are in the correct format (for example, SSL pages link to other SSL page and vice versa). If you don't do this, then it's possible the links on an SSL will be to non-SSL pages, thus causing a popup warning.

  5. Add a component for a session parameter, called isSecure to the TopCategoriesDisplay page.

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

    2. Add the following component to the <cache-id> for the TopCategoriesDisplay page
      <component  id="CommandContext" type="attribute">
              <method>isSecure
              </method>
              <required>true</required>
      </component>
      
      
      

    3. Use the cache monitor, look at the cache policies to verify that isSecure is a required component of the cache-id.

  6. Test the cachespec.xml.

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

    2. Hit the TopCategoriesDisplay page using http.

    3. Hit the TopCategoriesDisplay page using https.

    4. Look at the cached entries in the cache monitor. You see two entries for TopCategoriesDisplay.


Tutorial Summary In this tutorial, you learned about using method calls into the CommandContext for building cache-entries:
< Previous