InfoCenter Home >
6: Administer applications >
6.6: Tools and resources quick reference >
6.6.0: About user assistance >
6.6.0.4: Overview of editing property files by hand >
6.6.0.4.1: Dynamic caching of Servlets and JSPs >
6.6.0.4.1.4: Caching examples

6.6.0.4.1.4: Caching examples

  1. The first caching example describes a servlet that receives request parameters from CGI variables provided through either an HTML form or an applet.


    Servlet description

    The Calculator Servlet is defined within a tools Web application.
    It takes 2 arguments and one operation:
    • arg1
    • arg2
    • operation
    The values for these variables are provided through CGI variables from an external user,
    (also known as request parameters from a browser or applet).

    So to calculate 2+3, the servlet is invoked as:
    URI/tools/Calc?arg1="2"&arg2="3"&operation="+"

    Caching requirements

    The output of the servlet must be cached so that each set of results is distinquished from the previous set.

    Therefore, the results are distinguished by the request parameters.

    The request parameters of   2,3,+    must have one cache entry.

    The request parameters of   4,5,*   must have another cache entry.


    Definition in servletcache.xml


    <servlet>

    <path uri="/tools/Calc"/>

    <request>

    <parameter id="arg1" required="true"/>

    <parameter id="arg2" required="true"/>

    <parameter id="operation" required="true"/>

    </request>

    <timeout seconds="-1" />

    </servlet>

    Externally caching the output

    Since this servlet relies on request parameter variables (which are set externally by users), it is possible to externally cache the output of this servlet.

    Assume an external cache group named extcache is defined in the
    dynacache.xml file.

    You can now assign this servlet's output to the external cache using the <externalcache> element:


    <servlet>

    <path uri="/tools/Calc"/>

    <request>

    <parameter id="arg1" required="true"/>

    <parameter id="arg2" required="true"/>

    <parameter id="operation" required="true"/>

    </request>

    <timeout seconds="-1" />

    <externalcache id="extcache" />

    </servlet>



  2. The next caching example describes how to group and invalidate cache entries depending on the value of a variable.


    Servlet description

    The news servlet of class CoastalNewsServlet displays either west coast news or east coast news depending on the user's location.

    This servlet has the following attributes:

    • A session object named "location" of class LocationBean
    • LocationBean has a method getCoast() that returns "east" or "west"

    If the session object is not present on a session, the servlet will return the news for both coasts.



    Caching requirements

    All output from the servlet must be cached regardless if the session object, "location" is present on the session.

    Therefore, the output to be cached is:

    • east coast news if the getCoast() method returns "east"
    • west coast news if the getCoast() method returns "west"
    • news from both coasts if "location" is not present on the session

    Also, the entries cannot time out.



    Definition in servletcache.xml

    In this example, since the required parameter is not defined, it defaults to false which means "location" is not required, and the request still is cached.


    <servlet>

    <servletimpl class="CoastalNewsServlet"/>

    <session id="location" method="getCoast"/>

    <timeout seconds="-1" />

    </servlet>

    Grouping and invalidating cache entries

    Consider the design of the application when you want to group cache entries by coast, or invalidate all cache entries for a coast when the location bean is updated by a control servlet.

    If a variable is not available to a servlet at execution (i.e., the request/session variable has not been set), then, even if the servlet is cacheable, no group id is generated.

    Remember the behavior of the CoastalNewsServlet, where the missing session object, "location," causes the servlet to display the news for both coasts? In that case, you want the cache entry to belong to both the east and west coast groups. However, this will not occur because the cache does not handle data ids when variables are missing.

    To resolve this problem, mark the location bean as a required variable for caching. This means the output of the news servlet will not be cached if location is undefined. The location bean must be present to place entries into the correct groups.

    File, servletcache.xml, therefore, requires the following changes:

    1. The CoastalNewsServlet entry must be modified to put entries into groups
    2. A new entry for the control servlet must be added to allow invalidation of groups.
      (The control servlet updates the location bean.)

    Now when the news servlet is invoked, the cache takes the data_id "coast" and appends "=" and the value of location.getCoast() to create a group name to identify the entry.

    In the update servlet, a new_coast string is expected as a request attribute, and is used in the same way to build group names for removal from the cache.

    The new entries are highlighted.


    <servlet>

    <servletimpl class="CoastalNewsServlet"/>

    <session id="location" method="getCoast"/>

    <timeout seconds="-1" />

    </servlet>


    <servlet>

    <invalidateonly/>

    <servletimpl class="LocationUpdateServlet"/>

    <request>

    <attribute id="new_coast" invalidate="coast"/>

    </request>

    </servlet>

Notes:  
  • Invalidating has a higher performance cost than caching. Avoid using the same variable to invalidate a group and to group an entry. You will see less performance benefit than if you use the variable to define a group id.
  • Data_id and invalidate tags that are within the same <servlet> element should have different values. If they have the same value, the group is invalidated and the entry for the current servlet is put into that group. The invalidate tag that corresponds to a data_id occurs in different <servlet> elements.

Go to previous article: Removing entries from the cache Go to next article: Administering applications (overview)

 

 
Go to previous article: Removing entries from the cache Go to next article: Administering applications (overview)