Creating Cache from Template

Normally cache configurations are defined upfront in the cacheonix-config.xml. In addition to the static cache configuration, Cacheonix supports creating a cache at runtime. Cacheonix considers a cache with a name "default" as a template configuration for the new cache:

<paritionedCache name="default">
     <store>
         <lru maxElements="10" maxBytes="10mb"/>
         <overflowToDisk maxOverflowBytes="1mb"/>
         <expiration timeToLive="1s"/>
     </store>
</paritionedCache>

Some applications require creating caches using templates other then default. To support this scenario Cacheonix provides named templates. A named template is a cache configuration with attribute template set to true:

<paritionedCache name="small.cache.template" template="true">
     <store>
         <lru maxElements="10" maxBytes="10mb"/>
         <overflowToDisk maxOverflowBytes="1mb"/>
         <expiration timeToLive="1s"/>
     </store>
</paritionedCache>

The code fragment below shows creating a cache my.cache using template small.cache.template:

final Cacheonix cacheonix = Cacheonix.getInstance();
final Cache cache = cacheonix.getCache("my.cache", "small.cache.template");

The following example shows creating a cache using a template in detail:

package com.cacheonix.examples.cache;

import cacheonix.Cacheonix;
import cacheonix.ShutdownMode;
import cacheonix.cache.Cache;
import junit.framework.TestCase;

/**
 * Tester for CacheManager.
 */
public final class CacheonixTest extends TestCase {

   private static final String NEW_LOCAL_TEST_CACHE = "new.local.test.cache";

   private static final String NAMED_TEMPLATE = "named.template";

   private Cacheonix cacheonix;


   /**
    * Tests creating a Cache using a default template.
    */
   public void testCreateCacheFromDefaultTemplate() {

      final Cache cache = cacheonix.createCache(NEW_LOCAL_TEST_CACHE);
      assertNotNull("Cache returned by cacheonix should be not null", cache);
      assertEquals(NEW_LOCAL_TEST_CACHE, cache.getName());
   }


   /**
    * Tests creating a Cache using a named template.
    */
   public void testCreateCacheFromNamedTemplate(){

      final Cache cache = cacheonix.createCache(NEW_LOCAL_TEST_CACHE, NAMED_TEMPLATE);
      assertNotNull("Cache returned by cacheonix should be not null", cache);
      assertEquals(NEW_LOCAL_TEST_CACHE, cache.getName());
   }


   /**
    * Sets up the fixture. This method is called before a test is executed.
    * <p/>
    * Cacheonix receives the default configuration from a <code>cacheonix-config.xml</code> found in a class path or
    * using a file that name is defined by system parameter <code>cacheonix.config.xml<code>.
    */
   protected void setUp() throws Exception {

      super.setUp();

      // Get Cacheonix using a default Cacheonix configuration. The configuration
      // is stored in the conf/cacheonix-config.xml
      cacheonix = Cacheonix.getInstance();
   }


   /**
    * Tears down the fixture. This method is called after a test is executed.
    */
   protected void tearDown() throws Exception {

      // Cache manager has be be shutdown upon application exit.
      // Note that call to shutdown() here uses unregisterSingleton
      // set to true. This is necessary to support clean restart on setUp()
      cacheonix.shutdown(ShutdownMode.GRACEFUL_SHUTDOWN, true);
      cacheonix = null;

      super.tearDown();
   }

}

See Also

Labels

 
(None)