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

The following examples show how to use javax.cache.CacheManager#getCache() . 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: CertificateCacheManagerImpl.java    From carbon-device-mgt with Apache License 2.0 6 votes vote down vote up
public static Cache<String, CertificateResponse> getCertificateCache() {
    DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig();
    CacheManager manager = getCacheManager();
    Cache<String, CertificateResponse> certificateCache = null;
    if (config.getDeviceCacheConfiguration().isEnabled()) {
        if (!isCertificateCacheInitialized) {
            initializeCertificateCache();
        }
        if (manager != null) {
            certificateCache = manager.<String, CertificateResponse>getCache(CertificateCacheManagerImpl.CERTIFICATE_CACHE);
        } else {
            certificateCache = Caching.getCacheManager(CertificateCacheManagerImpl.CERTIFICATE_CACHE_MANAGER).
                    <String, CertificateResponse>getCache(CertificateCacheManagerImpl.CERTIFICATE_CACHE);
        }
    }
    return certificateCache;
}
 
Example 2
Source File: TCKCacheManagerTest.java    From blazingcache with Apache License 2.0 6 votes vote down vote up
@Test
    public void jmxExampleTest() throws Exception {
        CachingProvider cachingProvider = Caching.getCachingProvider();
        CacheManager cacheManager = cachingProvider.getCacheManager();
        MutableConfiguration<String, Integer> config
                = new MutableConfiguration<String, Integer>();
        config.setTypes(String.class, Integer.class)
                .setStatisticsEnabled(true);
        cacheManager.createCache("simpleCache", config);
        Cache<String, Integer> cache = cacheManager.getCache("simpleCache", String.class, Integer.class);
        cache.get("test");
        MBeanServer mBeanServer = JMXUtils.getMBeanServer();
        System.out.println("mBeanServer:" + mBeanServer);
        ObjectName objectName = new ObjectName("javax.cache:type=CacheStatistics"
                + ",CacheManager=" + (cache.getCacheManager().getURI().toString())
                + ",Cache=" + cache.getName());
        System.out.println("obhectNAme:" + objectName);
//        Thread.sleep(Integer.MAX_VALUE);
        Long CacheMisses = (Long) mBeanServer.getAttribute(objectName, "CacheMisses");
        System.out.println("CacheMisses:" + CacheMisses);
        assertEquals(1L, CacheMisses.longValue());
    }
 
Example 3
Source File: CacheManagerTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void getCaches_MutateReturn() {
  CacheManager cacheManager = getCacheManager();

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

  try {
    Iterator iterator = cacheManager.getCacheNames().iterator();
    iterator.next();
    iterator.remove();
    fail();
  } catch (UnsupportedOperationException e) {
    // immutable
  }
}
 
Example 4
Source File: TCKCacheManagerTest.java    From blazingcache with Apache License 2.0 6 votes vote down vote up
@Test
public void createCacheSameName() {
    CacheManager cacheManager = getCacheManager();
    String name1 = "c1";
    cacheManager.createCache(name1, new MutableConfiguration());
    Cache cache1 = cacheManager.getCache(name1);
    assertEquals(cache1, cacheManager.getCache(name1));
    ensureOpen(cache1);

    try {
        cacheManager.createCache(name1, new MutableConfiguration());
    } catch (CacheException e) {
        //expected
    }
    Cache cache2 = cacheManager.getCache(name1);
}
 
Example 5
Source File: JCacheOAuthDataProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected static <K, V> Cache<K, V> createCache(CacheManager cacheManager,
                                                String cacheKey, Class<K> keyType, Class<V> valueType) {

    Cache<K, V> cache = cacheManager.getCache(cacheKey, keyType, valueType);
    if (cache == null) {
        cache = cacheManager.createCache(
            cacheKey,
            new MutableConfiguration<K, V>()
                .setTypes(keyType, valueType)
                .setStoreByValue(true)
                .setStatisticsEnabled(false)
        );
    }

    return cache;
}
 
Example 6
Source File: CacheManagerTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void getOrCreateCache_Different() {
  String name1 = "c1";
  CacheManager cacheManager = getCacheManager();
  cacheManager.createCache(name1, new MutableConfiguration());
  Cache cache1 = cacheManager.getCache(name1);

  String name2 = "c2";
  cacheManager.createCache(name2, new MutableConfiguration());
  Cache cache2 = cacheManager.getCache(name2);

  assertEquals(cache1, cacheManager.getCache(name1));
  assertEquals(cache2, cacheManager.getCache(name2));
}
 
Example 7
Source File: EntitlementBaseCache.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Getting existing cache if the cache available, else returns a newly created cache.
 * This logic handles by javax.cache implementation
 *
 * @return
 */
