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:
- The CoastalNewsServlet entry must be modified to put entries into
groups
- 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>
|