Example #1
0
  @Test
  public void testReadThroughSimpleCase() {

    CacheManager manager = new CacheManager();
    manager.removalAll();

    CacheConfiguration config =
        new CacheConfiguration()
            .name("sampleCacheReadThru")
            .maxElementsInMemory(100)
            .timeToIdleSeconds(8)
            .timeToLiveSeconds(8)
            .overflowToDisk(false);
    manager.addCache(new Cache(config));

    Ehcache cache = manager.getEhcache("sampleCacheReadThru");
    ReadThroughCacheConfiguration readThroughConfig =
        new ReadThroughCacheConfiguration().modeGet(true).build();

    ReadThroughCache decorator = new ReadThroughCache(cache, readThroughConfig);

    cache.registerCacheLoader(stringifyCacheLoader);

    // should not be in the cache
    Element got = cache.get(new Integer(1));
    Assert.assertNull(got);

    // now load with decorator via get
    got = decorator.get(new Integer(1));
    Assert.assertNotNull(got);

    // now should be in the base cache
    got = cache.get(new Integer(1));
    Assert.assertNotNull(got);

    // now let it expire.
    sleepySeconds(10);

    // missing
    got = cache.get(new Integer(1));
    Assert.assertNull(got);

    // now load with get with loader
    got = decorator.get(new Integer(1));
    Assert.assertNotNull(got);

    // now should be in the base cache too.
    got = cache.get(new Integer(1));
    Assert.assertNotNull(got);

    manager.removalAll();
    manager.shutdown();
  }
Example #2
0
 /** Service SHUTDOWN */
 public void destroy() {
   try {
     cacheManager.clearAll();
     cacheManager.removalAll(); // removeAllCaches()
     cacheManager.shutdown();
   } catch (CacheException e) {
     // NOTHING TO DO HERE
     log.warn("destroy() cache shutdown failure: " + e);
   }
   cacheManager = null; // release
   log.info("SHUTDOWN");
 }
 @AfterClass
 public static void tearDownClass() {
   cacheManager.removalAll();
   cacheManager.shutdown();
 }