Java Code Examples for javax.cache.CacheManager#close()

The following examples show how to use javax.cache.CacheManager#close() . 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: RepositoryCachingProvider.java    From mobi with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void close(URI uri, ClassLoader classLoader) {
    synchronized (cacheManagers) {
        ClassLoader managerClassLoader = getManagerClassLoader(classLoader);
        Map<URI, CacheManager> cacheManagersByURI = cacheManagers.get(managerClassLoader);

        if (cacheManagersByURI != null) {
            CacheManager cacheManager = cacheManagersByURI.remove(getManagerUri(uri));
            if (cacheManager != null) {
                cacheManager.close();
            }
            if (cacheManagersByURI.isEmpty()) {
                cacheManagers.remove(managerClassLoader);
            }
        }
    }
}
 
Example 2
Source File: NotSerializableTest.java    From commons-jcs with Apache License 2.0 6 votes vote down vote up
@Test
public void run()
{
    final CachingProvider cachingProvider = Caching.getCachingProvider();
    final CacheManager cacheManager = cachingProvider.getCacheManager();
    cacheManager.createCache("default", new MutableConfiguration<String, NotSerializableAndImHappyWithIt>().setStoreByValue(false));
    final Cache<String, NotSerializableAndImHappyWithIt> cache = cacheManager.getCache("default");
    assertFalse(cache.containsKey("foo"));
    cache.put("foo", new NotSerializableAndImHappyWithIt("bar"));
    assertTrue(cache.containsKey("foo"));
    assertEquals("bar", cache.get("foo").name);
    cache.remove("foo");
    assertFalse(cache.containsKey("foo"));
    cache.close();
    cacheManager.close();
    cachingProvider.close();
}
 
Example 3
Source File: CacheManagerTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void testReuseCacheManager() throws Exception {
  CachingProvider provider = Caching.getCachingProvider();
  URI uri = provider.getDefaultURI();

  CacheManager cacheManager = provider.getCacheManager(uri, provider.getDefaultClassLoader());
  assertFalse(cacheManager.isClosed());
  cacheManager.close();
  assertTrue(cacheManager.isClosed());

  try {
    cacheManager.createCache("Dog", new MutableConfiguration());
    fail();
  } catch (IllegalStateException e) {
    //expected
  }

  CacheManager otherCacheManager = provider.getCacheManager(uri, provider.getDefaultClassLoader());
  assertFalse(otherCacheManager.isClosed());

  assertNotSame(cacheManager, otherCacheManager);
}
 
Example 4
Source File: JCSCachingProvider.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
@Override
public void close(final ClassLoader classLoader)
{
    final Map<URI, CacheManager> cacheManagers = cacheManagersByLoader.remove(classLoader);
    if (cacheManagers != null)
    {
        for (final CacheManager mgr : cacheManagers.values())
        {
            mgr.close();
        }
        cacheManagers.clear();
    }
}
 
Example 5
Source File: JCacheIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void instantiateCache() {
    CachingProvider cachingProvider = Caching.getCachingProvider("com.hazelcast.cache.HazelcastCachingProvider");
    CacheManager cacheManager = cachingProvider.getCacheManager();
    MutableConfiguration<String, String> config = new MutableConfiguration<>();
    Cache<String, String> cache = cacheManager.createCache("simpleCache", config);
    cache.put("key1", "value1");
    cache.put("key2", "value2");
    assertEquals("value1", cache.get("key1"));
    assertEquals("value2", cache.get("key2"));
    cacheManager.close();
}
 
Example 6
Source File: JCachingProvider.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public void close(URI uri, ClassLoader classLoader) {
    Map<URI, CacheManager> uri2manager = managers.get(classLoader);
    if (uri2manager == null) {
        return;
    }
    CacheManager manager = uri2manager.remove(uri);
    if (manager == null) {
        return;
    }
    manager.close();
    if (uri2manager.isEmpty()) {
        managers.remove(classLoader, Collections.emptyMap());
    }
}
 
Example 7
Source File: CacheManagerTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void close_twice() {
  CacheManager cacheManager = getCacheManager();

  cacheManager.close();
  cacheManager.close();
}
 
