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 / caching.xml < prev    next >
Encoding:
Extensible Markup Language  |  2004-07-12  |  16.7 KB  |  321 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  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. <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V1.0//EN" "document-v10.dtd">
  18.  
  19. <document>
  20.   <header>
  21.      <title>Caching</title>
  22.      <version>0.9</version>
  23.      <type>Technical document</type>
  24.      <authors><person name="Carsten Ziegeler" email="cziegeler@apache.org"/>
  25.      </authors>
  26.      <abstract>This document explains the basic caching algorithm of Apache Cocoon.</abstract>
  27.   </header>
  28.   <body>
  29.      <s1 title="Goal">
  30.         <p>This document explains the basic caching algorithm of Apache Cocoon.</p>
  31.      </s1>
  32.      <s1 title="Overview">
  33.         <p>The caching algorithm of Cocoon has a very flexible and powerful design.
  34.            The algorithms and components used are not hard coded into the core of 
  35.            Cocoon. They can be configured in the sitemap.</p>
  36.             <p>This document describes the components available for caching,
  37.                how they can be configured and how to implement your own cacheable components.
  38.             </p>
  39.      </s1>
  40.      <s1 title="How to Configure Caching">
  41.          <p>The caching can be turned on and off on a per pipeline setting in the sitemap.
  42.           This means, for each <em>map:pipeline</em> section in a sitemap, it's possible to
  43.           turn on/off caching and configure the caching algorithm.</p>
  44.           <p>The following example shows how to turn on caching for a pipeline:</p>
  45.     <source>
  46.      <![CDATA[
  47.        <map:pipeline type="caching">
  48.           ...
  49.        </map:pipeline>
  50.      ]]>
  51.     </source> 
  52.           <p>If you know that it doesn't make sense to turn on caching for some of 
  53.           your pipelines, put them together in their own section and use:</p>
  54.     <source>
  55.      <![CDATA[
  56.        <map:pipeline type="noncaching">
  57.           ...
  58.        </map:pipeline>
  59.      ]]>
  60.     </source>
  61.       <p>As you might guess from how the caching is turned on (via a type attribute), you
  62.          can have different caching (or better pipeline) implementation to choose from. This
  63.          is similar to choose from a set of generators the generator to use in your pipeline etc.
  64.          You will find in your main sitemap a section declaring all pipeline implementations.
  65.          It's in the <em>map:components</em> section:
  66.       </p>
  67.     <source>
  68.      <![CDATA[
  69.        <map:pipes default="caching">
  70.           <map:pipe name="caching" src="..."/>
  71.           <map:pipe name="noncaching" src="..."/>
  72.        </map:pipes>
  73.      ]]>
  74.     </source>
  75.       <p>Depending on your Cocoon installation you might have different implementations in
  76.       that section. As with all components, you can define a default for all pipelines and
  77.       override this whereever it makes sense.</p>
  78.      </s1>
  79.      <s1 title="The Default Caching Algorithm">
  80.         <p>The default algorithm uses a very easy but effective approach
  81.            to cache a request: The pipeline process is cached up to the most 
  82.            possible point.</p>
  83.         <p>Therefore each component in the pipeline is queried by Cocoon if it
  84.         supports caching. Several components, like the file generator or the xslt
  85.         transformer support caching. However, dynamic components like the sql transformer
  86.         or the cinclude transformer do not. Let's have a look at some examples:</p>
  87.         <s2 title="Simple Examples">
  88.           <p>If you have the following pipeline:</p>
  89.           <p>Generator[type=file|src=a.xml] -> Transformer[type="xslt"|src=a.xsl] -> Serializer</p>
  90.           <p>The file generator is cacheable and generates a key which uses the src 
  91.              (or the filename) to build the key. The cache uses the last modification date of the xml file
  92.              to test if the cached content is valid.</p>
  93.             <p>The xslt transformer is cacheable and generates a key which uses
  94.              the filename to build the unique key. The cache validity object
  95.              uses the last modification date of the xslt file.</p>
  96.           <p>The default serializer (html) supports the caching as well.</p>
  97.             <p>All three keys are used to build a unique key for this pipeline.
  98.              The first time it is invoked its response is cached. The second time
  99.              this pipeline is called, the cached content is get from the cache.
  100.              If it is still valid, the cached content is directly send to the client.</p>
  101.         </s2>
  102.         <s2 title="Complex Example">
  103.            <p>Only part of the following pipeline is cached:</p>
  104.            <p>Generator[type=file|src=a.xml] -> Transformer[type="xslt"|src=a.xsl] -> Transformer[type=sql] -> Transformer[type="xslt"|src=b.xsl] -> Serializer</p>
  105.            <p>The file generator is cacheable and generates a key which uses the src 
  106.              (or the filename) to build the key. The cache uses the last modification date of the xml file
  107.              to test if the cached content is valid.</p>
  108.              <p>The xslt transformer is cacheable and generates a key which uses
  109.              the filename to build the unique key. The cache validity object
  110.              uses the last modification date of the xslt file.</p>
  111.            <p>The sql transformer is not cacheable, so the caching algorithm stops
  112.                 at this point although the last transformer is cacheable again.</p>
  113.            <p>The cached response is the output of the first xslt transformer, so when the
  114.              next request comes in and the cached content is valid, the cached content is
  115.              directly feed into the sql transformer. The generator and the first
  116.              xslt transformer are not executed.</p>
  117.         </s2>
  118.         <s2 title="Making Components Cacheable">
  119.           <p>This chapter is only for developers of own sitemap components. It details what you have
  120.             to do when you want that your own sitemap components supports the caching.</p>
  121.           <p>Each sitemap component (generator or transformer) which might be
  122.             cacheable must implement the CacheableProcessingComponent interface. When the
  123.             pipeline is processed each sitemap component starting with
  124.            the generator is asked if it implements this interface. This
  125.            test stops either when the first component does not implement
  126.            the CacheableProcessingComponent interface or when the first cacheable component is
  127.            currently not cacheable for any reasons (more about this in a moment).</p>
  128.           <p>The CacheableProcessingComponent interface declares a method <code>getKey()</code>
  129.            which must produce a unique key for this sitemap component inside
  130.            the component space. For example the FileGenerator returns the
  131.            source argument (the xml document read). All parameters/values
  132.            which are used for the processing of the request by the generator must
  133.            be used for this key. If, e.g. the request parameters are used by
  134.            the component, it must build a key with respect to the current request
  135.            parameters. The key can be any serializable java object.</p>
  136.          <p>If for any reason the sitemap component detects that the current request
  137.            is not cacheable it can simply return <code>null</code> as the key. This has
  138.           the same effect as not declaring the CacheableProcessingComponent interface.</p>
  139.         <p>Now after the key is build for this particular request, it is looked up
  140.            in the cache if it exists. If not, the new request is generated and cached
  141.            for further requests.</p>
  142.          <p>If a cached response is found for the key, the caching algorithm checks
  143.            if this response is still valid. For this check each cacheable component
  144.            returns a validity object when the method <code>getValidity</code>
  145.            is invoked. (If a cacheable component returns <code>null</code> it
  146.            is temporarily not cacheable, like returning <code>null</code> for the key.)</p>
  147.            <p>A <code>SourceValidity</code> object contains all information the component
  148.            needs to verify if the cached content is still valid. For example the
  149.            file generator stores the last modification date of the xml document parsed
  150.            in the validity object.</p>
  151.           <p>When a response is cached all validity objects are stored together with
  152.             the cached response in the cache. Actually the <code>CachedResponse</code>
  153.             is stored which encapsulates all this information.</p>
  154.           <p>When a new response is generated and the key is build, the caching
  155.            algorithm also collects all uptodate cache validity objects. So if the
  156.            cached response is found in the cache these validity objects are compared.
  157.            If they are valid (or equal) the cached response is used and feed into
  158.            the pipeline. If they are not valid any more the cached response is removed
  159.            from the cache, the new response is generated and then stored together with
  160.            the new validity objects in the cache.</p>
  161.         </s2>
  162.      </s1>
  163.      <s1 title="Configuration">
  164.         <p>The caching of Cocoon can be completely configured by different Avalon
  165.                components. This chapter describes how the various components work
  166.                together.</p>            
  167.         <s2 title="Configuration of Pipelines">
  168.             <p>Each pipeline can be configured with a buffer size, and each
  169.               caching pipeline with the name of the Cache to use.</p>
  170.          <s3 title="Expiration of Content">
  171.          <p>
  172.          Utilize the pipeline <code>expires</code> parameter to dramatically reduce
  173.          redundand requests. Even the most dynamic application pages have a 
  174.          reasonable period of time during which they are static. 
  175.          Even if a page doesn't change for just one minute, still use the 
  176.          <code>expires</code> parameter. Here is an example:
  177.          </p>
  178. <source><![CDATA[
  179. <map:pipeline>
  180.   <map:parameter name="expires" value="access plus 1 minutes"/>
  181.   ...
  182. </map:pipeline> 
  183. ]]></source>
  184.         <p>
  185.          The value of the parameter is in a format borrowed from the Apache HTTP module mod_expires.
  186.          Examples of other possible values are:
  187.          </p>
  188. <source><![CDATA[
  189. access plus 1 hours
  190. access plus 1 month
  191. access plus 4 weeks
  192. access plus 30 days
  193. access plus 1 month 15 days 2 hours
  194. ]]></source>
  195.          <p>
  196.          Imagine 1'000 users hitting your web site at the same time.
  197.          Say that they are split into 5 groups, each of which has the same ISP.
  198.          Most ISPs use intermediate proxy servers to reduce traffic, hense
  199.          improving their end user experience and also reducing their operating costs.
  200.          In our case the 1'000 end user requests will result in just 5 requests to Cocoon.
  201.          </p>
  202.          <p>
  203.          After the first request from each group reaches the server, the expires header will
  204.          be recognized by the proxy servers which will serve the following requests from their cache.
  205.          Keep in mind however that most proxies cache HTTP GET requests, but will not cache HTTP POST requests.
  206.          </p>
  207.          <p>
  208.          To feel the difference, set an expires parameter on one of your pipelines and
  209.          load the page with the browser. Notice that after the first time, there are no 
  210.          access records in the server logs until the specified time expires.
  211.          </p>
  212.          <p>This parameter has effect on all pipeline implementations, even on 
  213.          the non caching ones. Remember, the caching does not take place in Cocoon,
  214.          it's either in a proxy inbetween Cocoon and the client or in the client
  215.          itself.</p>
  216.          </s3>
  217.          <s3 title="Response Buffering">
  218.           <p>Each pipeline can buffer the response, before it is send to the client.
  219.             The default buffer size is unlimited (-1), which means when all bytes of
  220.             the response are available on the server, they are send with one
  221.             command directly to the client.</p>
  222.           <p>Of course, this slows down the response as the whole response
  223.             is first buffered inside Cocoon and then send to the client instead of
  224.             directly sending the parts of the response when they are available.
  225.             But on the other hand this is very important for error handling. If you
  226.             don't buffer the response and an error occurs, you might get corrupt
  227.             pages. Example: you have a pipeline that already send some content
  228.             to the client and now an exception occurs. This exception "calls"
  229.             the error handler that generates a new response that is appended
  230.             to the already send content. If content is already send to the client
  231.             there is no way of reverting this! So buffering in these cases makes
  232.             sense.
  233.            </p>
  234.            <p>If you have a stable application running in production where the
  235.            error handler is never invoked, you can turn off the buffering, by
  236.            setting the buffer to <em>0</em>.</p>
  237.            <p>You can set the buffer to any other value higher than 0 which means
  238.            the content of the response is buffered in Cocoon until the buffer is
  239.            full. If the buffer is full it's flushed and the next part of the
  240.            response is buffered again. If you know the maximum size of your
  241.            content than you can fine tune the buffer handling with this.</p>
  242.            <p>You can set the default buffer size for each pipeline implementation
  243.             at the declaration of the pipeline. Example:</p>
  244.     <source>
  245.      <![CDATA[
  246.         <map:pipe name="noncaching" src="...">
  247.             <parameter name="outputBufferSize" value="2048"/>
  248.         </map:pipe>
  249.      ]]>
  250.     </source>
  251.           <p>The above configuration sets the buffer size to <em>2048</em> for the
  252.           non caching pipeline. Please note, that the parameter element does not
  253.           have the sitemap namespace!</p>
  254.           <p>You can override the buffer size in each <em>map:pipeline</em> section:</p>
  255.     <source>
  256.      <![CDATA[
  257.         <map:pipeline type="noncaching">
  258.             <map:parameter name="outputBufferSize" value="4096"/>
  259.             ...
  260.         </map:pipeline>
  261.      ]]>
  262.     </source>
  263.           <p>The above parameters sets the buffer size to <em>4096</em> for this
  264.           particular pipeline. Please note, that the parameter element does have
  265.           the sitemap namespace!</p>
  266.          </s3>
  267.         </s2>
  268.         <s2 title="Configuration of Caches">
  269.             <p>Each cache can be configured with the store to use.</p>
  270.         </s2>
  271.         <s2 title="Configuration of Stores">
  272.             <p>Have a look at the store configuration.</p>
  273.         </s2>
  274.       </s1>
  275.       <s1 title="Additional Information for Developers">
  276.          <s2 title="Java APIs">
  277.         <p>For more information on the java apis refer directly to the
  278.                javadocs of Cocoon.</p>
  279.             <p>The most important packages are:</p>
  280.         <ol>
  281.             <li><code>org.apache.cocoon.caching</code>: This package declares all interfaces for caching.</li>
  282.             <li><code>org.apache.cocoon.components.pipeline</code>: The interfaces and implementations of the pipelines.</li>
  283.         </ol>
  284.          </s2>
  285.         <s2 title="The XMLSerializer/XMLDeserializer">
  286.             <p>The caching of the sax events is implemented by two Avalon components: 
  287.                      The XMLSerializer and the XMLDeserializer. The XMLSerializer gets
  288.                      sax events and creates an object which is used by the XMLDeserializer
  289.                      to recreate these sax events.</p>
  290.             <s3 title="org.apache.cocoon.components.sax.XMLByteStreamCompiler">
  291.                   <p>The <code>XMLByteStreamCompiler</code>compiles sax events into a byte stream.</p>
  292.             </s3>
  293.             <s3 title="org.apache.cocoon.components.sax.XMLByteStreamInterpreter">
  294.                 <p>The <code>XMLByteStreamInterpreter</code> is the counterpart of the 
  295.                    <code>XMLByteStreamCompiler</code>. It interprets the byte
  296.                            stream and creates sax events.</p>
  297.             </s3> 
  298.             <s3 title="Configuration">
  299.             <p>The XMLSerializer and XMLDeserialzer are two Avalon components which
  300.                can be configured in the cocoon.xconf:</p>
  301.     <source>
  302.      <![CDATA[
  303. <xml-serializer
  304.     class="org.apache.cocoon.components.sax.XMLByteStreamCompiler"/>
  305.  
  306. <xml-deserializer
  307.     class="org.apache.cocoon.components.sax.XMLByteStreamInterpreter"/>
  308.      ]]>
  309.     </source> 
  310.             <p>You must assure that the correct (or matching) deserializer is 
  311.                      configured for the serializer.</p>
  312.             <p>Both components are poolable, so make sure you set appropriate pool sizes
  313.                for these components. For more information on component pooling have a look
  314.                at the Avalon documentation.</p>
  315.         </s3>
  316.         </s2>
  317.       </s1>
  318.  
  319.   </body>
  320. </document>
  321.