Tutorials > Dynamic caching > Accept cache identifiers
Accept 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, we will add a component to the cache-id of TopCategoriesDisplay that indicates if a user is a guest or is registered.
Procedure
- Add a component for a session parameter, called CommandContext to the TopCategoriesDisplay page.
- Open the cachespec.xml file located at WC_EAR\Stores.war\WEB-INF.
- Add the following component to the <cache-id> for the TopCategoriesDisplay page:
<component type="attribute"> <method>getUser <method>getRegisterType</method> </method> <required>true</required> </component>
- Use the cache monitor, look at the cache policies to verify that CommandContext is a required component of the cache-id.
- Test the cachespec.xml as a guest user.
- Use the cache monitor to clear the entries from the cache.
- Hit the TopCategoriesDisplay page.
- Look at the cached entries in the cache monitor. You should see a single entry for TopCategoriesDisplay, with CommandContext=G.
- Test the cachespec.xml as a registered user.
- Register a user for the store.
- Visit the TopCategoriesDisplay page.
- 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
- Reset the cachespec.xml.
- 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.
- Add a component for a session parameter, called isSecure to the TopCategoriesDisplay page.
- Open the cachespec.xml file located at WC_EAR\Stores.war\WEB-INF.
- Add the following component to the <cache-id> for the TopCategoriesDisplay page:
<component type="attribute"> <method>isSecure </method> <required>true</required> </component>
- Use the cache monitor, look at the cache policies to verify that isSecure is a required component of the cache-id.
- Test the cachespec.xml.
- Use the cache monitor to clear the entries from the cache.
- Hit the TopCategoriesDisplay page using http.
- Hit the TopCategoriesDisplay page using https.
- 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: