@Override protected Collection<Cache> loadCaches() { CacheManager cacheManager = getCacheManager(); Assert.state(cacheManager != null, "No CacheManager set"); Collection<Cache> caches = new LinkedHashSet<>(); for (String cacheName : cacheManager.getCacheNames()) { javax.cache.Cache<Object, Object> jcache = cacheManager.getCache(cacheName); caches.add(new JCacheCache(jcache, isAllowNullValues())); } return caches; }
@Override protected Cache getMissingCache(String name) { CacheManager cacheManager = getCacheManager(); Assert.state(cacheManager != null, "No CacheManager set"); // Check the JCache cache again (in case the cache was added at runtime) javax.cache.Cache<Object, Object> jcache = cacheManager.getCache(name); if (jcache != null) { return new JCacheCache(jcache, isAllowNullValues()); } return null; }
protected boolean cacheExists(String unqualifiedRegionName, SessionFactoryImplementor sessionFactory) { final String qualifiedRegionName = RegionNameQualifier.INSTANCE.qualify( unqualifiedRegionName, sessionFactory.getSessionFactoryOptions() ); return cacheManager.getCache( qualifiedRegionName ) != null; }
public static void visitDomainRegions(Consumer<Cache> action) { final CacheManager cacheManager = JCacheHelper.locateStandardCacheManager(); for ( String regionName : entityRegionNames ) { action.accept( cacheManager.getCache( regionName ) ); } for ( String regionName : collectionRegionNames ) { action.accept( cacheManager.getCache( regionName ) ); } }
public void removeCache(String name) { cacheNames.remove(name); given(cacheManager.getCache(name)).willReturn(null); } }
@Test public void factory() { Cache<Integer, Integer> cache = cacheManager.getCache("guice"); Map<Integer, Integer> result = cache.getAll(ImmutableSet.of(1, 2, 3)); assertThat(result, is(ImmutableMap.of(1, 1, 2, 2, 3, 3))); }
@Test public void jmxBeanIsRegistered_getCache() throws Exception { checkConfigurationJmx(() -> cacheManager.getCache("test-cache")); }
@Test public void definedCache() { try { cacheManager.createCache("test-cache-2", cacheConfig); Assert.fail(); } catch (CacheException ignored) {} checkConfiguration(() -> cacheManager.getCache("test-cache-2", String.class, Integer.class), 1000L); }
private Cache getCache(Class<?> type) { Cache cache; synchronized (caches) { cache = caches.get(type); if (cache == null) { Type declaredType = model.typeOf(type); String cacheName = getCacheName(declaredType); Class keyClass = getKeyClass(declaredType); cache = cacheManager.getCache(cacheName, keyClass, SerializedEntity.class); } } return cache; }
@Test public void getCache() { Cache<Integer, Integer> cache = Caching.getCachingProvider() .getCacheManager().getCache("test-cache"); assertThat(cache, is(not(nullValue()))); @SuppressWarnings("unchecked") CaffeineConfiguration<Integer, Integer> config = cache.getConfiguration(CaffeineConfiguration.class); checkTestCache(config); }
public static Cache<?, ?> getCache(String regionName) { final CacheManager cacheManager = JCacheHelper.locateStandardCacheManager(); regionName = prefix( regionName ); return cacheManager.getCache( regionName ); }
@Test public void anonymousCache() { checkConfiguration(() -> cacheManager.createCache("cache-not-in-config-file", cacheConfig), 500L); checkConfiguration(() -> cacheManager.getCache("cache-not-in-config-file", String.class, String.class), 500L); }
/** * @param cacheMgr Cache manager. * @param cacheName Cache name. */ private boolean isStatisticsEnabled(CacheManager cacheMgr, String cacheName) { return ((IgniteCache)cacheMgr.getCache(cacheName)).metrics().isStatisticsEnabled(); } }
@SuppressWarnings("unchecked") public void addCache(String name) { cacheNames.add(name); Cache cache = mock(Cache.class); given(cache.getName()).willReturn(name); given(cacheManager.getCache(name)).willReturn(cache); }
@Before public void setup() { this.cacheManager = getCachingProvider().getCacheManager(); this.cacheManager.createCache(CACHE_NAME, new MutableConfiguration<>()); this.cacheManager.createCache(CACHE_NAME_NO_NULL, new MutableConfiguration<>()); this.nativeCache = this.cacheManager.getCache(CACHE_NAME); this.cache = new JCacheCache(this.nativeCache); Cache<Object, Object> nativeCacheNoNull = this.cacheManager.getCache(CACHE_NAME_NO_NULL); this.cacheNoNull = new JCacheCache(nativeCacheNoNull, false); }
@Test public void sanity() { CachingProvider cachingProvider = Caching.getCachingProvider( "com.github.benmanes.caffeine.jcache.spi.CaffeineCachingProvider", getClass().getClassLoader()); Cache<String, Integer> cache = cachingProvider.getCacheManager() .getCache("test-cache-2", String.class, Integer.class); assertNull(cache.get("a")); } }
@SuppressWarnings("WeakerAccess") protected Cache<Object, Object> getOrCreateCache(String unqualifiedRegionName, SessionFactoryImplementor sessionFactory) { verifyStarted(); assert !RegionNameQualifier.INSTANCE.isQualified( unqualifiedRegionName, sessionFactory.getSessionFactoryOptions() ); final String qualifiedRegionName = RegionNameQualifier.INSTANCE.qualify( unqualifiedRegionName, sessionFactory.getSessionFactoryOptions() ); final Cache<Object, Object> cache = cacheManager.getCache( qualifiedRegionName ); if ( cache == null ) { return createCache( qualifiedRegionName ); } return cache; }
public JCache(URL url) { String method = url.getParameter(Constants.METHOD_KEY, ""); String key = url.getAddress() + "." + url.getServiceKey() + "." + method; // jcache parameter is the full-qualified class name of SPI implementation String type = url.getParameter("jcache"); CachingProvider provider = StringUtils.isEmpty(type) ? Caching.getCachingProvider() : Caching.getCachingProvider(type); CacheManager cacheManager = provider.getCacheManager(); Cache<Object, Object> cache = cacheManager.getCache(key); if (cache == null) { try { //configure the cache MutableConfiguration config = new MutableConfiguration<Object, Object>() .setTypes(Object.class, Object.class) .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.MILLISECONDS, url.getMethodParameter(method, "cache.write.expire", 60 * 1000)))) .setStoreByValue(false) .setManagementEnabled(true) .setStatisticsEnabled(true); cache = cacheManager.createCache(key, config); } catch (CacheException e) { // concurrent cache initialization cache = cacheManager.getCache(key); } } this.store = cache; }
public JCache(URL url) { String method = url.getParameter(Constants.METHOD_KEY, ""); String key = url.getAddress() + "." + url.getServiceKey() + "." + method; // jcache parameter is the full-qualified class name of SPI implementation String type = url.getParameter("jcache"); CachingProvider provider = StringUtils.isEmpty(type) ? Caching.getCachingProvider() : Caching.getCachingProvider(type); CacheManager cacheManager = provider.getCacheManager(); Cache<Object, Object> cache = cacheManager.getCache(key); if (cache == null) { try { //configure the cache MutableConfiguration config = new MutableConfiguration<Object, Object>() .setTypes(Object.class, Object.class) .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.MILLISECONDS, url.getMethodParameter(method, "cache.write.expire", 60 * 1000)))) .setStoreByValue(false) .setManagementEnabled(true) .setStatisticsEnabled(true); cache = cacheManager.createCache(key, config); } catch (CacheException e) { // concurrent cache initialization cache = cacheManager.getCache(key); } } this.store = cache; }
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<>() ); }