private Cache<K, V> getEntitlementCache() {

    Cache<K, V> cache = null;
    CacheManager cacheManager = Caching.getCacheManagerFactory().getCacheManager(ENTITLEMENT_CACHE_MANAGER);
    if (this.cacheTimeout > 0) {
        if (cacheBuilder == null) {
            synchronized (Entitlement_CACHE_NAME.intern()) {
                if (cacheBuilder == null) {
                    cacheManager.removeCache(Entitlement_CACHE_NAME);
                    this.cacheBuilder = cacheManager.<K, V>createCacheBuilder(Entitlement_CACHE_NAME).
                            setExpiry(CacheConfiguration.ExpiryType.MODIFIED,
                                      new CacheConfiguration.Duration(TimeUnit.SECONDS, cacheTimeout)).
                            setStoreByValue(false);
                    cache = cacheBuilder.build();

                    if (cacheEntryUpdatedListener != null) {
                        this.cacheBuilder.registerCacheEntryListener(cacheEntryUpdatedListener);
                    }
                    if (cacheEntryCreatedListener != null) {
                        this.cacheBuilder.registerCacheEntryListener(cacheEntryCreatedListener);
                    }
                    if (log.isDebugEnabled()) {
                        String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
                        log.debug("Cache : " + Entitlement_CACHE_NAME + "  is built with time out value " + ": " +
                                  cacheTimeout + " for tenant domain : " + tenantDomain);
                    }
                }
            }
        } else {
            cache = cacheManager.getCache(Entitlement_CACHE_NAME);
        }
    } else {
        cache = cacheManager.getCache(Entitlement_CACHE_NAME);
    }
    return cache;
}
 
Example 8
Source File: CacheManagerTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void getUntypedCache() {
  CacheManager cacheManager = getCacheManager();

  //configure an un-typed Cache
  MutableConfiguration config = new MutableConfiguration();

  cacheManager.createCache("untyped-cache", config);

  Cache cache = cacheManager.getCache("untyped-cache");

  assertNotNull(cache);
  assertEquals(Object.class, cache.getConfiguration(CompleteConfiguration.class).getKeyType());
  assertEquals(Object.class, cache.getConfiguration(CompleteConfiguration.class).getValueType());
}
 
Example 9
Source File: EntitlementEngineCache.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
private Cache<Integer, EntitlementEngine> getEntitlementCache() {
    Cache<Integer, EntitlementEngine> cache;
    CacheManager cacheManager = Caching.getCacheManagerFactory().getCacheManager(ENTITLEMENT_ENGINE_CACHE_MANAGER);
    if (cacheManager != null) {
        if (cacheBuilder == null) {
            Properties properties = EntitlementServiceComponent.getEntitlementConfig().getEngineProperties();
            String engineCachingInterval = properties.getProperty(PDPConstants.ENTITLEMENT_ENGINE_CACHING_INTERVAL);
            long entitlementEngineCachingInterval = DEFAULT_ENTITLEMENT_ENGINE_CACHING_INTERVAL;
            if (engineCachingInterval != null) {
                try {
                    entitlementEngineCachingInterval = Long.parseLong(engineCachingInterval);
                } catch (NumberFormatException e) {
                    log.warn("Invalid value for " + PDPConstants.ENTITLEMENT_ENGINE_CACHING_INTERVAL + ". Using " +
                             "default value " + entitlementEngineCachingInterval + " seconds.");
                }
            } else {
                if (log.isDebugEnabled()) {
                    log.debug(PDPConstants.ENTITLEMENT_ENGINE_CACHING_INTERVAL + " not set. Using default value " +
                              entitlementEngineCachingInterval + " seconds.");
                }
            }
            cacheManager.removeCache(ENTITLEMENT_ENGINE_CACHE);
            cacheBuilder = cacheManager.<Integer, EntitlementEngine>createCacheBuilder(ENTITLEMENT_ENGINE_CACHE).
                    setExpiry(CacheConfiguration.ExpiryType.ACCESSED,
                            new CacheConfiguration.Duration(TimeUnit.SECONDS, entitlementEngineCachingInterval)).
                    setExpiry(CacheConfiguration.ExpiryType.MODIFIED,
                            new CacheConfiguration.Duration(TimeUnit.SECONDS, entitlementEngineCachingInterval));
            cache = cacheBuilder.build();
        } else {
            cache = cacheManager.getCache(ENTITLEMENT_ENGINE_CACHE);
        }
    } else {
        cache = Caching.getCacheManager().getCache(ENTITLEMENT_ENGINE_CACHE);
    }
    if (log.isDebugEnabled()) {
        log.debug("created authorization cache : " + cache);
    }
    return cache;
}
 
