Пример #1
0
 /**
  * A factory method to register cache Loaders
  *
  * @param cacheConfiguration the cache configuration
  * @param cache the cache
  */
 protected static void registerCacheLoaders(CacheConfiguration cacheConfiguration, Ehcache cache) {
   List cacheLoaderConfigurations = cacheConfiguration.getCacheLoaderConfigurations();
   for (Object cacheLoaderConfiguration : cacheLoaderConfigurations) {
     CacheConfiguration.CacheLoaderFactoryConfiguration factoryConfiguration =
         (CacheConfiguration.CacheLoaderFactoryConfiguration) cacheLoaderConfiguration;
     CacheLoader cacheLoader = createCacheLoader(factoryConfiguration, cache);
     cache.registerCacheLoader(cacheLoader);
   }
 }
Пример #2
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();
  }