Java Code Examples for org.ehcache.CacheManager#removeCache()

The following examples show how to use org.ehcache.CacheManager#removeCache() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestEhcache.java    From elastic-rabbitmq with MIT License 6 votes vote down vote up
@Test
public void testTem() {
    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
    .withCache("preConfigured",
       CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10)))
    .build();

    cacheManager.init();

    Cache<Long, String> preConfigured =
            cacheManager.getCache("preConfigured", Long.class, String.class);

    Cache<Long, String> myCache = cacheManager.createCache("myCache",
            CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10)));

    myCache.put(1L, "da one!");
    myCache.putIfAbsent(0L, "ee");
    String value = myCache.get(1L);

    System.out.println("Value is " + value);
    cacheManager.removeCache("preConfigured");
    cacheManager.close();
}
 
Example 2
Source File: GettingStarted.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void cachemanagerExample() {
  // tag::cachemanagerExample[]
  CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() // <1>
      .withCache("preConfigured",
          CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10))) // <2>
      .build(); // <3>
  cacheManager.init(); // <4>

  Cache<Long, String> preConfigured =
      cacheManager.getCache("preConfigured", Long.class, String.class); // <5>

  Cache<Long, String> myCache = cacheManager.createCache("myCache", // <6>
      CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10)));

  myCache.put(1L, "da one!"); // <7>
  String value = myCache.get(1L); // <8>

  cacheManager.removeCache("preConfigured"); // <9>

  cacheManager.close(); // <10>
  // end::cachemanagerExample[]
}
 
Example 3
Source File: EhcacheManagerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testCachesAddedAtRuntimeGetReInited() {
  Store.Provider storeProvider = mock(Store.Provider.class);
  when(storeProvider.rank(any(Set.class), any(Collection.class))).thenReturn(1);
  Store store = mock(Store.class);
  CacheEventDispatcherFactory cacheEventNotificationListenerServiceProvider = mock(CacheEventDispatcherFactory.class);

  when(storeProvider.createStore(any(Store.Configuration.class), ArgumentMatchers.<ServiceConfiguration>any())).thenReturn(store);
  when(store.getConfigurationChangeListeners()).thenReturn(new ArrayList<>());
  when(cacheEventNotificationListenerServiceProvider.createCacheEventDispatcher(store)).thenReturn(mock(CacheEventDispatcher.class));

  CacheConfiguration<Long, String> cache1Configuration = new TestCacheConfig<>(Long.class, String.class);
  Map<String, CacheConfiguration<?, ?>> caches = newCacheMap();
  caches.put("cache1", cache1Configuration);
  DefaultConfiguration config = new DefaultConfiguration(caches, null);
  CacheManager cacheManager = new EhcacheManager(config, Arrays.asList(
      storeProvider,
      mock(CacheLoaderWriterProvider.class),
      mock(WriteBehindProvider.class),
      cacheEventNotificationListenerServiceProvider,
      mock(CacheEventListenerProvider.class),
      mock(LocalPersistenceService.class),
      mock(ResilienceStrategyProvider.class)
  ));
  cacheManager.init();


  CacheConfiguration<Long, String> cache2Configuration = new TestCacheConfig<>(Long.class, String.class, ResourcePoolsHelper.createResourcePools(100L));
  cacheManager.createCache("cache2", cache2Configuration);
  cacheManager.removeCache("cache1");

  cacheManager.close();
  cacheManager.init();
  try {
    assertThat(cacheManager.getCache("cache1", Long.class, String.class), nullValue());
    assertThat(cacheManager.getCache("cache2", Long.class, String.class), notNullValue());
  } finally {
    cacheManager.close();
  }
}
 
Example 4
Source File: EHCacheUtil.java    From webChat with Apache License 2.0 4 votes vote down vote up
public void closeCache(CacheManager manager,String configureName){
    manager.removeCache(configureName);
    manager.close();
}
 
