javax.cache.Cache.getConfiguration()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(15.7k)|赞(0)|评价(0)|浏览(140)

本文整理了Java中javax.cache.Cache.getConfiguration()方法的一些代码示例,展示了Cache.getConfiguration()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cache.getConfiguration()方法的具体详情如下:
包路径:javax.cache.Cache
类名称:Cache
方法名:getConfiguration

Cache.getConfiguration介绍

[英]Provides a standard way to access the configuration of a cache using JCache configuration or additional proprietary configuration.

The returned value must be immutable.

If the provider's implementation does not support the specified class, the IllegalArgumentException is thrown.
[中]提供使用JCache配置或其他专有配置访问缓存配置的标准方法。
返回的值必须是不可变的。
如果提供程序的实现不支持指定的类,则引发IllegalArgumentException。

代码示例

代码示例来源:origin: ben-manes/caffeine

@SuppressWarnings("unchecked")
 private CompleteConfiguration<?, ?> configuration() {
  return cache.getConfiguration(CompleteConfiguration.class);
 }
}

代码示例来源:origin: redisson/redisson

@Override
public <K, V> Cache<K, V> getCache(String cacheName) {
  checkNotClosed();
  Cache<K, V> cache = (Cache<K, V>) getCache(cacheName, Object.class, Object.class);
  if (cache != null) {
    if (cache.getConfiguration(CompleteConfiguration.class).getKeyType() != Object.class) {
      throw new IllegalArgumentException("Wrong type of key for " + cacheName);
    }
    if (cache.getConfiguration(CompleteConfiguration.class).getValueType() != Object.class) {
      throw new IllegalArgumentException("Wrong type of value for " + cacheName);
    }
  }
  return cache;
}

代码示例来源:origin: redisson/redisson

@Override
public <K, V> Cache<K, V> getCache(String cacheName) {
  checkNotClosed();
  Cache<K, V> cache = (Cache<K, V>) getCache(cacheName, Object.class, Object.class);
  if (cache != null) {
    if (cache.getConfiguration(CompleteConfiguration.class).getKeyType() != Object.class) {
      throw new IllegalArgumentException("Wrong type of key for " + cacheName);
    }
    if (cache.getConfiguration(CompleteConfiguration.class).getValueType() != Object.class) {
      throw new IllegalArgumentException("Wrong type of value for " + cacheName);
    }
  }
  return cache;
}

代码示例来源:origin: apache/ignite

/** {@inheritDoc} */
@Override public <K, V> Cache<K, V> getCache(String cacheName, Class<K> keyType, Class<V> valType) {
  kernalGateway.readLock();
  try {
    Cache<K, V> cache = getCache0(cacheName);
    if (cache != null) {
      if(!keyType.isAssignableFrom(cache.getConfiguration(Configuration.class).getKeyType()))
        throw new ClassCastException();
      if(!valType.isAssignableFrom(cache.getConfiguration(Configuration.class).getValueType()))
        throw new ClassCastException();
    }
    return cache;
  }
  finally {
    kernalGateway.readUnlock();
  }
}

代码示例来源:origin: ben-manes/caffeine

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)));
 }
}

代码示例来源:origin: ben-manes/caffeine

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)));
 }
}

代码示例来源:origin: ben-manes/caffeine

@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);
}

代码示例来源:origin: ehcache/ehcache3

@Test
public void testTypeOverriding() throws Exception {
 CachingProvider provider = Caching.getCachingProvider();
 javax.cache.CacheManager cacheManager =
   provider.getCacheManager(this.getClass().getResource("/ehcache-107-types.xml").toURI(), getClass().getClassLoader());
 MutableConfiguration<Long, String> cache1Conf = new MutableConfiguration<>();
 cache1Conf.setTypes(Long.class, String.class);
 javax.cache.Cache<Long, String> cache = cacheManager.createCache("defaultCache", cache1Conf);
 @SuppressWarnings("unchecked")
 Configuration<Long, String> cache1CompleteConf = cache.getConfiguration(Configuration.class);
 assertThat(cache1CompleteConf.getKeyType(), is(equalTo(Long.class)));
 assertThat(cache1CompleteConf.getValueType(), is(equalTo(String.class)));
}

代码示例来源:origin: ehcache/ehcache3

@SuppressWarnings("unchecked")
public static void testJsr107EhcacheOsgi() throws Exception {
 CachingProvider cachingProvider = Caching.getCachingProvider("org.ehcache.jsr107.EhcacheCachingProvider", TestMethods.class.getClassLoader());
 CacheManager cacheManager = cachingProvider.getCacheManager(TestMethods.class.getResource("/org/ehcache/osgi/ehcache-107-osgi.xml").toURI(), TestMethods.class.getClassLoader());
 Cache<Long, Person> personCache = cacheManager.getCache("personCache", Long.class, Person.class);
 assertEquals(Person.class, personCache.getConfiguration(javax.cache.configuration.Configuration.class).getValueType());
}