Example 8
Source File: CacheManagerTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@After
public void teardown() {
  CacheManager cacheManager = getCacheManager();
  for (String cacheName : cacheManager.getCacheNames()) {
    cacheManager.destroyCache(cacheName);
  }
  cacheManager.close();
}
 
Example 9
Source File: CaffeineCachingProvider.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("PMD.CloseResource")
public void close(ClassLoader classLoader) {
  synchronized (cacheManagers) {
    ClassLoader managerClassLoader = getManagerClassLoader(classLoader);
    Map<URI, CacheManager> cacheManagersByURI = cacheManagers.remove(managerClassLoader);
    if (cacheManagersByURI != null) {
      for (CacheManager cacheManager : cacheManagersByURI.values()) {
        cacheManager.close();
      }
    }
  }
}
 
Example 10
Source File: IteratorTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testIterateExpiredIsSkipped() throws Exception {
  EhcacheCachingProvider provider = (EhcacheCachingProvider) Caching.getCachingProvider();
  TestTimeSource testTimeSource = new TestTimeSource();
  TimeSourceConfiguration timeSourceConfiguration = new TimeSourceConfiguration(testTimeSource);
  CacheManager cacheManager = provider.getCacheManager(new URI("test://testIterateExpiredReturnsNull"), new DefaultConfiguration(getClass().getClassLoader(), timeSourceConfiguration));

  Cache<Number, CharSequence> testCache = cacheManager.createCache("testCache", new MutableConfiguration<Number, CharSequence>()
      .setExpiryPolicyFactory(() -> new ExpiryPolicy() {
        @Override
        public Duration getExpiryForCreation() {
          return Duration.ETERNAL;
        }

        @Override
        public Duration getExpiryForAccess() {
          return new Duration(TimeUnit.SECONDS, 1L);
        }

        @Override
        public Duration getExpiryForUpdate() {
          return Duration.ZERO;
        }
      })
      .setTypes(Number.class, CharSequence.class));

  testCache.put(1, "one");
  testCache.get(1);

  testTimeSource.advanceTime(1000);

  Iterator<Cache.Entry<Number, CharSequence>> iterator = testCache.iterator();
  assertThat(iterator.hasNext(), is(false));

  cacheManager.close();
}
 
Example 11
Source File: TCKCacheManagerTest.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Test
public void close_cachesClosed() {
    CacheManager cacheManager = getCacheManager();

    cacheManager.createCache("c1", new MutableConfiguration());
    Cache cache1 = cacheManager.getCache("c1");
    cacheManager.createCache("c2", new MutableConfiguration());
    Cache cache2 = cacheManager.getCache("c2");

    cacheManager.close();

    ensureClosed(cache1);
    ensureClosed(cache2);
}
 
Example 12
Source File: CacheTest.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
@Test
public void accessExpiry() throws InterruptedException
{
    final CachingProvider cachingProvider = Caching.getCachingProvider();
    final CacheManager cacheManager = cachingProvider.getCacheManager(cachingProvider.getDefaultURI(),
            Thread.currentThread().getContextClassLoader(),
            cachingProvider.getDefaultProperties());
    final Cache<Integer, Integer> cache = cacheManager.createCache(
            "test",
            new MutableConfiguration<Integer, Integer>()
                    .setStoreByValue(false)
                    .setStatisticsEnabled(true)
                    .setManagementEnabled(true)
                    .setTypes(Integer.class, Integer.class)
                    .setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(new Duration(TimeUnit.MILLISECONDS, 500))));

    try {
        cache.put(1, 2);
        cache.get(1);
        Thread.sleep(650);
        assertFalse(cache.containsKey(1));
        cache.put(1, 2);
        for (int i = 0; i < 3; i++) { // we update the last access to force the idle time and lastaccess to be synced
            Thread.sleep(250);
            assertTrue("iteration: " + Integer.toString(i), cache.containsKey(1));
        }
        assertTrue(cache.containsKey(1));
        Thread.sleep(650);
        assertFalse(cache.containsKey(1));
    } finally {
        cacheManager.close();
        cachingProvider.close();
    }
}
 
Example 13
Source File: JCSCachingProvider.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
@Override
public void close()
{
    for (final Map<URI, CacheManager> v : cacheManagersByLoader.values())
    {
        for (final CacheManager m : v.values())
        {
            m.close();
        }
        v.clear();
    }
    cacheManagersByLoader.clear();
}
 
