home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / eventcache.xsp < prev    next >
Encoding:
Extensible Markup Language  |  2004-07-12  |  7.7 KB  |  169 lines

  1. <?xml version="1.0"?>
  2. <!--
  3.   Copyright 1999-2004 The Apache Software Foundation
  4.  
  5.   Licensed under the Apache License, Version 2.0 (the "License");
  6.   you may not use this file except in compliance with the License.
  7.   You may obtain a copy of the License at
  8.  
  9.       http://www.apache.org/licenses/LICENSE-2.0
  10.  
  11.   Unless required by applicable law or agreed to in writing, software
  12.   distributed under the License is distributed on an "AS IS" BASIS,
  13.   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.   See the License for the specific language governing permissions and
  15.   limitations under the License.
  16. -->
  17.  
  18. <!--+
  19.     | XSP event-based cache sample.
  20.     |
  21.     | CVS $Id: eventcache.xsp,v 1.6 2004/04/05 12:25:34 antonio Exp $
  22.     +-->
  23.  
  24. <xsp:page language="java"
  25.           xmlns:xsp="http://apache.org/xsp"
  26.           xmlns:xsp-request="http://apache.org/xsp/request/2.0">
  27.  
  28. <xsp:structure>
  29.     <xsp:include>org.apache.excalibur.source.SourceValidity</xsp:include>
  30.     <xsp:include>org.apache.cocoon.caching.validity.EventValidity</xsp:include>
  31.     <xsp:include>org.apache.cocoon.caching.validity.NamedEvent</xsp:include>
  32.     <xsp:include>java.io.Serializable</xsp:include>
  33. </xsp:structure>
  34.  
  35. <xsp:logic>
  36.     // artificial slowdown to make the effects of the cache visible
  37.     final int DELAY_SECS = 2;
  38.  
  39.     /**
  40.      * Generate the unique key for the cache.
  41.      *
  42.      * This key must be unique inside the space of this XSP page, it is used
  43.      * to find the page contents in the cache (if getValidity says that the
  44.      * contents are still valid).
  45.      *
  46.      * This method will be invoked before the getValidity() method.
  47.      *
  48.      * @return The generated key or null if the component
  49.      *         is currently not cacheable.
  50.      */
  51.     public Serializable getKey()
  52.     {
  53.        // for our test, pages having the same value of "pageKey" will share
  54.        // the same cache location
  55.        String key = request.getParameter("pageKey") ;
  56.        return ((key==null||"".equals(key)) ? "one" : key);
  57.     }
  58.  
  59.     /**
  60.      * Generate the validity object, tells the cache how long to
  61.      * keep contents having this key around.  In this case, it will 
  62.      * be until an Event is retrieved matching the NamedEvent created below.
  63.      *
  64.      * Before this method can be invoked the getKey() method
  65.      * will be invoked.
  66.      *
  67.      * @return The generated validity object or null if the
  68.      *         component is currently not cacheable.
  69.      */
  70.     public SourceValidity getValidity() {
  71.        String key = request.getParameter("pageKey") ;
  72.        return new EventValidity(
  73.                    new NamedEvent(
  74.                        (key==null||"".equals(key)) ? "one" : key));
  75.     }
  76.  </xsp:logic>
  77.  
  78.  
  79.   <page>
  80.     <title>Demonstrating Event-Aware Caching</title>
  81.     <content>
  82.         <para>
  83.             This xsp page is based on (copied from) the cacheable xsp sample 
  84.             but there are some important differences.  If you don't already 
  85.             understand at least the basics of caching in Cocoon, you should 
  86.             probably start there, not here.  Read the text below, and the 
  87.             sitemap and source for more details.
  88.         </para>
  89.         <para>
  90.             I pause for <xsp:expr>DELAY_SECS</xsp:expr> seconds during generation, so 
  91.             that you can tell if I'm being served from the cache or not.
  92.             <br/>
  93.             What you see here was generated on <b><xsp:expr>new java.util.Date()</xsp:expr></b>.
  94.         </para>
  95.  
  96.         <para>
  97.             I'm cached for each unique value of request parameter 'pageKey'.  Other 
  98.             parameters do not matter.
  99.             <br/>
  100.             Here the value is:
  101.             <b><xsp-request:get-parameter name="pageKey"/></b>.  
  102.             <br/>
  103.             If this is not the same as the 'pageKey' parameter in the page URL, we have a problem.
  104.         </para>
  105.  
  106.         <para>
  107.             Unlike other cacheable pages in Cocoon, I can be un-cached by events external 
  108.             to Cocoon - for instance, when a database table or row is updated.
  109.             <br/>
  110.             My cache entry will be invalidated (actually, removed) when an event named 
  111.             <i><xsp-request:get-parameter name="pageKey"/></i> occurs.  This can be manually 
  112.             simulated by clicking one of the "uncache" links below.
  113.         </para>
  114.         <para>Test links:
  115.             <ul>
  116.                 <li><a href="?pageKey=one">pageKey=one</a> 
  117.                     (<a href="action?pageKey=one&event=one">uncache with action</a>) 
  118.                     (<a href="flow?pageKey=one&event=one">uncache with flow</a>)</li>
  119.                 <li><a href="?pageKey=two">pageKey=two</a>
  120.                     (<a href="action?pageKey=two&event=two">uncache with action</a>) 
  121.                     (<a href="flow?pageKey=two&event=two">uncache with flow</a>)</li>
  122.             </ul>
  123.             Note: the random numbers you see included in the url after an uncache link 
  124.             serve two purposes in the example, making it easier to see the effect of the 
  125.             cache invalidation.  They prevent browser caching and they demonstrate that 
  126.             only our designated key matters in the retrieval from cache.  
  127.         </para>
  128.         <para>
  129.             This event based cache system consists essentially of three parts:
  130.             <ul>
  131.                 <li>A new type of SourceValidity, EventValidity, which contains information 
  132.                 on the Event which will invalidate this cached content.  Until this event is 
  133.                 received, EventValidities will usually always return valid, though they don't 
  134.                 have to.</li>
  135.                 <li>An extension to Cocoon's Cache implementation.  Cocoon's Cache is really just 
  136.                 a thin wrapper around Avalon-Excalibur's Store project.  The EventAwareCacheImpl 
  137.                 does two things.  It examines each pipeline on its way into the cache to 
  138.                 determine if any of its SourceValidities are instances of EventValidity.  If so, 
  139.                 it notifies an event registry as described next.  The second critical function of 
  140.                 the EventAware cache implementation is that it allows other components to 
  141.                 contact it and notify it of an Event.  The Cache then looks up the keys 
  142.                 mapped to that event in the event registry and cleans out the cache and 
  143.                 registry accordingly.  <i>See the sitemap of this sample for an example of 
  144.                 configuring a pipeline to use this implementation.</i></li>
  145.                 <li>The EventRegistry is responsible for mapping Events to cache keys, and 
  146.                 providing information about that mapping to systems that need it, usually just 
  147.                 the EventAwareCache.  Another crucial responsibility of the EventRegistry is to 
  148.                 persist its data across container shutdown and startup.  The default implementation 
  149.                 does by serializing an object to disk (currently in WEB-INF).  If recovering this 
  150.                 fails, the EventAwareCache is notified, and it is expected to ensure there are no 
  151.                 orphaned EventValidities (currently by clearing the entire cache).
  152.                 </li>
  153.             </ul>
  154.             Note that though this example uses xsp with actions or flow, any pipeline component can be 
  155.             made to use EventValidity, and any code with access to the ComponentManager can 
  156.             translate real-world events to Events and notify the Cache of them.  
  157.         </para>
  158.         <xsp:logic>
  159.           // slowdown page generation.
  160.             try {
  161.               Thread.sleep(DELAY_SECS * 1000L);
  162.             } catch (InterruptedException ie) {
  163.               // Not much that can be done...
  164.             }
  165.         </xsp:logic>
  166.     </content>
  167.   </page>
  168. </xsp:page>
  169.