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.


  1. It shall be possible to cache objects.
  2. The application using caching shall not depend on the caching implementation used so that the caching implementation can be easily switched.
  3. 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.


The design provides a Cache 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 CachedObject that provides a method to obtain an object from the cache by key and, if not found, execute a callback to compute the object.


Package Overview

Caching support consists of a single package.

Package overview.


The Cache interface encapsulates a certain cache.


The following cache implemenations are provided:

  • ZeroCache: A cache that does not cache anything. Useful for debugging.
  • ForeverCache: A cache that never expires any objects from the cache and does not impose limits on the number of cached objects.
  • EhCache: A cache that uses EHCache 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.

Cached Object utility.

The CachedObject encapsulates the logic for obtaining an object from the cache and recomputing it if necessary. At construction of the CachedObject, a Computation callback must be passed which performs the computation.

Cached object.


Object Retrieval from the Cache

This scenario describes usage of CachedObject.

Retrieving an object from the cache.

At construction of the CachedObject, the user must pass a Computation callback to the object which is used to compute an object. To obtain the object, get() is called. This first looks in the cache if the object is there, and if not, executes the computation callback to compute the object.

Design Rules