Example 14
Source File: CacheManagerTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void close_cachesClosed() {
  CacheManager cacheManager = getCacheManager();

  cacheManager.createCache("c1", new MutableConfiguration());
  Cache cache1 = cacheManager.getCache("c1");
  cacheManager.createCache("c2", new MutableConfiguration());
  Cache cache2 = cacheManager.getCache("c2");

  cacheManager.close();

  ensureClosed(cache1);
  ensureClosed(cache2);
}
 
Example 15
Source File: CachingProviderTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void closeCacheManagers() {
  CachingProvider provider = Caching.getCachingProvider();

  ClassLoader loader1 = CLASS_LOADER;
  CacheManager manager1 = provider.getCacheManager(provider.getDefaultURI(), loader1, null);
  manager1.close();

  ClassLoader loader2 = new MyClassLoader(CLASS_LOADER);
  CacheManager manager2 = provider.getCacheManager(provider.getDefaultURI(), loader2, null);
  manager2.close();

  assertNotSame(manager1, provider.getCacheManager(provider.getDefaultURI(), loader1, null));
  assertNotSame(manager2, provider.getCacheManager(provider.getDefaultURI(), loader2, null));
}
 
Example 16
Source File: BlazingCacheProvider.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public synchronized void close() {
    WeakHashMap<ClassLoader, HashMap<URI, BlazingCacheManager>> managersByClassLoader = this.cacheManagersByClassLoader;
    this.cacheManagersByClassLoader = new WeakHashMap<>();

    for (Map.Entry<ClassLoader, HashMap<URI, BlazingCacheManager>> entry : managersByClassLoader.entrySet()) {
        HashMap<URI, BlazingCacheManager> managers = entry.getValue();
        for (CacheManager cacheManager : managers.values()) {
            cacheManager.close();
        }
    }
}
 
Example 17
Source File: CacheManagerTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void enableStatistics_managerStopped() {
  CacheManager cacheManager = getCacheManager();
  cacheManager.close();
  cacheManager.enableStatistics("notThere", true);
  fail();
}
 
Example 18
Source File: CacheManagerTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void removeCache_Stopped() {
  CacheManager cacheManager = getCacheManager();
  cacheManager.close();
  try {
    cacheManager.destroyCache("c1");
    fail();
  } catch (IllegalStateException e) {
    //ok
  }
}
 
Example 19
Source File: InfinispanJCache.java    From infinispan-simple-tutorials with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
   // Construct a simple local cache manager with default configuration
   CachingProvider jcacheProvider = Caching.getCachingProvider();
   CacheManager cacheManager = jcacheProvider.getCacheManager();
   MutableConfiguration<String, String> configuration = new MutableConfiguration<>();
   configuration.setTypes(String.class, String.class);
   // create a cache using the supplied configuration
   Cache<String, String> cache = cacheManager.createCache("myCache", configuration);
   // Store a value
   cache.put("key", "value");
   // Retrieve the value and print it out
   System.out.printf("key = %s\n", cache.get("key"));
   // Stop the cache manager and release all resources
   cacheManager.close();
}
 
Example 20
Source File: InternalCacheRule.java    From commons-jcs with Apache License 2.0 4 votes vote down vote up
@Override
public Statement apply(final Statement base, final Description description)
{
    return new Statement()
    {
        @Override
        public void evaluate() throws Throwable
        {
            final CachingProvider provider = Caching.getCachingProvider();
            final CacheManager manager = provider.getCacheManager();
            try
            {
                Field cache = null;
                CompleteConfiguration<?, ?> config = null;
                for (final Field f : test.getClass().getDeclaredFields())
                {
                    if (Cache.class.isAssignableFrom(f.getType()))
                    {
                        f.setAccessible(true);
                        cache = f;
                    }
                    else if (Configuration.class.isAssignableFrom(f.getType()))
                    {
                        f.setAccessible(true);
                        config = (CompleteConfiguration<?, ?>) f.get(test);
                    }
                }
                if (cache != null)
                {
                    if (config == null)
                    {
                        throw new IllegalStateException("Define a Configuration field");
                    }
                    cache.set(test, manager.createCache(cache.getName(), config));
                }
                base.evaluate();
            }
            finally
            {
                manager.close();
                provider.close();
            }
        }
    };
}