KSB Clustered Caching Using OSCache

You can use OSCache two ways with KSB to provide clustered caching:

  1. Use the KSB-provided wrapper classes and Spring configuration to configure caching

  2. Configure OSCache to use KSB and then use OSCache directly

Distributed caching using the KSB wrapper classes and Spring configuration

These classes were created to:

  1. Simplify the configuration of the OSCache distributed caching

  2. Return null when the cache doesn't have a value instead of throwing an exception. Without this, OSCache throws a NeedsRefreshException when it can't find an entry in the cache.

Configure the cache in Spring:

<bean id="cache" class=" org.kuali.rice.ksb.cache.RiceCacheAdministratorImpl"  />

Inject the cache into KSBConfigurer:

<bean id="rice" class="org.kuali.rice.core.config.RiceConfigurer">
    <property name="serviceNamespace" value="KEW" />
    <property name="dataSource" ref="dataSource" />
    <property name="nonTransactionalDataSource" ref="nonTransactionalDataSource" />
    <property name="transactionManager" ref="jotm" />
    <property name="userTransaction" ref="jotm" />
    <property name="rootConfig" ref="config" />
    <property name=”ksbConfigurer”>
        <bean class=” org.kuali.rice.ksb.messaging.config.KSBConfigurer”>
        <property name="serviceServletUrl" value="http://localhost:${ksb.testharness.port}/en-test/remoting/" />
        <property name="cache">
            <ref bean="cache"/>
        </property>

(The entire RiceConfigurer is shown here for clarity.)

Now use the cache:

RiceCacheAdministrator cache = getBeanFromSpring("cache");

//clear cache
cache.flushAll();

//put entry in cache
cache.putInCache(this.key, this.value, this.group);

//flush entry
cache.flushEntry(this.key);

//flush group
cache.flushGroup(this.group);

All flushing will be broadcast across the cluster. The cache cluster is a topic base off the messageEntity in the KRice configurer. As machines with a KRice cache configured with the same messageEntity come up, they will automatically join the cluster.

Configuring the OSCache behind the scenes

OSCache has many configuration options available as properties. You can use the Rice XML Configuration Subsystem to make those properties available to OSCache. Simply prefix any parameter with the string 'cache' and that parameter will be given to OSCache when the RiceCacheAdministratorImpl configures OSCache.

Using OSCache directly and using the bus to distribute cache notifications

OSCache is configured through a properties object. This is the easiest way to configure it to use KSB with these properties. Putting the following property in the OSCache configuration properties will configure OSCache to use the KSB to cluster its caching:

properties.put(AbstractCacheAdministrator.CACHE_ENTRY_EVENT_LISTENERS_KEY, RiceDistributedCacheListener.class.getName());

It is also possible to put the RiceDistributedCacheListener in a properties file that configures OSCache. KSB must be fully configured and properly running for this to work. The cluster is based on a topic using the messageEntity given to the KSB in its configuration.