7.4.5. Hierarchical caches

7.4.5. Hierarchical caches

If you need to set up a hierarchy within a single VM, you can use the LocalDelegatingCacheLoader. This type of hierarchy can currently only be set up programmatically. The code below shows how a first-level cache delegates to a local second-level cache:

TreeCache firstLevel, secondLevel;
LocalDelegatingCacheLoader cache_loader;

// create and configure firstLevel
firstLevel=new TreeCache();

// create and configure secondLevel
secondLevel=new TreeCache();

// create DelegatingCacheLoader
cache_loader=new LocalDelegatingCacheLoader(secondLevel);

// set CacheLoader in firstLevel
firstLevel.setCacheLoader(cache_loader);

// start secondLevel
secondLevel.startService();

// start firstLevel
firstLevel.startService();

If you need to set up a hierarchy across VMs but within a cluster, you can use the RpcDelegatingCacheLoader, which delegates all cache loading requests from non-coordinator caches to the cluster's coordinator cache. The coordinator cache is the first cache in the cluster to come online. Note that if the coordinator cache leaves the cluster for any reason, the second cache in the cluster to come online becomes the coordinator and so on. The XML below shows how to configure a cluster using RpcDelegatingCacheLoader:

<!-- ==================================================================== -->
<!-- Defines TreeCache configuration                                      -->
<!-- ==================================================================== -->

<mbean code="org.jboss.cache.TreeCache" name="jboss.cache:service=TreeCache">
...
<attribute name="CacheLoaderConfiguration">
    <config>
        <passivation>false</passivation>
        <preload>/some/stuff</preload>
        <cacheloader>
            <class>org.jboss.cache.loader.RpcDelegatingCacheLoader</class>
            <!-- whether the cache loader writes are asynchronous -->
            <async>false</async>
            <!-- only one cache loader in the chain may set fetchPersistentState to true.
                 An exception is thrown if more than one cache loader sets this to true. 
	    -->
            <fetchPersistentState>false</fetchPersistentState>
            <!-- determines whether this cache loader ignores writes - defaults to false. 
	    -->
            <ignoreModifications>false</ignoreModifications>
            <!-- if set to true, purges the contents of this cache loader when 
	    the cache starts up.
            Defaults to false.  -->
            <purgeOnStartup>false</purgeOnStartup>
        </cacheloader>
    </config>
</attribute>
...
</mbean>

Note that currently (JBossCache 1.3.0) this cache loader is not well supported, and has not been tested. We suggest to use TcpDelegatingCacheLoader instead (see next).