Java Code Examples for javax.cache.Cache#getConfiguration()

The following examples show how to use javax.cache.Cache#getConfiguration() . 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: TestJCache.java    From jframe with Apache License 2.0 6 votes vote down vote up
@Test
public void configEhcache2Jsr107() {
    CachingProvider provider = Caching.getCachingProvider();
    CacheManager cacheManager = provider.getCacheManager();

    CacheConfiguration<Long, String> cacheConfiguration = CacheConfigurationBuilder
            .newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10)).build();

    Cache<Long, String> cache = cacheManager.createCache("myCache",
            Eh107Configuration.fromEhcacheCacheConfiguration(cacheConfiguration));

    Eh107Configuration<Long, String> configuration = cache.getConfiguration(Eh107Configuration.class);
    configuration.unwrap(CacheConfiguration.class);

    configuration.unwrap(CacheRuntimeConfiguration.class);

    try {
        cache.getConfiguration(CompleteConfiguration.class);
        throw new AssertionError("IllegalArgumentException expected");
    } catch (IllegalArgumentException iaex) {
        // Expected
    }
}
 
Example 2
Source File: BlazingCacheManager.java    From blazingcache with Apache License 2.0 6 votes vote down vote up
@Override
public <K, V> Cache<K, V> getCache(String cacheName, Class<K> keyType, Class<V> valueType) {
    checkClosed();
    if (keyType == null || valueType == null) {
        throw new NullPointerException();
    }
    if (cacheName == null) {
        throw new NullPointerException();
    }
    Cache<K, V> res = caches.get(cacheName);
    if (res == null) {
        return null;
    }
    Configuration configuration = res.getConfiguration(Configuration.class);
    if ((!keyType.equals(configuration.getKeyType()))
            || !valueType.equals(configuration.getValueType())) {
        throw new ClassCastException();
    }
    return res;
}
 
Example 3
Source File: TestJCache.java    From jframe with Apache License 2.0 6 votes vote down vote up
@Test
public void cacheConfigTest() {
    CachingProvider provider = Caching.getCachingProvider();
    CacheManager cacheManager = provider.getCacheManager();

    MutableConfiguration<Long, String> configuration = new MutableConfiguration<Long, String>();
    configuration.setTypes(Long.class, String.class);
    Cache<Long, String> cache = cacheManager.createCache("someCache", configuration);

    CompleteConfiguration<Long, String> completeConfiguration = cache.getConfiguration(CompleteConfiguration.class);

    Eh107Configuration<Long, String> eh107Configuration = cache.getConfiguration(Eh107Configuration.class);

    CacheRuntimeConfiguration<Long, String> runtimeConfiguration = eh107Configuration
            .unwrap(CacheRuntimeConfiguration.class);
}
 
Example 4
Source File: JCacheManagerAdapter.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Override @SuppressWarnings("unchecked")
public <K, V> Cache<K, V> getCache(String _cacheName, final Class<K> _keyType, final Class<V> _valueType) {
  if (_keyType == null || _valueType == null) {
    throw new NullPointerException();
  }
  Cache<K, V> c = getCache(_cacheName);
  if (c == null) {
    return null;
  }
  Configuration cfg = c.getConfiguration(Configuration.class);
  if (!cfg.getKeyType().equals(_keyType)) {
    throw new ClassCastException("key type mismatch, expected: " + cfg.getKeyType().getName());
  }
  if (!cfg.getValueType().equals(_valueType)) {
    throw new ClassCastException("value type mismatch, expected: " + cfg.getValueType().getName());
  }
  return c;
}
 
Example 5
Source File: ConfigStatsManagementActivationTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void basicJsr107StillWorks() throws Exception {
  CacheManager cacheManager = provider.getCacheManager();

  MutableConfiguration<Long, String> configuration = new MutableConfiguration<>();
  configuration.setTypes(Long.class, String.class);
  configuration.setManagementEnabled(true);
  configuration.setStatisticsEnabled(true);

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

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

  assertThat(isMbeanRegistered("cache", MBEAN_MANAGEMENT_TYPE), is(true));
  assertThat(isMbeanRegistered("cache", MBEAN_STATISTICS_TYPE), is(true));
}
 
