Example: Set the dynamic cache service


 

+

Search Tips   |   Advanced Search

 

This example puts all of the steps together for configuring the dynamic cache service with the cachespec.xml file, showing the use of...

Suppose that a servlet, newspage, manages a simple news site. The newspage servlet uses the query parameter "action", with values...

The newspage servlet supports an optional customized layout stored in the user's session using the attribute name "layout".

Here are sample URL requests to newspage:

To configure dynamic cache for the servlet newspage using cachespec.xml...

  1. Define the <cache-entry> element to identify the servlet...

    <cache-entry> 
        <name> /newspage </name>
        <class> servlet </class>  
    </cache-entry>
    

  2. Define cache ID generation rules.

    Cache ID components include...

    • Parameter "action" when the value equals "view".
    • Parameter category
    • Session layout (optional)

    The cache entry is now:

    <cache-entry> 
    
      <name> /newspage </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 servlet newspage, 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.

    Here is an example of the cache entry after adding the dependency ID...

    <cache-entry> 
    
      <name>newspage </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.

    Because a category dependency ID is already 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 only determine whether or not the invalidation ID creates and runs. The final cache-entry now looks like the following:

    <cache-entry> 
      <name>newspage </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>
      <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>
         </component>
      </invalidation>
    </cache-entry>
    




 

Related tasks


Task overview: Using the dynamic cache service to improve performance
Set servlet caching
Verifying the cacheable page