Getting Cache from Cache Manager

Call getCache(String cacheName) method of the cache manager to get an instance of a cache:

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

The detailed example below shows how to get a cache from the cache manager:

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 LOCAL_TEST_CACHE = "local.test.cache";

   private Cacheonix cacheonix;


   /**
    * Tests getting an instance of Cache. The configuration for the cache is defined in the cacheonix configuration
    * file.
    */
   public void testGetCache() {

      final Cache cache = cacheonix.getCache(LOCAL_TEST_CACHE);
      assertNotNull("Cache returned by cacheonix should be not null", cache);
      assertEquals(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)