org.apache.excalibur.source.SourceValidity org.apache.excalibur.source.impl.validity.AggregatedValidity org.apache.excalibur.source.impl.validity.TimeStampValidity org.apache.cocoon.caching.validity.EventValidity org.apache.cocoon.caching.validity.NamedEvent java.io.Serializable // artificial slowdown to make the effects of the cache visible final int DELAY_SECS = 2; final String DATABASE_TABLE = "user"; /** * Generate the unique key for the cache. * * This key must be unique inside the space of this XSP page, it is used * to find the page contents in the cache (if getValidity says that the * contents are still valid). * * This method will be invoked before the getValidity() method. * * @return The generated key or null if the component * is currently not cacheable. */ public Serializable getKey() { // this page will only render a single view // depending on the database table. Therefore // there is no need to distinguish different // outcomes in the cache. If we were paging // through the result set, those parameters // should be used for the key. return ""; } /** * Generate the validity object, tells the cache how long to * keep contents having this key around. In this case, it will * be until an Event is retrieved matching the NamedEvent created below. * * Before this method can be invoked the getKey() method * will be invoked. * * @return The generated validity object or null if the * component is currently not cacheable. */ public SourceValidity getValidity() { String key = DATABASE_TABLE; // a composite validity is required here since changes to // the source XSP file should invalid the cache as well as // changes to the database table. AggregatedValidity validity = new AggregatedValidity(); // invalidate on external events // multiple events can be added, the cache will be invalidated // when any of those events occur. validity.add(new EventValidity(new NamedEvent(key))); // invalidate on changes to XSP source validity.add(new TimeStampValidity(this.dateCreated)); return validity; } Demonstrating Event-Aware Caching or Database Generated Pages. This xsp page is based on (copied from) the event aware cacheable xsp sample. I pause for DELAY_SECS 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 new java.util.Date().
I'm cached for a specific database table name: DATABASE_TABLE. Other parameters ie. request parameters do not matter. 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. I will also be invalidated when the XSP source is modified.
My cache entry will be invalidated (actually, removed) when an event named DATABASE_TABLE occurs. Go to the database samples and modify, insert, or delete a user and check if this page is updated.
Test links: personnel select * from user order by name, firstname, uname, uid
Please refer to the event based cache example for more details on the event based cache invalidation. // slowdown page generation. try { Thread.sleep(DELAY_SECS * 1000L); } catch (InterruptedException ie) { // Not much that can be done... }