本文整理汇总了Java中javax.cache.CacheManager类的典型用法代码示例。如果您正苦于以下问题:Java CacheManager类的具体用法?Java CacheManager怎么用?Java CacheManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CacheManager类属于javax.cache包,在下文中一共展示了CacheManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: cacheExposesMetrics
import javax.cache.CacheManager; //导入依赖的package包/类
@ParameterizedTest
@MethodSource("cachingProviders")
void cacheExposesMetrics(CachingProvider provider) {
CacheManager cacheManager = provider.getCacheManager();
MutableConfiguration<Integer, String> configuration = new MutableConfiguration<>();
configuration.setTypes(Integer.class, String.class)
.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(ONE_HOUR))
.setStatisticsEnabled(true);
Cache<Integer, String> cache = cacheManager.createCache("a", configuration);
cache.put(1, "test");
MeterRegistry registry = new SimpleMeterRegistry();
JCacheMetrics.monitor(registry, cache, "jcache", emptyList());
assertThat(registry.mustFind("jcache.puts").tags("name", "a").gauge().value()).isEqualTo(1.0);
}
示例2: createCache
import javax.cache.CacheManager; //导入依赖的package包/类
/**
* Create a new cache from the given cache manager.
*
* @param cacheName Name of the cache.
* @param keyClass Type of the key class.
* @param valueClass Type of the value class.
* @param defaultExpiryTime Cache expire time in minutes.
* @param cacheConfigMap Cache config map.
* @param cacheManager Cache manager to use to create the cache.
* @param <K> Type of the Key.
* @param <V> Type of the Value.
* @return Created cache.
*/
public static <K, V> Cache<K, V> createCache(String cacheName, Class<K> keyClass, Class<V> valueClass,
int defaultExpiryTime, Map<String, CacheConfig> cacheConfigMap,
CacheManager cacheManager) {
Duration cacheExpiry = new Duration(TimeUnit.MINUTES, getExpireTime(cacheConfigMap, cacheName,
defaultExpiryTime));
boolean isStatisticsEnabled = false;
if (cacheConfigMap.get(cacheName) != null) {
isStatisticsEnabled = cacheConfigMap.get(cacheName).isStatisticsEnabled();
}
MutableConfiguration<K, V> configuration = new MutableConfiguration<>();
configuration.setStoreByValue(false)
.setTypes(keyClass, valueClass)
.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(cacheExpiry))
.setStatisticsEnabled(isStatisticsEnabled);
return cacheManager.createCache(cacheName, configuration);
}
示例3: getCacheManager
import javax.cache.CacheManager; //导入依赖的package包/类
@Override
public CacheManager getCacheManager(URI uri, ClassLoader classLoader, Properties properties)
{
URI _uri = (uri != null) ? uri : getDefaultURI();
ClassLoader _classLoader = (classLoader != null) ? classLoader : getDefaultClassLoader();
Properties _properties = (properties != null) ? properties : new Properties();
CacheManager newCacheManager = new GuavaCacheManager(_uri, _classLoader, _properties, this);
CacheManager oldCacheManager = cacheManagers.putIfAbsent(new Pair<>(_uri, _classLoader), newCacheManager);
if (oldCacheManager != null)
{
return oldCacheManager;
}
return newCacheManager;
}
示例4: close
import javax.cache.CacheManager; //导入依赖的package包/类
@Override
public void close()
{
Iterator<Map.Entry<Pair<URI, ClassLoader>, CacheManager>> i = cacheManagers.entrySet().iterator();
while (i.hasNext())
{
Map.Entry<Pair<URI, ClassLoader>, CacheManager> entry = i.next();
CacheManager cm = entry.getValue();
if (cm != null && !cm.isClosed())
{
cm.close();
}
i.remove();
}
}
示例5: testCacheManagerWithCustomClassLoader
import javax.cache.CacheManager; //导入依赖的package包/类
@Test
public void testCacheManagerWithCustomClassLoader()
throws Exception
{
CacheManager cacheManager1 = cachingProvider.getCacheManager();
CacheManager cacheManager2 = cachingProvider.getCacheManager(null, ClassLoader.getSystemClassLoader());
assertNotNull(cacheManager1);
assertNotNull(cacheManager2);
assertNotEquals(cacheManager1, cacheManager2);
cacheManager1.close();
cacheManager2.close();
assertTrue(cacheManager1.isClosed());
assertTrue(cacheManager2.isClosed());
}
示例6: testCacheManagerWithCustomProperties
import javax.cache.CacheManager; //导入依赖的package包/类
@Test
public void testCacheManagerWithCustomProperties()
throws Exception
{
Properties properties = new Properties();
properties.setProperty("concurrencyLevel", "1");
properties.setProperty("initialCapacity", "16");
CacheManager cacheManager1 = cachingProvider.getCacheManager();
CacheManager cacheManager2 = cachingProvider.getCacheManager(null, null, properties);
assertNotNull(cacheManager1);
assertNotNull(cacheManager2);
// properties do not form part of the identity of the CacheManager
assertEquals(cacheManager1, cacheManager2);
cacheManager1.close();
cacheManager2.close();
assertTrue(cacheManager1.isClosed());
assertTrue(cacheManager2.isClosed());
}
示例7: testCacheManagerWithCustomInvalidProperties
import javax.cache.CacheManager; //导入依赖的package包/类
@Test(expected = IllegalArgumentException.class)
public void testCacheManagerWithCustomInvalidProperties()
throws Exception
{
Properties properties = new Properties();
properties.setProperty("unknownProperty", "true");
try (CacheManager cacheManager = cachingProvider.getCacheManager(null, null, properties);)
{
assertNotNull(cacheManager);
MutableConfiguration configuration = new MutableConfiguration();
configuration.setStoreByValue(false);
cacheManager.createCache("test", configuration);
}
}
示例8: testCacheManagerWithCustomClassLoaderAndProperties
import javax.cache.CacheManager; //导入依赖的package包/类
@Test
public void testCacheManagerWithCustomClassLoaderAndProperties()
throws Exception
{
Properties properties = new Properties();
properties.setProperty("concurrencyLevel", "1");
properties.setProperty("initialCapacity", "16");
CacheManager cacheManager1 = cachingProvider.getCacheManager();
CacheManager cacheManager2 =
cachingProvider.getCacheManager(null, ClassLoader.getSystemClassLoader(), properties);
assertNotNull(cacheManager1);
assertNotNull(cacheManager2);
assertNotEquals(cacheManager1, cacheManager2);
cacheManager1.close();
cacheManager2.close();
assertTrue(cacheManager1.isClosed());
assertTrue(cacheManager2.isClosed());
}
示例9: testCreateCacheWithModifiedExpiryPolicy
import javax.cache.CacheManager; //导入依赖的package包/类
@Test
public void testCreateCacheWithModifiedExpiryPolicy()
{
CacheManager cacheManager = cachingProvider.getCacheManager();
MutableConfiguration<Number, Number> configuration = new MutableConfiguration<>();
configuration.setStoreByValue(false);
configuration.setExpiryPolicyFactory(ModifiedExpiryPolicy.factoryOf(Duration.ONE_DAY));
Cache cache = cacheManager.createCache("cache", configuration);
CompleteConfiguration actualConfiguration =
(CompleteConfiguration) cache.getConfiguration(CompleteConfiguration.class);
assertEquals(ModifiedExpiryPolicy.factoryOf(Duration.ONE_DAY), actualConfiguration.getExpiryPolicyFactory());
}
示例10: testCreateCacheWithTouchedExpiryPolicy
import javax.cache.CacheManager; //导入依赖的package包/类
@Test
public void testCreateCacheWithTouchedExpiryPolicy()
{
CacheManager cacheManager = cachingProvider.getCacheManager();
MutableConfiguration<Number, Number> configuration = new MutableConfiguration<>();
configuration.setStoreByValue(false);
configuration.setExpiryPolicyFactory(TouchedExpiryPolicy.factoryOf(Duration.ONE_MINUTE));
Cache cache = cacheManager.createCache("cache", configuration);
CompleteConfiguration actualConfiguration =
(CompleteConfiguration) cache.getConfiguration(CompleteConfiguration.class);
assertEquals(TouchedExpiryPolicy.factoryOf(Duration.ONE_MINUTE), actualConfiguration.getExpiryPolicyFactory());
}
示例11: getCacheManagerWithNonNullProperties
import javax.cache.CacheManager; //导入依赖的package包/类
@Test // org.jsr107.tck.CacheManagerTest.getCacheManager_nonNullProperties
public void getCacheManagerWithNonNullProperties()
{
// see https://github.com/jsr107/jsr107tck/issues/102
// make sure existing cache managers are closed and the non empty properties get picked up
try
{
Caching.getCachingProvider().close();
}
catch (CacheException ignore)
{
// ignore exception which may happen if the provider is not active
}
CachingProvider provider = Caching.getCachingProvider();
Properties properties = new Properties();
properties.put("dummy.com", "goofy");
provider.getCacheManager(provider.getDefaultURI(), provider.getDefaultClassLoader(), properties);
CacheManager manager = provider.getCacheManager();
assertEquals(properties, manager.getProperties());
}
示例12: call
import javax.cache.CacheManager; //导入依赖的package包/类
@Override
public Collection<ValueObject> call()
throws Exception
{
CachingProvider cachingProvider = Caching.getCachingProvider(GuavaCachingProvider.class.getName());
CacheManager cacheManager = cachingProvider.getCacheManager();
Cache<KeyObject, ValueObject> cache =
cacheManager.getCache("workerCache", KeyObject.class, ValueObject.class);
Set<ValueObject> values = new HashSet<>();
for (long id = 0; id < TEST_CACHE_SIZE; id++)
{
values.add(cache.get(new KeyObject(id)));
}
return values;
}
示例13: ehCacheManager
import javax.cache.CacheManager; //导入依赖的package包/类
@Bean
@Primary
public CacheManager ehCacheManager(JiraSyncConfig jiraSyncConfig) throws Exception {
CachingProvider cachingProvider = Caching.getCachingProvider();
EhcacheCachingProvider ehcacheCachingProvider = (EhcacheCachingProvider) cachingProvider;
CacheConfig cacheConfig = jiraSyncConfig.getCache();
CacheManager cacheManager = getCacheManager(ehcacheCachingProvider, cacheConfig);
boolean persistentCache = cacheConfig.isPersistent();
createCache(cacheManager, CACHE_NAME_PROJECTS, ONE_HOUR, persistentCache);
createCache(cacheManager, CACHE_NAME_PRIORITIES, ONE_HOUR, persistentCache);
createCache(cacheManager, CACHE_NAME_RESOLUTIONS, ONE_HOUR, persistentCache);
createCache(cacheManager, CACHE_NAME_VERSIONS, ONE_HOUR, persistentCache);
createCache(cacheManager, CACHE_NAME_FIELDS, ONE_HOUR, persistentCache);
createCache(cacheManager, CACHE_NAME_REMOTE_LINKS, ONE_HOUR, persistentCache);
createCache(cacheManager, CACHE_NAME_USERS, ONE_HOUR, persistentCache);
createCache(cacheManager, CACHE_NAME_FIELD_ALLOWED_VALUES, ONE_HOUR, persistentCache);
createCache(cacheManager, CACHE_NAME_MYSELF, ONE_HOUR, false);
createCache(cacheManager, CACHE_NAME_SERVER_INFO, THIRTY_SECONDS, false);
return cacheManager;
}
示例14: createCache
import javax.cache.CacheManager; //导入依赖的package包/类
private void createCache(CacheManager cacheManager, String cacheName, Duration timeToLive, boolean persistentCache) {
ResourcePoolsBuilder resourcePoolsBuilder = ResourcePoolsBuilder.heap(100);
if (persistentCache) {
resourcePoolsBuilder = resourcePoolsBuilder.disk(1, MemoryUnit.MB, true);
}
CacheConfiguration<Object, Object> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class,
resourcePoolsBuilder)
.withExpiry(Expirations.timeToLiveExpiration(timeToLive))
.build();
for (String cache : cacheManager.getCacheNames()) {
if (cache.equals(cacheName)) {
log.warn("cache '{}' already exists. skipping creation", cacheName);
return;
}
}
cacheManager.createCache(cacheName, Eh107Configuration.fromEhcacheCacheConfiguration(cacheConfiguration));
}
示例15: createManager
import javax.cache.CacheManager; //导入依赖的package包/类
public CacheManager createManager(Map<String, Configuration<?, ?>> configs, ShutdownManager shutdownManager) {
CachingProvider provider;
try {
// TODO: an explicit config property to pick explicit provider out of available choices... though probably
// pointless in most cases (do we realistically expect multiple providers on classpath?)
provider = Caching.getCachingProvider();
} catch (CacheException e) {
throw new RuntimeException("'bootique-jcache' doesn't bundle any JCache providers. " +
"You must place a JCache 1.0 provider on classpath explicitly.", e);
}
shutdownManager.addShutdownHook(provider);
CacheManager manager = getConfigUri().map(u -> provider.getCacheManager(u, null)).orElse(provider.getCacheManager());
shutdownManager.addShutdownHook(manager);
// now load contributed configs
configs.forEach(manager::createCache);
return manager;
}