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 during runtime. Cacheonix considers a cache with a name "default" as a template configuration for the new cache:

<cacheonix tempDir="${java.io.temp}">
   <local>
      <cache name="default" maxSize="1000" policy="lru" expirationTimeSecs="0" overflowToDisk="no" maxOverflowSizeMBytes="0"/>
   </local>
</cacheonix>

Some applications require creating caches using different templates. To support this scenario Cacheonix provides named templates. A named template is a cache configuration with attribute template set to "yes":
<cacheonix tempDir="${java.io.temp}">
   <local>
      <cache name="named.template" maxSize="1000" policy="lru" expirationTimeSecs="0" overflowToDisk="no" maxOverflowSizeMBytes="0"
             template="yes"/>
   </local>
</cacheonix>

The following example shows creating a cache using a template:

package com.cacheonix.examples.cache;

import junit.framework.TestCase;

import cacheonix.cache.Cache;
import cacheonix.cache.CacheManager;
import com.cacheonix.storage.StorageException;

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

   private static final String LOCAL_TEST_CACHE = "local.test.cache";
   private static final String NAMED_TEMPLATE = "named.template";
   private static final String NEW_LOCAL_TEST_CACHE = "new.local.test.cache";

   private CacheManager cacheManager;


   /**
    * Tests creating a Cache using a default template.
    */
   public void testCreateCacheFromDefaultTemplate() throws StorageException {
      final Cache cache = cacheManager.createCache(NEW_LOCAL_TEST_CACHE);
      assertNotNull("Cache returned by cacheManager should be not null", cache);
      assertEquals(NEW_LOCAL_TEST_CACHE, cache.getName());
   }


   /**
    * Tests creating a Cache using a named template.
    */
   public void testCreateCacheFromNamedTemplate() throws StorageException {
      final Cache cache = cacheManager.createCache(NEW_LOCAL_TEST_CACHE, NAMED_TEMPLATE);
      assertNotNull("Cache returned by cacheManager 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 CacheManager using a default Cacheonix configuration. The configuration
      // is stored in the conf/cacheonix-config.xml
      cacheManager = CacheManager.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
      cacheManager.shutdown();
      super.tearDown();
   }
}

Labels

 
(None)