Example 5
Source File: DefaultCollectorServiceTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Test
public void test_collector() throws Exception {
  final Queue<Object> messages = new ConcurrentLinkedQueue<>();
  final List<String> notifs = new ArrayList<>(7);
  final CountDownLatch num = new CountDownLatch(6);

  CacheConfiguration<String, String> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
      newResourcePoolsBuilder()
          .heap(10, EntryUnit.ENTRIES)
          .offheap(1, MemoryUnit.MB))
      .build();

  ManagementRegistryService managementRegistry = new DefaultManagementRegistryService(new DefaultManagementRegistryConfiguration()
      .setCacheManagerAlias("my-cm-1"));

  CollectorService collectorService = new DefaultCollectorService(new CollectorService.Collector() {
    @Override
    public void onNotification(ContextualNotification notification) {
      onEvent(notification);
    }

    @Override
    public void onStatistics(Collection<ContextualStatistics> statistics) {
      onEvent(statistics);
    }

    void onEvent(Object event) {
      messages.offer(event);
      if (event instanceof ContextualNotification) {
        notifs.add(((ContextualNotification) event).getType());
      }
      num.countDown();
    }
  });

  // ehcache cache manager
  CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
      .using(managementRegistry)
      .using(collectorService)
      .build(false);

  cacheManager.init();

  cacheManager.close();
  cacheManager.init();

  managementRegistry.withCapability("StatisticCollectorCapability")
      .call("startStatisticCollector",
        new Parameter(1L, long.class.getName()),
        new Parameter(TimeUnit.SECONDS, TimeUnit.class.getName()))
      .on(Context.create("cacheManagerName", "my-cm-1"))
      .build()
      .execute()
      .getSingleResult();

  Cache<String, String> cache = cacheManager.createCache("my-cache", cacheConfiguration);
  cache.put("key", "val");
  cache.clear();

  num.await(10, TimeUnit.SECONDS);
  cacheManager.removeCache("my-cache");
  cacheManager.close();

  assertThat(notifs, equalTo(Arrays.asList("CACHE_MANAGER_AVAILABLE", "CACHE_MANAGER_CLOSED", "CACHE_MANAGER_AVAILABLE", "CACHE_ADDED", "CACHE_CLEARED", "CACHE_REMOVED", "CACHE_MANAGER_CLOSED")));
  assertThat(messages.size(), equalTo(8));
}
 
Example 6
Source File: EhcacheManagerTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Test
public void testChangesToManagerAreReflectedInConfig() {
  Store.Provider storeProvider = mock(Store.Provider.class);
  when(storeProvider.rank(any(Set.class), any(Collection.class))).thenReturn(1);
  Store store = mock(Store.class);
  CacheEventDispatcherFactory cacheEventNotificationListenerServiceProvider = mock(CacheEventDispatcherFactory.class);

  when(storeProvider.createStore(any(Store.Configuration.class), ArgumentMatchers.<ServiceConfiguration>any())).thenReturn(store);
  when(store.getConfigurationChangeListeners()).thenReturn(new ArrayList<>());
  when(cacheEventNotificationListenerServiceProvider.createCacheEventDispatcher(store)).thenReturn(mock(CacheEventDispatcher.class));

  CacheConfiguration<Long, String> cache1Configuration = new TestCacheConfig<>(Long.class, String.class);
  Map<String, CacheConfiguration<?, ?>> caches = newCacheMap();
  caches.put("cache1", cache1Configuration);
  DefaultConfiguration config = new DefaultConfiguration(caches, null);

  CacheManager cacheManager = new EhcacheManager(config, Arrays.asList(storeProvider,
      mock(CacheLoaderWriterProvider.class),
      mock(WriteBehindProvider.class),
      cacheEventNotificationListenerServiceProvider,
      mock(CacheEventListenerProvider.class),
      mock(LocalPersistenceService.class),
      mock(ResilienceStrategyProvider.class)
  ));
  cacheManager.init();

  try {
    final CacheConfiguration<Long, String> cache2Configuration = new TestCacheConfig<>(Long.class, String.class, ResourcePoolsHelper
      .createResourcePools(100L));
    final Cache<Long, String> cache = cacheManager.createCache("cache2", cache2Configuration);
    final CacheConfiguration<?, ?> cacheConfiguration = cacheManager.getRuntimeConfiguration()
        .getCacheConfigurations()
        .get("cache2");

    assertThat(cacheConfiguration, notNullValue());
    final CacheConfiguration<?, ?> runtimeConfiguration = cache.getRuntimeConfiguration();
    assertThat(cacheConfiguration == runtimeConfiguration, is(true));
    assertThat(cacheManager.getRuntimeConfiguration().getCacheConfigurations().get("cache1")
               == cacheManager.getCache("cache1", Long.class, String.class).getRuntimeConfiguration(), is(true));

    cacheManager.removeCache("cache1");
    assertThat(cacheManager.getRuntimeConfiguration().getCacheConfigurations().containsKey("cache1"), is(false));
  } finally {
    cacheManager.close();
  }
}