Example 6
Source File: ConfigStatsManagementActivationTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testManagementEnabledOverriddenFromTemplate() throws Exception {
  CacheManager cacheManager = provider.getCacheManager(getClass().getResource("/ehcache-107-mbeans-template-config.xml")
          .toURI(),
      provider.getDefaultClassLoader());

  MutableConfiguration<Long, String> configuration = new MutableConfiguration<>();
  configuration.setTypes(Long.class, String.class);
  configuration.setManagementEnabled(true);
  configuration.setStatisticsEnabled(true);

  Cache<Long, String> cache = cacheManager.createCache("disables-mbeans", configuration);

  @SuppressWarnings("unchecked")
  Eh107Configuration<Long, String> eh107Configuration = cache.getConfiguration(Eh107Configuration.class);
  assertThat(eh107Configuration.isManagementEnabled(), is(false));
  assertThat(eh107Configuration.isStatisticsEnabled(), is(false));

  assertThat(isMbeanRegistered("disables-mbeans", MBEAN_MANAGEMENT_TYPE), is(false));
  assertThat(isMbeanRegistered("disables-mbeans", MBEAN_STATISTICS_TYPE), is(false));
}
 
Example 7
Source File: JCSCachingManager.java    From commons-jcs with Apache License 2.0 6 votes vote down vote up
private <K, V> Cache<K, V> doGetCache(final String cacheName, final Class<K> keyType, final Class<V> valueType)
{
    final Cache<K, V> cache = (Cache<K, V>) caches.get(cacheName);
    if (cache == null)
    {
        return null;
    }

    final Configuration<K, V> config = cache.getConfiguration(Configuration.class);
    if ((keyType != null && !config.getKeyType().isAssignableFrom(keyType))
            || (valueType != null && !config.getValueType().isAssignableFrom(valueType)))
    {
        throw new IllegalArgumentException("this cache is <" + config.getKeyType().getName() + ", " + config.getValueType().getName()
                + "> " + " and not <" + keyType.getName() + ", " + valueType.getName() + ">");
    }
    return cache;
}
 
Example 8
Source File: ConfigStatsManagementActivationTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnableCacheLevelProgrammatic() throws Exception {
  CacheManager cacheManager = provider.getCacheManager();

  CacheConfigurationBuilder<Long, String> configurationBuilder = newCacheConfigurationBuilder(Long.class, String.class, heap(10))
      .withService(new Jsr107CacheConfiguration(ConfigurationElementState.ENABLED, ConfigurationElementState.ENABLED));
  Cache<Long, String> cache = cacheManager.createCache("test", Eh107Configuration.fromEhcacheCacheConfiguration(configurationBuilder));

  @SuppressWarnings("unchecked")
  Eh107Configuration<Long, String> configuration = cache.getConfiguration(Eh107Configuration.class);
  assertThat(configuration.isManagementEnabled(), is(true));
  assertThat(configuration.isStatisticsEnabled(), is(true));

  assertThat(isMbeanRegistered("test", MBEAN_MANAGEMENT_TYPE), is(true));
  assertThat(isMbeanRegistered("test", MBEAN_STATISTICS_TYPE), is(true));
}
 