代码示例来源:origin: ehcache/ehcache3

@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));
}

代码示例来源:origin: ehcache/ehcache3

@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));
}

代码示例来源:origin: ehcache/ehcache3

@Test
public void testRunTimeTypeLaxity() throws Exception {
 CachingProvider provider = Caching.getCachingProvider();
 javax.cache.CacheManager cacheManager =
   provider.getCacheManager(this.getClass().getResource("/ehcache-107-types.xml").toURI(), getClass().getClassLoader());
 MutableConfiguration<Long, String> cache1Conf = new MutableConfiguration<>();
 cache1Conf.setTypes(Long.class, String.class);
 javax.cache.Cache<Long, String> cache = cacheManager.createCache("cache1", cache1Conf);
 @SuppressWarnings("unchecked")
 Configuration<Long, String> cache1CompleteConf = cache.getConfiguration(Configuration.class);
 assertThat(cache1CompleteConf.getKeyType(), is(equalTo(Long.class)));
 assertThat(cache1CompleteConf.getValueType(), is(equalTo(String.class)));
 try {
  cacheManager.getCache("cache1");
 } finally {
  cacheManager.destroyCache("cache1");
  cacheManager.close();
 }
}

代码示例来源:origin: ehcache/ehcache3

@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));
}

代码示例来源:origin: ehcache/ehcache3

@Test
@SuppressWarnings("unchecked")
public void testCompileTimeTypeSafety() throws Exception {
 CachingProvider provider = Caching.getCachingProvider();
 javax.cache.CacheManager cacheManager =
   provider.getCacheManager(this.getClass().getResource("/ehcache-107-types.xml").toURI(), getClass().getClassLoader());
 MutableConfiguration<Long, String> cache1Conf = new MutableConfiguration<>();
 javax.cache.Cache<Long, String> cache = cacheManager.createCache("cache1", cache1Conf);
 cache.put(1l, "one");
 cache.put(2l, "two");
 Configuration<Object, Object> cache1CompleteConf = cache.getConfiguration(Configuration.class);
 //This ensures that we have compile time type safety, i.e when configuration does not have types defined but
 // what you get cache as should work.
 assertThat(cache1CompleteConf.getKeyType(), is(equalTo(Object.class)));
 assertThat(cache1CompleteConf.getValueType(), is(equalTo(Object.class)));
 assertThat(cache.get(1l), is(equalTo("one")));
 assertThat(cache.get(2l), is(equalTo("two")));
 javax.cache.Cache<String, String> second = cacheManager.getCache("cache1");
 second.put("3","three");
 assertThat(second.get("3"), is(equalTo("three")));
 cacheManager.destroyCache("cache1");
 cacheManager.close();
}

代码示例来源:origin: ehcache/ehcache3

@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));
}

代码示例来源:origin: ehcache/ehcache3

@Test
@SuppressWarnings("unchecked")
public void testUsingEhcacheConfiguration() throws Exception {
 // tag::ehcacheBasedConfigurationExample[]
 CacheConfiguration<Long, String> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
   ResourcePoolsBuilder.heap(10)).build(); // <1>
 Cache<Long, String> cache = cacheManager.createCache("myCache",
   Eh107Configuration.fromEhcacheCacheConfiguration(cacheConfiguration)); // <2>
 Eh107Configuration<Long, String> configuration = cache.getConfiguration(Eh107Configuration.class);
 configuration.unwrap(CacheConfiguration.class); // <3>
 configuration.unwrap(CacheRuntimeConfiguration.class); // <4>
 try {
  cache.getConfiguration(CompleteConfiguration.class); // <5>
  throw new AssertionError("IllegalArgumentException expected");
 } catch (IllegalArgumentException iaex) {
  // Expected
 }
 // end::ehcacheBasedConfigurationExample[]
}

代码示例来源:origin: ehcache/ehcache3

@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));
}

代码示例来源:origin: ehcache/ehcache3

@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));
}

代码示例来源:origin: ehcache/ehcache3

@Test
public void testEnableCacheLevelProgrammatic() throws Exception {
 CacheManager cacheManager = provider.getCacheManager();
 CacheConfigurationBuilder<Long, String> configurationBuilder = newCacheConfigurationBuilder(Long.class, String.class, heap(10))
   .add(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));
}

代码示例来源:origin: ehcache/ehcache3

@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));
 }
}

相关文章