Overview

There are three cache implementations

GlobalCacheService

This Service functions as a Global Cache. A global cache is a good place to store items that you may need to access often but don't necessarily need (or want) to fetch from the database everytime. A good example would be a look up table of States that you store in a database and use throughout your application. Since information about States doesn't change very often, you could store this information in the Global Cache and decrease the overhead of hitting the database everytime you need State information.

It is written for use in Turbine but it can be used in any container compatible with Avalon's ECM container.

First, here is the role configuration.

    
        <role
            name="org.apache.fulcrum.cache.GlobalCacheService"
            shorthand="cache"
            default-class="org.apache.fulcrum.cache.DefaultGlobalCacheService"/>
    
    

And here is the configuration:


    
        <cache cacheInitialSize="20" cacheCheckFrequency="5"/>
    
    

EHCacheService

First, here is the role configuration.

    
        <role
            name="org.apache.fulcrum.cache.EHCacheService"
            shorthand="ehcache"
            default-class="org.apache.fulcrum.cache.impl.DefaultEHCacheService"/>
    
    

And here is the configuration:


    
 <ehcache>

    </ehcache>
    
    

JCSCacheService

The JCS cache service implements the interface GlobalCacheService and thus can serve as a drop-in replacement for DefaultGlobalCacheService. However it is possible to configure the cache behaviour in much more detail to provide disk caches or lateral TCP caches for example.

First, here is the role configuration.

    
        <role
            name="org.apache.fulcrum.cache.GlobalCacheService"
            shorthand="jcscache"
            default-class="org.apache.fulcrum.cache.impl.JCSCacheService"/>
    
    

And here is the configuration:


    
        <jcscache>
	    	<cacheCheckFrequency>5000</cacheCheckFrequency>
        	<region>fulcrum</region>
        	<configurationFile>/cache.ccf</configurationFile>
        </jcscache>
    
    

The region parameter is the name of the cache region to use when caching objects. It defaults to fulcrum. JCS will store the objects in a group named default_group in that region. The configuration file parameter gives the location of the JCS configuration file. Please note that JCS uses a class loader to read this file, so make sure this path is part of your classpath. The default values of all configuration settings are shown in the example. See the JCS site for more information about configuring JCS.

Usage


    GlobalCacheService gs = null;
    try
    {
        /*
         * Look for the item in the cache.
         * If it doesn't exist or the item is stale,
         * the cache will throw an exception.
         */
        gs = (GlobalCacheService)avalonComponentService.lookup(GlobalCacheService.ROLE)

        CachedObject obj = gs.getObject("cached_object");

        data.setMessage( data.getScreen() + " Got " +
            obj.getContents().toString() + " from global cache!" );
    }
    catch(ObjectExpiredException gone)
    {
        /*
         * Add the item to the cache.
         */
        gs.addObject("cached_object",
            new CachedObject("in_the_cache",5000));

        data.setMessage( data.getScreen() +
            " Refreshed/or added new item to" +
            " the cache! Expires in 5 seconds" );
    }

    

You can also place an expiration time on your objects so the Service will automatically remove them when they expire. If you don't specify an expiration time, the DefaultGlobalCacheService uses 5 seconds. For JCS this value depends on values set in the cache configuration file. To see an example, look at the test case CacheTest