Example 9
Source File: ConfigStatsManagementActivationTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testManagementDisabledOverriddenFromTemplate() throws Exception {
  CacheManager cacheManager = provider.getCacheManager(getClass().getResource("/ehcache-107-mbeans-template-config.xml")
          .toURI(),
      provider.getDefaultClassLoader());

  MutableConfiguration<Long, String> configuration = new MutableConfiguration<>();
  configuration.setTypes(Long.class, String.class);
  configuration.setManagementEnabled(false);
  configuration.setStatisticsEnabled(false);

  Cache<Long, String> cache = cacheManager.createCache("enables-mbeans", configuration);

  @SuppressWarnings("unchecked")
  Eh107Configuration<Long, String> eh107Configuration = cache.getConfiguration(Eh107Configuration.class);
  assertThat(eh107Configuration.isManagementEnabled(), is(true));
  assertThat(eh107Configuration.isStatisticsEnabled(), is(true));

  assertThat(isMbeanRegistered("enables-mbeans", MBEAN_MANAGEMENT_TYPE), is(true));
  assertThat(isMbeanRegistered("enables-mbeans", MBEAN_STATISTICS_TYPE), is(true));
}
 
Example 10
Source File: ConfigStatsManagementActivationTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testCacheLevelOnlyOneOverridesCacheManagerLevel() 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("overrideOneCache", String.class, String.class);
  @SuppressWarnings("unchecked")
  Eh107Configuration<String, String> configuration = cache.getConfiguration(Eh107Configuration.class);

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

  assertThat(isMbeanRegistered("overrideOneCache", MBEAN_MANAGEMENT_TYPE), is(true));
  assertThat(isMbeanRegistered("overrideOneCache", MBEAN_STATISTICS_TYPE), is(false));
}
 
Example 11
Source File: JCacheConfigurationTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
private void checkConfiguration(Supplier<Cache<?, ?>> cacheSupplier, long expectedValue) {
  Cache<?, ?> cache = cacheSupplier.get();

  @SuppressWarnings("unchecked")
  CaffeineConfiguration<?, ?> configuration =
      cache.getConfiguration(CaffeineConfiguration.class);
  assertThat(configuration.getMaximumSize(), is(OptionalLong.of(expectedValue)));
}
 
Example 12
Source File: TypesafeConfigurationTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 13
Source File: CacheManagerTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
private void checkConfigurationJmx(Supplier<Cache<?, ?>> cacheSupplier) throws Exception {
  Cache<?, ?> cache = cacheSupplier.get();

  @SuppressWarnings("unchecked")
  CompleteConfiguration<?, ?> configuration = cache.getConfiguration(CompleteConfiguration.class);
  assertThat(configuration.isManagementEnabled(), is(true));
  assertThat(configuration.isStatisticsEnabled(), is(true));

  String name = "javax.cache:Cache=%s,CacheManager=%s,type=CacheStatistics";
  ManagementFactory.getPlatformMBeanServer().getObjectInstance(
      new ObjectName(String.format(name, cache.getName(), PROVIDER_NAME)));
}
 
Example 14
Source File: ConfigStatsManagementActivationTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testCacheLevelOverridesCacheManagerLevel() 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("overrideCache", String.class, String.class);
  @SuppressWarnings("unchecked")
  Eh107Configuration<String, String> configuration = cache.getConfiguration(Eh107Configuration.class);

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

  assertThat(isMbeanRegistered("overrideCache", MBEAN_MANAGEMENT_TYPE), is(false));
  assertThat(isMbeanRegistered("overrideCache", MBEAN_STATISTICS_TYPE), is(false));
}
 
Example 15
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 16
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 17
Source File: Eh107XmlIntegrationTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void test107CacheCanReturnCompleteConfigurationWhenNonePassedIn() {
  CacheManager cacheManager = cachingProvider.getCacheManager();
  Cache<Long, String> cache = cacheManager.createCache("cacheWithoutCompleteConfig", new Configuration<Long, String>() {
    private static final long serialVersionUID = 1L;

    @Override
    public Class<Long> getKeyType() {
      return Long.class;
    }

    @Override
    public Class<String> getValueType() {
      return String.class;
    }

    @Override
    public boolean isStoreByValue() {
      return true;
    }
  });

  @SuppressWarnings("unchecked")
  CompleteConfiguration<Long, String> configuration = cache.getConfiguration(CompleteConfiguration.class);
  assertThat(configuration, notNullValue());
  assertThat(configuration.isStoreByValue(), is(true));

  // Respects defaults
  assertThat(configuration.getExpiryPolicyFactory(), equalTo(EternalExpiryPolicy.factoryOf()));
}
 
