This page is a sample for event aware caching. We subclassed the
JXTG template generate for this sample. If you don't already
understand at least the basics of caching in Cocoon, you should
probably start there, not here. Read the text below, and the
sitemap and source for more details.
I pause for 4 seconds during generation, so
that you can tell if I'm being served from the cache or not.
What you see here was generated on Fri, 12 Aug 2022 12:51:42 -0500.
I'm cached for each unique value of request parameter 'pageKey'. Other
parameters do not matter.
Here the value is:
If this is not the same as the 'pageKey' parameter in the page URL, we have a problem.
Unlike other cacheable pages in Cocoon, I can be un-cached by events external
to Cocoon - for instance, when a database table or row is updated.
My cache entry will be invalidated (actually, removed) when an event named
one occurs. This can be manually
simulated by clicking one of the "uncache" links below.
Note: the random numbers you see included in the url after an uncache link
serve two purposes in the example, making it easier to see the effect of the
cache invalidation. They prevent browser caching and they demonstrate that
only our designated key matters in the retrieval from cache.
This event based cache system consists essentially of three parts:
Note that though this example extends the jxtg in combination with actions or flow,
any pipeline component can be
made to use EventValidity, and any code with access to the ServiceManager can
translate real-world events to Events and notify the Cache of them.
- A new type of SourceValidity, EventValidity, which contains information
on the Event which will invalidate this cached content. Until this event is
received, EventValidities will usually always return valid, though they don't
- An extension to Cocoon's Cache implementation. Cocoon's Cache is really just
a thin wrapper around Avalon-Excalibur's Store project. The EventAwareCacheImpl
does two things. It examines each pipeline on its way into the cache to
determine if any of its SourceValidities are instances of EventValidity. If so,
it notifies an event registry as described next. The second critical function of
the EventAware cache implementation is that it allows other components to
contact it and notify it of an Event. The Cache then looks up the keys
mapped to that event in the event registry and cleans out the cache and
registry accordingly. See the sitemap of this sample for an example of
configuring a pipeline to use this implementation.
- The EventRegistry is responsible for mapping Events to cache keys, and
providing information about that mapping to systems that need it, usually just
the EventAwareCache. Another crucial responsibility of the EventRegistry is to
persist its data across container shutdown and startup. The default implementation
does by serializing an object to disk (currently in WEB-INF). If recovering this
fails, the EventAwareCache is notified, and it is expected to ensure there are no
orphaned EventValidities (currently by clearing the entire cache).