Example 10
Source File: ConfigStatsManagementActivationTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testEnabledAtCacheManagerLevel() throws Exception {
  CacheManager cacheManager = provider.getCacheManager(getClass().getResource("/org/ehcache/docs/ehcache-107-mbeans-cache-manager-config.xml")
      .toURI(), provider.getDefaultClassLoader());

  Cache<String, String> cache = cacheManager.getCache("stringCache", String.class, String.class);
  @SuppressWarnings("unchecked")
  Eh107Configuration<String, String> configuration = cache.getConfiguration(Eh107Configuration.class);

  assertThat(configuration.isManagementEnabled(), is(true));
  assertThat(configuration.isStatisticsEnabled(), is(true));

  assertThat(isMbeanRegistered("stringCache", MBEAN_MANAGEMENT_TYPE), is(true));
  assertThat(isMbeanRegistered("stringCache", MBEAN_STATISTICS_TYPE), is(true));
}
 
Example 11
Source File: TCKCacheManagerTest.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Test
public void createCache_Different() {
    String name1 = "c1";
    CacheManager cacheManager = getCacheManager();
    cacheManager.createCache(name1, new MutableConfiguration());
    Cache cache1 = cacheManager.getCache(name1);

    String name2 = "c2";
    cacheManager.createCache(name2, new MutableConfiguration());
    Cache cache2 = cacheManager.getCache(name2);

    assertEquals(cache1, cacheManager.getCache(name1));
    assertEquals(cache2, cacheManager.getCache(name2));
}
 
Example 12
Source File: JCacheCacheManager.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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;
}
 
Example 13
Source File: ConfigStatsManagementActivationTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testEnabledAtCacheLevel() throws Exception {
  CacheManager cacheManager = provider.getCacheManager(getClass().getResource("/ehcache-107-mbeans-cache-config.xml")
      .toURI(), provider.getDefaultClassLoader());

  Cache<String, String> cache = cacheManager.getCache("stringCache", String.class, String.class);
  @SuppressWarnings("unchecked")
  Eh107Configuration<String, String> configuration = cache.getConfiguration(Eh107Configuration.class);

  assertThat(configuration.isManagementEnabled(), is(true));
  assertThat(configuration.isStatisticsEnabled(), is(true));

  assertThat(isMbeanRegistered("stringCache", MBEAN_MANAGEMENT_TYPE), is(true));
  assertThat(isMbeanRegistered("stringCache", MBEAN_STATISTICS_TYPE), is(true));
}
 
Example 14
Source File: SplitBrainCacheTest.java    From hazelcast-simulator with Apache License 2.0 5 votes vote down vote up
@Setup
@SuppressWarnings("unchecked")
public void setup() {
    CacheManager cacheManager = createCacheManager(targetInstance);
    cache = cacheManager.getCache(name);
    this.lastClusterSizeChange = new LastClusterSizeChange(0L,
            getMemberCount());
    this.minimalClusterSize = targetInstance.getConfig()
            .getSplitBrainProtectionConfig("cache-quorum-ref").getMinimumClusterSize();
}
 
Example 15
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 16
Source File: EntryProcessorICacheTest.java    From hazelcast-simulator with Apache License 2.0 5 votes vote down vote up
@Setup
public void setup() {
    resultsPerWorker = targetInstance.getList(name + ":ResultMap");

    CacheManager cacheManager = createCacheManager(targetInstance);
    cache = cacheManager.getCache(name);
}
 
Example 17
Source File: CacheConfiguration.java    From jhipster-online with Apache License 2.0 4 votes vote down vote up
private void createIfNotExists(CacheManager cacheManager, String cacheName,
                               javax.cache.configuration.Configuration<Object, Object> cacheConfiguration) {
    if(cacheManager.getCache(cacheName) == null) {
        cacheManager.createCache(cacheName, cacheConfiguration);
    }
}
 
Example 18
Source File: LongExternalizableCacheTest.java    From hazelcast-simulator with Apache License 2.0 4 votes vote down vote up
@Setup
public void setUp() {
    CacheManager cacheManager = createCacheManager(targetInstance);
    cache = cacheManager.getCache(name);
}
 
Example 19
Source File: PolicyManagerUtil.java    From carbon-device-mgt with Apache License 2.0 4 votes vote down vote up
public static Cache<Integer, Policy> getPolicyCache(String name) {
    CacheManager manager = getCacheManager();
    return (manager != null) ? manager.<Integer, Policy>getCache(name) :
            Caching.getCacheManager().<Integer, Policy>getCache(name);
}
 
Example 20
Source File: CacheManagerTest.java    From cache2k with Apache License 2.0 3 votes vote down vote up
@Test(expected = NullPointerException.class)
public void getNullTypeCacheRequest() {
  CacheManager cacheManager = getCacheManager();

  MutableConfiguration config = new MutableConfiguration();

  cacheManager.createCache("untyped-cache", config);

  Cache cache = cacheManager.getCache("untyped-cache", null, null);
}