Example: Configure the dynamic cache using the cachespec.xml file

This example puts all the steps together for configuring the dynamic cache with the cachespec.xml file and shows the use of the cache ID generation rules, dependency IDs, and invalidation rules.

Suppose we have a servlet which is used to manage a simple news site. This servlet uses the query parameter "action" to determine if the request is being used to "view" news or "update" news (used by the administrator). Another query parameter "category" is used to select the news category. Also, this site supports an optional customized layout, which is stored in the user's session and uses the attribute name "layout". Here are example URL requests to this servlet:

http://yourhost/yourwebapp/newscontroller?action=view&category;=sports (Returns a news page for the sports category)

http://yourhost/yourwebapp/newscontroller?action=view&category=money (Returns a news page for the money category)

http://yourhost/yourwebapp/newscontroller?action=update&category=fashion (Allows the administrator to update news in the fashion category)

To configure dynamic cache using the cachespec.xml file (using the example variables), perform the following steps:

  1. Define the cache-entry elements necessary to identify the servlet. In this case, the servlet's URI is "newscontroller" so this is the cache-entry's name element. Also, because a servlet/JavaServer Page (JSP) is cached, the cache-entry class is "servlet".
    <cache-entry>
      <name> /newscontroller </name>
      <class>servlet  </class>  
    </cache-entry>
    
  2. Define cache ID generation rules. For this servlet, only cache when action=view. One component of the cache ID is the parameter "action" when the value equals "view". The news category is also an essential part of the cache ID. Finally, the optional session attribute for the user's layout is included in the cache ID. The cache-entry now looks like this:
    <cache-entry>
      <name> /newscontroller </name>
      <class>servlet  </class>
        <cache-id>
          <component id="action" type="parameter">
            <value>view</value>
            <required>true</required>
          </component>
          <component id="category" type="parameter">
            <required>true</required>
          </component>
          <component id="layout" type="session">
            <required>false</required>
          </component>
      </cache-id>
    </cache-entry>
    
  3. Define dependency ID rules. For this servlet, a dependency ID is added for the category. Later, when the category is invalidated due to an update event, all views of that news category are invalidated. After adding the dependency-id, the cache-entry now looks like this:
    <cache-entry>
      <name> /newscontroller </name>
      <class>servlet  </class>
      <cache-id>
        <component id="action" type="parameter">
          <value>view</value>
          <required>true</required>
        </component>
        <component id="category" type="parameter">
          <required>true</required>
        </component>
        <component id="layout" type="session">
          <required>false</required>
        </component>
      </cache-id>
      <dependency-id>category
        <component id="category" type="parameter">
          <required>true</required>
        </component>
      </dependency-id>
    </cache-entry> 
    
  4. Define invalidation rules. Since a category dependency ID was defined, define an invalidation rule to invalidate the category when action=update. To incorporate the conditional logic, add "ignore-value" components into the invalidation rule. These components do not add to the output of the invalidation ID, but they determine if the invalidation ID is created and executed. The final cache-entry now looks like this:
    <cache-entry>
      <name> /newscontroller </name>
        <component id="action" type="parameter">
          <value>view</value>
          <required>true</required>
        </component>
        <component id="category" type="parameter">
          <required>true</required>
        </component>
        <component id="layout" type="session">
          <required>false</required>
        </component>
      </cache-id>
      <dependency-id>category
        <component id="category" type="parameter">
          <required>true</required>
        </component
      </dependency-id
      <invalidation>category<
        <component id="action" type="parameter" ignore-value="true">
          <value>update</value>
          <required>true</required>
        </component>
        <component id="category" type="parameter">
          <required>true</required>
      </invalidation>
    </cache-entry>