@Override public boolean contains(Class<?> type, Object key) { Cache cache = getCache(type); return cache != null && !cache.isClosed() && cache.containsKey(key); }
@Override public void invalidate(Class<?> type, Object key) { Cache cache = getCache(type); if (cache != null && !cache.isClosed()) { cache.remove(key); } }
@Override public <T> T get(Class<T> type, Object key) { Cache cache = getCache(type); if (cache != null && cache.isClosed()) { cache = null; } if (cache != null) { SerializedEntity container = (SerializedEntity) cache.get(key); if (container != null) { return type.cast(container.getEntity()); } } return null; }
<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 CacheControllerDispatcher get() { final Map<CacheType, CacheController<Object, Object>> cacheControllers = new LinkedHashMap<CacheType, CacheController<Object, Object>>(); for (final BaseCacheLoader cacheLoader : cacheLoaders) { final CacheType cacheType = cacheLoader.getCacheType(); final Cache cache = cacheManager.getCache(cacheType.getCacheName(), cacheType.getKeyType(), cacheType.getValueType()); if (cache == null) { logger.warn("Cache for cacheName='{}' not configured", cacheLoader.getCacheType().getCacheName()); continue; } Preconditions.checkState(!cache.isClosed(), "Cache '%s' should not be closed", cacheType.getCacheName()); final CacheController<Object, Object> killBillCacheController = new KillBillCacheController<Object, Object>(cache, cacheLoader); cacheControllers.put(cacheType, killBillCacheController); } return new CacheControllerDispatcher(cacheControllers); } }
/** Processes the event and logs if an exception is thrown. */ @SuppressWarnings("PMD.SwitchStmtsShouldHaveDefault") public void dispatch(@NonNull JCacheEntryEvent<K, V> event) { try { if (event.getSource().isClosed()) { return; } switch (event.getEventType()) { case CREATED: onCreated(event); return; case UPDATED: onUpdated(event); return; case REMOVED: onRemoved(event); return; case EXPIRED: onExpired(event); return; } throw new IllegalStateException("Unknown event type: " + event.getEventType()); } catch (Exception e) { logger.log(Level.WARNING, null, e); } catch (Throwable t) { logger.log(Level.SEVERE, null, t); } }
@Test @SuppressWarnings({"EmptyTryBlock", "unused"}) public void testCachesReleasedOnSessionFactoryClose() { TestHelper.preBuildAllCaches(); try (SessionFactoryImplementor sessionFactory = TestHelper.buildStandardSessionFactory() ) { } TestHelper.visitDomainRegions( cache -> { if ( cache == null ) { return; } if ( cache.isClosed() ) { return; } fail( "Cache was not closed " ); } ); } }
@Override public boolean isClosed() { return cache.isClosed(); }
public static <K,V> void shutdownCache(Cache<K,V> cache) { cache.close(); if ( ! cache.isClosed() ) throw new IllegalStateException("Cache is not properly closed !"); } }
@Override public void invalidate(Class<?> type, Object key) { Cache cache = getCache(type); if (cache != null && !cache.isClosed()) { cache.remove(key); } }
@Override public boolean contains(Class<?> type, Object key) { Cache cache = getCache(type); return cache != null && !cache.isClosed() && cache.containsKey(key); }
@Override public <T> T get(Class<T> type, Object key) { Cache cache = getCache(type); if (cache != null && cache.isClosed()) { cache = null; } if (cache != null) { SerializedEntity container = (SerializedEntity) cache.get(key); if (container != null) { return type.cast(container.getEntity()); } } return null; }
<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()); } }
private void ensureClosed(Cache cache) { if (!cache.isClosed()) { fail(); } } }
@Override public void destroyCache(final String cacheName) { assertNotClosed(); assertNotNull(cacheName, "cacheName"); final Cache<?, ?> cache = caches.remove(cacheName); if (cache != null && !cache.isClosed()) { cache.clear(); cache.close(); } }
@Override public void destroyCache(final String cacheName) { assertNotClosed(); assertNotNull(cacheName, "cacheName"); final Cache<?, ?> cache = caches.remove(cacheName); if (cache != null && !cache.isClosed()) { cache.clear(); cache.close(); } }
@Override public void destroyCache(final String cacheName) { assertNotClosed(); assertNotNull(cacheName, "cacheName"); final Cache<?, ?> cache = caches.remove(cacheName); if (cache != null && !cache.isClosed()) { cache.clear(); cache.close(); } }
private void ensureOpen(Cache cache) { if (cache.isClosed()) { fail(); } }
@Override public void destroy() { if (!cache.isClosed()) { cache.close(); } if (!manager.isClosed()) { manager.close(); } provider.close(); }