Example 18
Source File: CacheListenerTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
private int getConfigurationCacheEntryListenerConfigurationSize(Cache cache) {
  int i = 0;
  CompleteConfiguration<Long, String> cacheConfig = (CompleteConfiguration)cache.getConfiguration(CompleteConfiguration.class);
  for (CacheEntryListenerConfiguration<Long, String> listenerConfig : cacheConfig.getCacheEntryListenerConfigurations()) {
    i++;
  }
  return i;
}
 
Example 19
Source File: EhCache107ConfigurationIntegrationDocTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testGettingToEhcacheConfiguration() {
  // tag::mutableConfigurationExample[]
  MutableConfiguration<Long, String> configuration = new MutableConfiguration<>();
  configuration.setTypes(Long.class, String.class);
  Cache<Long, String> cache = cacheManager.createCache("someCache", configuration); // <1>

  CompleteConfiguration<Long, String> completeConfiguration = cache.getConfiguration(CompleteConfiguration.class); // <2>

  Eh107Configuration<Long, String> eh107Configuration = cache.getConfiguration(Eh107Configuration.class); // <3>

  CacheRuntimeConfiguration<Long, String> runtimeConfiguration = eh107Configuration.unwrap(CacheRuntimeConfiguration.class); // <4>
  // end::mutableConfigurationExample[]
  assertThat(completeConfiguration, notNullValue());
  assertThat(runtimeConfiguration, notNullValue());

  // Check uses default JSR-107 expiry
  long nanoTime = System.nanoTime();
  LOGGER.info("Seeding random with {}", nanoTime);
  Random random = new Random(nanoTime);
  assertThat(runtimeConfiguration.getExpiryPolicy().getExpiryForCreation(random.nextLong(), Long.toOctalString(random.nextLong())),
              equalTo(org.ehcache.expiry.ExpiryPolicy.INFINITE));
  assertThat(runtimeConfiguration.getExpiryPolicy().getExpiryForAccess(random.nextLong(),
    () -> Long.toOctalString(random.nextLong())), nullValue());
  assertThat(runtimeConfiguration.getExpiryPolicy().getExpiryForUpdate(random.nextLong(),
    () -> Long.toOctalString(random.nextLong()), Long.toOctalString(random.nextLong())), nullValue());
}
 
Example 20
Source File: EhCacheBuilder.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Cache<K, V> build(CacheManager manager) {
  checkNotNull(manager);
  checkNotNull(keyType);
  checkNotNull(valueType);
  checkNotNull(name);
  checkNotNull(expiryFactory);

  CacheConfigurationBuilder<K, V> builder = CacheConfigurationBuilder.newCacheConfigurationBuilder(
      keyType,
      valueType,
      ResourcePoolsBuilder.heap(cacheSize));

  builder.withExpiry(mapToEhCacheExpiry(expiryFactory.create()));

  Cache<K, V> cache = manager.createCache(name,  Eh107Configuration.fromEhcacheCacheConfiguration(builder));

  manager.enableStatistics(name, statisticsEnabled);
  manager.enableManagement(name, managementEnabled);

  if (persister != null) {
    CacheEventListener listener = (final CacheEvent cacheEvent) ->
        persister.accept((K) cacheEvent.getKey(), (V) cacheEvent.getOldValue());

    Eh107Configuration<K, V> configuration = cache.getConfiguration(Eh107Configuration.class);
    configuration.unwrap(CacheRuntimeConfiguration.class)
        .registerCacheEventListener(listener, EventOrdering.UNORDERED, EventFiring.ASYNCHRONOUS,
            EventType.EVICTED, EventType.REMOVED, EventType.EXPIRED);
  }

  return cache;
}