@AfterMethod(alwaysRun = true) public void after() { cacheManager.destroyCache("jcache"); cacheManager.destroyCache("jcacheLoading"); }
<C extends Configuration> void createCache(final CacheManager cacheManager, final String cacheName, final C configuration) { // Make sure we start from a clean state - this is mainly useful for tests cacheManager.destroyCache(cacheName); final Cache cache = cacheManager.createCache(cacheName, configuration); Preconditions.checkState(!cache.isClosed(), "Cache '%s' should not be closed", cacheName); // Re-create the metrics to support dynamically created caches (e.g. for Shiro) metricRegistry.removeMatching(new MetricFilter() { @Override public boolean matches(final String name, final Metric metric) { return name != null && name.startsWith(PROP_METRIC_REG_JCACHE_STATISTICS); } }); metricRegistry.register(PROP_METRIC_REG_JCACHE_STATISTICS, new JCacheGaugeSet()); } }
@Override public void invalidate(Class<?> type) { Cache cache = getCache(type); if (cache != null) { cache.clear(); String cacheName = getCacheName(model.typeOf(type)); cacheManager.destroyCache(cacheName); synchronized (caches) { caches.remove(type); } cache.close(); } }
@BeforeClass public void beforeClass() { final CachingProvider provider = Caching.getCachingProvider(PROVIDER_NAME); cacheManager = provider.getCacheManager(); cacheManager.destroyCache("cache-not-in-config-file"); cacheConfig = new MutableConfiguration<>(); cacheConfig.setTypes(String.class, String.class); cacheConfig.setStatisticsEnabled(true); }
@Override public void close() { if (isClosed()) { return; } synchronized (configuration) { if (!isClosed()) { enableManagement(false); enableStatistics(false); cacheManager.destroyCache(name); closed = true; Throwable thrown = null; thrown = tryClose(expiry, thrown); thrown = tryClose(writer, thrown); thrown = tryClose(cacheLoader.orElse(null), thrown); for (Registration<K, V> registration : dispatcher.registrations()) { thrown = tryClose(registration.getCacheEntryListener(), thrown); } if (thrown != null) { logger.log(Level.WARNING, "Failure when closing cache resources", thrown); } } } cache.invalidateAll(); }
public static void createCache(CacheManager cacheManager, String name, boolean usePrefix) { if ( usePrefix ) { name = prefix( name ); } if ( cacheManager.getCache( name ) != null ) { cacheManager.destroyCache( name ); } cacheManager.createCache( name, new MutableConfiguration<>() ); }
@Test public void testRunTimeTypeLaxity() throws Exception { CachingProvider provider = Caching.getCachingProvider(); javax.cache.CacheManager cacheManager = provider.getCacheManager(this.getClass().getResource("/ehcache-107-types.xml").toURI(), getClass().getClassLoader()); MutableConfiguration<Long, String> cache1Conf = new MutableConfiguration<>(); cache1Conf.setTypes(Long.class, String.class); javax.cache.Cache<Long, String> cache = cacheManager.createCache("cache1", cache1Conf); @SuppressWarnings("unchecked") Configuration<Long, String> cache1CompleteConf = cache.getConfiguration(Configuration.class); assertThat(cache1CompleteConf.getKeyType(), is(equalTo(Long.class))); assertThat(cache1CompleteConf.getValueType(), is(equalTo(String.class))); try { cacheManager.getCache("cache1"); } finally { cacheManager.destroyCache("cache1"); cacheManager.close(); } }
@Test @SuppressWarnings("unchecked") public void testCompileTimeTypeSafety() throws Exception { CachingProvider provider = Caching.getCachingProvider(); javax.cache.CacheManager cacheManager = provider.getCacheManager(this.getClass().getResource("/ehcache-107-types.xml").toURI(), getClass().getClassLoader()); MutableConfiguration<Long, String> cache1Conf = new MutableConfiguration<>(); javax.cache.Cache<Long, String> cache = cacheManager.createCache("cache1", cache1Conf); cache.put(1l, "one"); cache.put(2l, "two"); Configuration<Object, Object> cache1CompleteConf = cache.getConfiguration(Configuration.class); //This ensures that we have compile time type safety, i.e when configuration does not have types defined but // what you get cache as should work. assertThat(cache1CompleteConf.getKeyType(), is(equalTo(Object.class))); assertThat(cache1CompleteConf.getValueType(), is(equalTo(Object.class))); assertThat(cache.get(1l), is(equalTo("one"))); assertThat(cache.get(2l), is(equalTo("two"))); javax.cache.Cache<String, String> second = cacheManager.getCache("cache1"); second.put("3","three"); assertThat(second.get("3"), is(equalTo("three"))); cacheManager.destroyCache("cache1"); cacheManager.close(); }
/** * */ @Test public void testStartIgnite() { javax.cache.spi.CachingProvider cachingProvider = Caching.getCachingProvider(); assert cachingProvider instanceof CachingProvider; CacheManager cacheMgr = cachingProvider.getCacheManager(); assertEquals(Collections.<String>emptySet(), Sets.newHashSet(cacheMgr.getCacheNames())); Cache<Integer, String> cacheA = cacheMgr.createCache("a", new CacheConfiguration()); cacheA.put(1, "1"); assertEquals("1", cacheA.get(1)); cacheMgr.createCache("b", new CacheConfiguration()); assertEquals(Sets.newHashSet("a", "b"), Sets.newHashSet(cacheMgr.getCacheNames())); cacheMgr.destroyCache("a"); cacheMgr.destroyCache("b"); assertEquals(Collections.<String>emptySet(), Sets.newHashSet(cacheMgr.getCacheNames())); }
/** * Tests start -> destroy -> start -> close using CacheManager. */ @Test public void testTckStyleCreateDestroyClose() throws Exception { startGridsMultiThreaded(gridCount()); CacheManager mgr = Caching.getCachingProvider().getCacheManager(); String cacheName = "cache"; mgr.createCache(cacheName, new MutableConfiguration<Integer, String>().setTypes(Integer.class, String.class)); mgr.destroyCache(cacheName); Cache<Integer, String> cache = mgr.createCache(cacheName, new MutableConfiguration<Integer, String>().setTypes(Integer.class, String.class)); cache.close(); // Check second close succeeds without exception. cache.close(); try { cache.get(1); fail(); } catch (IllegalStateException ignored) { // No-op; } }
@TimeStep(prob = 0.2) public void destroyCache(ThreadState state) { try { cacheManager.destroyCache(name); state.counter.destroy++; } catch (IllegalStateException e) { state.counter.destroyException++; } }
@Override protected void postDestroy() { CacheManager cacheManager = cacheManagerRef.get(); if (cacheManager != null) { cacheManager.destroyCache(getName()); } resetCacheManager(); }
@Override protected void postDestroy() { CacheManager cacheManager = cacheManagerRef.get(); if (cacheManager != null) { cacheManager.destroyCache(getName()); } resetCacheManager(); }
@TimeStep(prob = 0.1) public void destroyCache(ThreadState state) { try { int cacheNumber = state.randomInt(maxCaches); state.cacheManager.destroyCache(name + cacheNumber); state.counter.destroy++; } catch (CacheException e) { state.counter.destroyException++; } catch (IllegalStateException e) { state.counter.destroyException++; } }
static void destroy() { if (cacheManager != null) { Iterable<String> cacheNames = cacheManager.getCacheNames(); for (String name : cacheNames) { cacheManager.destroyCache(name); } } if (instance != null) { instance.shutdown(); } }
@Override public void destroy() { LOGGER.debug( "Destroying all active caches and destroying the Cache Manager" ); for ( String cacheId : cacheManager.getCacheNames() ) { cacheManager.destroyCache( CACHE_ID_PREFIX + cacheId ); } cacheManager.close(); cachingProvider.close(); Hazelcast.shutdownAll(); }
@After public void tearDown() throws MalformedObjectNameException { //assertEquals(0, mBeanServer.queryNames(new ObjectName("java.cache:*"), null).size()); if (!cacheManager.isClosed()) { for (String cacheName : cacheManager.getCacheNames()) { cacheManager.destroyCache(cacheName); } } cacheManager.close(); //All registered object names should be removed during shutdown assertThat(mBeanServer.queryNames(new ObjectName("javax.cache:*"), null), IsEmptyCollection.<ObjectName>empty()); }
@Test public void removeCache_Stopped() { CacheManager cacheManager = getCacheManager(); cacheManager.close(); try { cacheManager.destroyCache("c1"); fail(); } catch (IllegalStateException e) { //ok } }
@Test(expected = NullPointerException.class) public void getCacheNullValueClass() { String name = "c1"; CacheManager manager = Caching.getCachingProvider().getCacheManager(); manager.createCache(name, new MutableConfiguration().setTypes(Long.class, String.class)); try { Caching.getCache(name, Long.class, null); } finally { manager.destroyCache(name); } }
@Test public void cachingProviderGetCache() { String name = "c1"; Caching.getCachingProvider().getCacheManager().createCache(name, new MutableConfiguration().setTypes(Long.class, String.class)); Cache cache = Caching.getCache(name, Long.class, String.class); assertEquals(name, cache.getName()); Caching.getCachingProvider().getCacheManager().destroyCache(name); }