javax.cache.Caching.getCache()方法的使用及代码示例

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

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

Caching.getCache介绍

[英]A convenience that method that looks up a managed Cache given its name. using the default CachingProvider and CacheManager. For the full range of Cache look up methods see CacheManager.

This method must be used for Caches that were configured with runtime key and value types. Use CacheManager#getCache(String) for Caches where these were not specified.

Implementations must ensure that the key and value types are the same as those configured for the Cache prior to returning from this method.

Implementations may further perform type checking on mutative cache operations and throw a ClassCastException if these checks fail.

Implementations that support declarative mechanisms for pre-configuring Caches may return a pre-configured Cache instead of null.
[中]一个方便的方法是查找给定名称的托管缓存。使用默认值CachingProviderCacheManager。有关Cache查找方法的完整范围,请参阅CacheManager。
此方法必须用于配置了运行时键和值类型的缓存。对未指定缓存的缓存使用CacheManager#getCache(字符串)。
在从该方法返回之前,实现必须确保键和值类型与为缓存配置的键和值类型相同。
实现可以进一步对可变缓存操作执行类型检查,如果这些检查失败,则抛出ClassCastException。
支持预配置缓存的声明性机制的实现可能返回预配置的缓存,而不是null

代码示例

代码示例来源:origin: com.github.bordertech.taskmaster/taskmaster-cache-helper

@Override
public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass,
    final Class<V> valueClass, final Configuration<K, V> config) {
  Cache<K, V> cache = Caching.getCache(name, keyClass, valueClass);
  if (cache == null) {
    final CacheManager mgr = Caching.getCachingProvider().getCacheManager();
    cache = mgr.createCache(name, config);
  }
  return cache;
}

代码示例来源:origin: com.github.bordertech.taskmaster/taskmaster

@Override
public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass,
    final Class<V> valueClass, final Configuration<K, V> config) {
  Cache<K, V> cache = Caching.getCache(name, keyClass, valueClass);
  if (cache == null) {
    final CacheManager mgr = Caching.getCachingProvider().getCacheManager();
    cache = mgr.createCache(name, config);
  }
  return cache;
}

代码示例来源:origin: javax.cache/cache-tests

@Test
public void cachingProviderGetNonExistentCache() {
 String name = "nonExistentCache";
 Cache cache = Caching.getCache(name, Long.class, String.class);
 assertNull(null, cache);
}

代码示例来源:origin: com.github.bordertech.taskmaster/taskmaster-cache-helper

@Override
public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass,
    final Class<V> valueClass, final Duration duration) {
  Cache<K, V> cache = Caching.getCache(name, keyClass, valueClass);
  if (cache == null) {
    final CacheManager mgr = Caching.getCachingProvider().getCacheManager();
    MutableConfiguration<K, V> config = new MutableConfiguration<>();
    config.setTypes(keyClass, valueClass);
    config.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(duration));
    cache = mgr.createCache(name, config);
  }
  return cache;
}

代码示例来源:origin: com.github.bordertech.taskmaster/taskmaster

@Override
public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass,
    final Class<V> valueClass, final Duration duration) {
  Cache<K, V> cache = Caching.getCache(name, keyClass, valueClass);
  if (cache == null) {
    final CacheManager mgr = Caching.getCachingProvider().getCacheManager();
    MutableConfiguration<K, V> config = new MutableConfiguration<>();
    config.setTypes(keyClass, valueClass);
    config.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(duration));
    cache = mgr.createCache(name, config);
  }
  return cache;
}

代码示例来源:origin: com.github.bordertech.wcomponents/wcomponents-core

/**
 * @return the cache instance
 */
protected synchronized Cache<Object, Resource> getCache() {
  Cache<Object, Resource> cache = Caching.getCache(CACHE_NAME, Object.class, Resource.class);
  if (cache == null) {
    final CacheManager mgr = Caching.getCachingProvider().getCacheManager();
    MutableConfiguration<Object, Resource> config = new MutableConfiguration<>();
    config.setTypes(Object.class, Resource.class);
    config.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(new Duration(TimeUnit.HOURS, 12)));
    // Velocity template classes are not serializable so use by ref.
    config.setStoreByValue(false);
    cache = mgr.createCache(CACHE_NAME, config);
  }
  return cache;
}

代码示例来源:origin: com.github.bordertech.wcomponents/wcomponents-core

/**
 * @return the cache instance
 */
protected synchronized Cache<TemplateSource, Template> getCache() {
  Cache<TemplateSource, Template> cache = Caching.getCache(CACHE_NAME, TemplateSource.class, Template.class);
  if (cache == null) {
    final CacheManager mgr = Caching.getCachingProvider().getCacheManager();
    MutableConfiguration<TemplateSource, Template> config = new MutableConfiguration<>();
    config.setTypes(TemplateSource.class, Template.class);
    config.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(new Duration(TimeUnit.HOURS, 12)));
    // Handlebars template classes are not serializable so use by ref.
    config.setStoreByValue(false);
    cache = mgr.createCache(CACHE_NAME, config);
  }
  return cache;
}

代码示例来源:origin: com.github.bordertech.wcomponents/wcomponents-core

/**
 * @return the cache instance
 */
protected synchronized Cache<String, String> getCache() {
  Cache<String, String> cache = Caching.getCache(CACHE_NAME, String.class, String.class);
  if (cache == null) {
    final CacheManager mgr = Caching.getCachingProvider().getCacheManager();
    MutableConfiguration<String, String> config = new MutableConfiguration<>();
    config.setTypes(String.class, String.class);
    config.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(new Duration(TimeUnit.HOURS, 12)));
    // No need to serialize the result
    config.setStoreByValue(false);
    cache = mgr.createCache(CACHE_NAME, config);
  }
  return cache;
}

代码示例来源:origin: javax.cache/cache-tests

@Test(expected = NullPointerException.class)
public void getCacheNullValueClass() {
 String name = "c1";
 CacheManager manager = Caching.getCachingProvider().getCacheManager();
 manager.createCache(name, new MutableConfiguration().setTypes(Long.class, String.class));
 try {
  Caching.getCache(name, Long.class, null);
 } finally {
  manager.destroyCache(name);
 }
}

代码示例来源:origin: javax.cache/cache-tests

@Test
public void cachingProviderGetCache() {
 String name = "c1";
 getCacheManager().createCache(name, new MutableConfiguration().setTypes(Long.class, String.class));
 Cache cache = Caching.getCache(name, Long.class, String.class);
 assertEquals(name, cache.getName());
}

代码示例来源:origin: javax.cache/cache-tests

@Test
public void cachingProviderGetCache() {
 String name = "c1";
 Caching.getCachingProvider().getCacheManager().createCache(name, new MutableConfiguration().setTypes(Long.class, String.class));
 Cache cache = Caching.getCache(name, Long.class, String.class);
 assertEquals(name, cache.getName());
 Caching.getCachingProvider().getCacheManager().destroyCache(name);
}

相关文章