<?xml version="1.0"?>
<!--
  Copyright 2002-2004 The Apache Software Foundation or its licensors,
  as applicable.
  
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
  http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<document xmlns="http://maven.apache.org/XDOC/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/XDOC/2.0 http://maven.apache.org/xsd/xdoc-2.0.xsd">
  <properties>
    <title>design caching</title>
  </properties>
  <body>
    <section name="Table of contents">
      <macro name="toc">
        <param name="section" value="0"/>
        <param name="fromDepth" value="0"/>
        <param name="toDepth" value="4"/>
      </macro>
      <p>This document explains the design of the caching support..</p>
    </section>
    <section name="Introduction">
      <p>
        In many cases, application-level caching can be used to obtain a more performant
        application. An example of this is a typical web application, which needs to generate a page
        containing links to several items. In this case, to generate the page itself, it needs to 
        first needs to obtain a list of items to put on the page. The returned HTML for the page 
        refers to these items. As a result, the browser sends a separate request for each item on
        references on the page. In this case, caching can be used to obtain an increase in
        performance since the results of the initial query can be cached. 
      </p>
    </section>
    <section name="Requirements">
      <ol>
        <li>
          It shall be possible to cache objects.   
        </li>
        <li>
          The application using caching shall not depend on the caching implementation used so that
          the caching implementation can be easily switched.  
        </li>
        <li>
          Caching shall provide a utility to obtain a specific object specified by a unique key 
          by querying the cache, and if not there, by computing the required object. 
        </li>
      </ol>
    </section>
    <section name="Design">
      <p>
        The design provides a <code>Cache</code> interface which represents a certain cache. The
        cache provides options for setting and retrieving an object based on a key and for removing 
        objects from the cache. In addition there is a utility <code>CachedObject</code> that
        provides a method to obtain an object from the cache by key and, if not found, execute a
        callback to compute the object. 
      </p>
    </section>
    <section name="Structure">
      <subsection name="Package Overview">
        <p>
          Caching support consists of a single package. 
        </p>
        <img src="../umlpictures/class-diagram-org.wamblee.cache.packages.jpg" alt="Package overview. "/>
      </subsection>
      <subsection name="Caching">
        <p>
          The <code>Cache</code> interface encapsulates a certain cache. 
        </p>
        <img src="../umlpictures/class-diagram-org.wamblee.cache.main.jpg" alt="Overview. "/>
        <p>
          The following cache implemenations are provided: 
        </p>
        <ul>
          <li><strong>ZeroCache</strong>: A cache that does not cache anything. Useful for debugging. </li>
          <li><strong>ForeverCache</strong>: A cache that never expires any objects from the cache
            and does not impose limits on the number of cached objects. </li>
          <li><strong>EhCache</strong>: A cache that uses <code>EHCache</code> internally for its
            implementation. It is constructed with a cache name referring to a configured cache
            region for EHCache and with the configuration file for EHCache. </li>
        </ul>
      </subsection>
      <subsection name="Cached Object utility.">
        <p>
          The <code>CachedObject</code> encapsulates the logic for obtaining an object from the 
          cache and recomputing it if necessary.  At construction of the <code>CachedObject</code>,
          a <code>Computation</code> callback must be passed which performs the computation. 
        </p>
        <img src="../umlpictures/class-diagram-org.wamblee.cache.cachedobject.jpg" alt="Cached object. "/>
      </subsection>
    </section>
    <section name="Dynamics">
      <subsection name="Object Retrieval from the Cache">
        <p>
          This scenario describes usage of <code>CachedObject</code>. 
        </p>
        <img src="../umlpictures/sequence-diagram-org.wamblee.cache.cachedobject.jpg" alt="Retrieving an object from the cache."/>
        <p>
          At construction of the <code>CachedObject</code>, the user must pass a <code>Computation</code>
          callback to the object which is used to compute an object. To obtain the object,
          <code>get()</code> is called. This first looks in the cache if the object is there, and if
          not, executes the computation callback to compute the object. 
        </p>
      </subsection>
    </section>
    <section name="Design Rules">
      <p>-</p>
    </section>
  </body>
</document>
