示例#1
0
 @Override
 public void deleteSchema() {
   cache.removeAll();
   manager.destroyCache(super.getPersistentClass().getSimpleName());
   persistentDataStore.deleteSchema();
   LOG.info(
       "Deleted schema on persistent store and destroyed cache for persistent bean {}.",
       super.getPersistentClass().getSimpleName());
 }
  @Test
  public void testJSRExample1() throws InterruptedException {
    final String cacheName = randomString();

    CacheManager cacheManager = cachingProvider1.getCacheManager();
    assertNotNull(cacheManager);

    assertNull(cacheManager.getCache(cacheName));

    CacheConfig<Integer, String> config = new CacheConfig<Integer, String>();
    Cache<Integer, String> cache = cacheManager.createCache(cacheName, config);
    assertNotNull(cache);

    assertTrueEventually(
        new AssertTask() {
          @Override
          public void run() throws Exception {
            CacheManager cm2 = cachingProvider2.getCacheManager();
            assertNotNull(cm2.getCache(cacheName));
          }
        });

    Integer key = 1;
    String value1 = "value";
    cache.put(key, value1);

    String value2 = cache.get(key);
    assertEquals(value1, value2);
    cache.remove(key);
    assertNull(cache.get(key));

    Cache<Integer, String> cache2 = cacheManager.getCache(cacheName);
    assertNotNull(cache2);

    key = 1;
    value1 = "value";
    cache.put(key, value1);

    value2 = cache.get(key);
    assertEquals(value1, value2);
    cache.remove(key);
    assertNull(cache.get(key));

    cacheManager.destroyCache(cacheName);
    cacheManager.close();
  }
 @Test
 public void testCachesDestroy() {
   CacheManager cacheManager = cachingProvider1.getCacheManager();
   CacheManager cacheManager2 = cachingProvider2.getCacheManager();
   MutableConfiguration configuration = new MutableConfiguration();
   final Cache c1 = cacheManager.createCache("c1", configuration);
   final Cache c2 = cacheManager2.getCache("c1");
   c1.put("key", "value");
   cacheManager.destroyCache("c1");
   assertTrueEventually(
       new AssertTask() {
         @Override
         public void run() throws Exception {
           try {
             c2.get("key");
             throw new AssertionError("get should throw IllegalStateException");
           } catch (IllegalStateException e) {
             // ignored as expected
           }
         }
       });
 }
  @Test
  public void testExpiryOnCreation() throws Exception {

    // close cache since need to configure cache with ExpireOnCreationPolicy
    CacheManager mgr = cache.getCacheManager();
    mgr.destroyCache(cache.getName());

    MutableConfiguration<Long, String> config = new MutableConfiguration<Long, String>();
    config.setStatisticsEnabled(true);
    config.setTypes(Long.class, String.class);
    config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(ExpireOnCreationPolicy.class));

    cache = mgr.createCache(getTestCacheName(), config);
    cache.put(1L, "hello");
    assertEquals(0L, lookupManagementAttribute(cache, CacheStatistics, "CachePuts"));

    Map<Long, String> map = new HashMap<Long, String>();
    map.put(2L, "goodbye");
    map.put(3L, "world");
    cache.putAll(map);
    assertEquals(0L, lookupManagementAttribute(cache, CacheStatistics, "CachePuts"));
  }