本文整理汇总了Java中org.ehcache.config.builders.CacheConfigurationBuilder类的典型用法代码示例。如果您正苦于以下问题:Java CacheConfigurationBuilder类的具体用法?Java CacheConfigurationBuilder怎么用?Java CacheConfigurationBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CacheConfigurationBuilder类属于org.ehcache.config.builders包,在下文中一共展示了CacheConfigurationBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: before
import org.ehcache.config.builders.CacheConfigurationBuilder; //导入依赖的package包/类
@Before
public void before() throws Exception {
CachingProvider cachingProvider = Caching.getCachingProvider();
EhcacheCachingProvider ehcacheProvider = (EhcacheCachingProvider) cachingProvider;
DefaultConfiguration configuration = new DefaultConfiguration(ehcacheProvider.getDefaultClassLoader(),
new DefaultPersistenceConfiguration(diskPath.newFolder()));
CacheConfiguration<Integer, String> cacheConfiguration =
CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, String.class, resources).build();
configuration.addCacheConfiguration("cache", cacheConfiguration);
cacheManager = ehcacheProvider.getCacheManager(ehcacheProvider.getDefaultURI(), configuration);
EhcacheManager ehcacheManager = cacheManager.unwrap(EhcacheManager.class);
Field field = EhcacheManager.class.getDeclaredField("serviceLocator");
field.setAccessible(true);
@SuppressWarnings("unchecked")
ServiceProvider<Service> serviceProvider = (ServiceProvider<Service>)field.get(ehcacheManager);
StatisticsService statisticsService = serviceProvider.getService(StatisticsService.class);
cache = cacheManager.getCache("cache", Integer.class, String.class);
cacheStatistics = statisticsService.getCacheStatistics("cache");
}
示例2: newCacheConfigurationBuilder_oneExpiryThreeResourcePools
import org.ehcache.config.builders.CacheConfigurationBuilder; //导入依赖的package包/类
@Test
public void newCacheConfigurationBuilder_oneExpiryThreeResourcePools() throws Exception {
//given
addProp(CacheConfigurationServiceImpl.PROP_KEY_SUFFIX_TIME_TO_IDLE_SECS,60);
addProp(CacheConfigurationServiceImpl.PROP_KEY_SUFFIX_MAX_ENTRIES_HEAP,1);
addProp(CacheConfigurationServiceImpl.PROP_KEY_SUFFIX_MAX_MB_OFF_HEAP,2);
addProp(CacheConfigurationServiceImpl.PROP_KEY_SUFFIX_MAX_MB_DISK,3);
//when
CacheConfigurationBuilder<String,String> builder = cacheConfigurationService.newCacheConfigurationBuilder(CACHE_NAME, String.class, String.class);
CacheConfiguration<String, String> cacheConfiguration = builder.build();
//then
assertThat(cacheConfiguration.getResourcePools().getResourceTypeSet().size()).isEqualTo(3);
assertThat(cacheConfiguration.getExpiry()).isEqualTo(Expirations.timeToIdleExpiration(Duration.of(60, TimeUnit.SECONDS)));
}
示例3: createCache
import org.ehcache.config.builders.CacheConfigurationBuilder; //导入依赖的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));
}
示例4: testTem
import org.ehcache.config.builders.CacheConfigurationBuilder; //导入依赖的package包/类
@Test
public void testTem() {
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.withCache("preConfigured",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10)))
.build();
cacheManager.init();
Cache<Long, String> preConfigured =
cacheManager.getCache("preConfigured", Long.class, String.class);
Cache<Long, String> myCache = cacheManager.createCache("myCache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10)));
myCache.put(1L, "da one!");
myCache.putIfAbsent(0L, "ee");
String value = myCache.get(1L);
System.out.println("Value is " + value);
cacheManager.removeCache("preConfigured");
cacheManager.close();
}
示例5: load
import org.ehcache.config.builders.CacheConfigurationBuilder; //导入依赖的package包/类
public void load(String vocabFile, float cacheSize) throws IOException {
int size = (int) (cacheSize * createVirtualMem(vocabFile, cacheSize));
this.mCacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.withCache("preConfigured", CacheConfigurationBuilder
.newCacheConfigurationBuilder(String.class, double[].class, ResourcePoolsBuilder.heap(size))
.build())
.build(true);
this.mCache = mCacheManager.getCache("preConfigured", String.class, double[].class);
this.UNK = mWEVirtualMemory.swap(mCache, size);
if (this.UNK == null)
throw new IOException("Vector <UNK> not found!");
}
示例6: ServerInterfaceCache
import org.ehcache.config.builders.CacheConfigurationBuilder; //导入依赖的package包/类
private ServerInterfaceCache() {
this.cacheManager = CacheManagerBuilder.newCacheManagerBuilder().withCache("airports",
CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(10, EntryUnit.ENTRIES)
.offheap(10, MemoryUnit.MB))
)
.build(true);
this.airportCache = this.cacheManager.getCache("airports", String.class, String.class);
this.airplaneCache = this.cacheManager.createCache("airplanes",
CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(10, EntryUnit.ENTRIES)
.offheap(10, MemoryUnit.MB)).build());
this.flightCache = this.cacheManager.createCache("flights",
CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(10, EntryUnit.ENTRIES)
.offheap(10, MemoryUnit.MB)).build());
}
示例7: cacheManager
import org.ehcache.config.builders.CacheConfigurationBuilder; //导入依赖的package包/类
@Bean
public CacheManager cacheManager(@Autowired Config config) {
long ttl = Long.valueOf(config.getProperties().getProperty(Config.PROP_CACHE_TTL));
double pctOfHeap = Double.valueOf(config.getProperties().getProperty(Config.PROP_CACHE_SIZE));
long cacheSizeMB = new Double(Runtime.getRuntime().maxMemory() * pctOfHeap / 1048576.0).longValue();
LogFactory.getLog(getClass()).info(
String.format("Initializing cache TTL=%d secs, size=%d MB (%.2f percent of max heap)",
ttl, cacheSizeMB, pctOfHeap * 100));
org.ehcache.config.CacheConfiguration<Object, Object> cacheConfiguration = CacheConfigurationBuilder
.newCacheConfigurationBuilder(Object.class, Object.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(cacheSizeMB, MemoryUnit.MB))
.withExpiry(Expirations.timeToLiveExpiration(new org.ehcache.expiry.Duration(ttl, TimeUnit.SECONDS)))
.build();
Map<String, CacheConfiguration<?, ?>> caches = new HashMap<>();
caches.put(CACHE_DEFAULT, cacheConfiguration);
EhcacheCachingProvider provider = (EhcacheCachingProvider) javax.cache.Caching.getCachingProvider();
// when our cacheManager bean is re-created several times for
// diff test configurations, this provider seems to hang on to state
// causing cache settings to not be right. so we always close().
provider.close();
DefaultConfiguration configuration = new DefaultConfiguration(
caches, provider.getDefaultClassLoader());
return new JCacheCacheManager(
provider.getCacheManager(provider.getDefaultURI(), configuration));
}
示例8: getCache
import org.ehcache.config.builders.CacheConfigurationBuilder; //导入依赖的package包/类
@Override
public <K, V> Optional<Cache<K, V>> getCache(String name, Class<K> keyType, Class<V> valueType) {
Cache<K, V> cache = null;
Optional<CacheConfig> optionalCacheConfig = this.cacheConfigs.entrySet().stream().filter(entry -> {
return entry.getValue().getCacheName().equals(name); }).map(entry -> { return entry.getValue(); }).findFirst();
if (optionalCacheConfig.isPresent()) {
org.ehcache.Cache<K, V> ehcache = this.cacheMgr.getCache(name, keyType, valueType);
if (ehcache == null) {
try {
CacheConfig cacheConfig = optionalCacheConfig.get();
Expiry<Object, Object> timeToLiveExpiration = Expirations
.timeToLiveExpiration(new Duration((long) cacheConfig.getTtlSeconds(), TimeUnit.SECONDS));
cache = new EhcacheCache<>(this.cacheMgr.createCache(cacheConfig.getCacheName(),
CacheConfigurationBuilder
.newCacheConfigurationBuilder(keyType, valueType, ResourcePoolsBuilder.heap(1000l))
.withExpiry(timeToLiveExpiration).build()));
} catch (Exception ex) {
LOGGER.error("Could not get Cache with name: [{}], Exception!!", name, ex);
}
}
} else {
LOGGER.warn("CacheConfig against name [{}] doesn't exist, please create it first!", name);
}
return Optional.ofNullable(cache);
}
示例9: createInMemoryCacheManager
import org.ehcache.config.builders.CacheConfigurationBuilder; //导入依赖的package包/类
private CacheManager createInMemoryCacheManager() {
long cacheSize = properties.getCache().getEhcache().getSize();
long ttl = properties.getCache().getEhcache().getTimeToLiveSeconds();
org.ehcache.config.CacheConfiguration<Object, Object> cacheConfiguration = CacheConfigurationBuilder
.newCacheConfigurationBuilder(Object.class, Object.class, ResourcePoolsBuilder
.newResourcePoolsBuilder()
.heap(cacheSize))
.withExpiry(Expirations.timeToLiveExpiration(new org.ehcache.expiry.Duration(ttl, TimeUnit.SECONDS)))
.build();
Map<String, org.ehcache.config.CacheConfiguration<?, ?>> caches = createCacheConfigurations(cacheConfiguration);
EhcacheCachingProvider provider = getCachingProvider();
DefaultConfiguration configuration = new DefaultConfiguration(caches, provider.getDefaultClassLoader());
return provider.getCacheManager(provider.getDefaultURI(), configuration);
}
示例10: createRegistry
import org.ehcache.config.builders.CacheConfigurationBuilder; //导入依赖的package包/类
@Override
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry registry = super.createRegistry();
registry.bind(
"myConf",
CacheConfigurationBuilder.newCacheConfigurationBuilder(
String.class,
String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(100, EntryUnit.ENTRIES)
.offheap(1, MemoryUnit.MB))
.build()
);
return registry;
}
示例11: EhCacheDao
import org.ehcache.config.builders.CacheConfigurationBuilder; //导入依赖的package包/类
/**
* To initiate EhCacheDao with cacheManager and cache.
*
* @param cacheName
* name of the cache.
* @param cacheSize
* cache size.
*/
public EhCacheDao(final String cacheName, final long cacheSize) {
setCacheManager(CacheManagerBuilder.newCacheManagerBuilder()
.using(PooledExecutionServiceConfigurationBuilder.newPooledExecutionServiceConfigurationBuilder()
.defaultPool("dflt", 0, 10)
.pool("defaultEventPool", 1, 3)
.pool("cache2Pool", 2, 2)
.build())
.withDefaultEventListenersThreadPool("defaultEventPool")
.withCache(cacheName, CacheConfigurationBuilder
.newCacheConfigurationBuilder(Long.class, byte[].class, ResourcePoolsBuilder.heap(cacheSize))
.build())
.build(true));
setCache(cacheManager.getCache(cacheName, Long.class, byte[].class));
}
示例12: testUpdateResources
import org.ehcache.config.builders.CacheConfigurationBuilder; //导入依赖的package包/类
@Test
public void testUpdateResources() {
CacheConfiguration<Long, String> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(10L, EntryUnit.ENTRIES).disk(10, MemoryUnit.MB).build()).build();
final CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.with(new CacheManagerPersistenceConfiguration(new File(System.getProperty("java.io.tmpdir") + "/myData")))
.withCache("cache", cacheConfiguration).build(true);
Cache<Long, String> cache = cacheManager.getCache("cache", Long.class, String.class);
ResourcePoolsBuilder poolsBuilder = ResourcePoolsBuilder.newResourcePoolsBuilder();
poolsBuilder = poolsBuilder.heap(20L, EntryUnit.ENTRIES);
ResourcePools pools = poolsBuilder.build();
cache.getRuntimeConfiguration().updateResourcePools(pools);
assertThat(cache.getRuntimeConfiguration().getResourcePools()
.getPoolForResource(ResourceType.Core.HEAP).getSize(), is(20L));
pools = poolsBuilder.build();
cache.getRuntimeConfiguration().updateResourcePools(pools);
assertThat(cache.getRuntimeConfiguration().getResourcePools()
.getPoolForResource(ResourceType.Core.HEAP).getSize(), is(20L));
cacheManager.close();
}
示例13: testCanGetCapabilities
import org.ehcache.config.builders.CacheConfigurationBuilder; //导入依赖的package包/类
@Test
public void testCanGetCapabilities() {
CacheConfiguration<Long, String> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, heap(10))
.build();
ManagementRegistryService managementRegistry = new DefaultManagementRegistryService(new DefaultManagementRegistryConfiguration().setCacheManagerAlias("myCM"));
CacheManager cacheManager1 = CacheManagerBuilder.newCacheManagerBuilder()
.withCache("aCache", cacheConfiguration)
.using(managementRegistry)
.build(true);
assertThat(managementRegistry.getCapabilities()).hasSize(4);
assertThat(new ArrayList<Capability>(managementRegistry.getCapabilities()).get(0).getName()).isEqualTo("ActionsCapability");
assertThat(new ArrayList<Capability>(managementRegistry.getCapabilities()).get(1).getName()).isEqualTo("SettingsCapability");
assertThat(new ArrayList<Capability>(managementRegistry.getCapabilities()).get(2).getName()).isEqualTo("StatisticCollectorCapability");
assertThat(new ArrayList<Capability>(managementRegistry.getCapabilities()).get(3).getName()).isEqualTo("StatisticsCapability");
assertThat(new ArrayList<Capability>(managementRegistry.getCapabilities()).get(0).getDescriptors()).hasSize(4);
assertThat(new ArrayList<Capability>(managementRegistry.getCapabilities()).get(3).getDescriptors()).hasSize(ONHEAP_DESCRIPTORS.size() + CACHE_DESCRIPTORS.size());
assertThat(new ArrayList<Capability>(managementRegistry.getCapabilities()).get(0).getCapabilityContext().getAttributes()).hasSize(2);
assertThat(new ArrayList<Capability>(managementRegistry.getCapabilities()).get(3).getCapabilityContext().getAttributes()).hasSize(2);
cacheManager1.close();
}
示例14: testTerminationBeforeCacheManagerCloseWithCaches
import org.ehcache.config.builders.CacheConfigurationBuilder; //导入依赖的package包/类
@Test
@Ignore("Need to decide if we close cache entity in a daemon thread")
public void testTerminationBeforeCacheManagerCloseWithCaches() throws Exception {
CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
CacheManagerBuilder.newCacheManagerBuilder()
.with(ClusteringServiceConfigurationBuilder.cluster(cluster.getConnectionURI().resolve("/MyCacheManagerName"))
.autoCreate()
.defaultServerResource("primary-server-resource"))
.withCache("simple-cache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.with(ClusteredResourcePoolBuilder.clusteredDedicated(4, MemoryUnit.MB))));
final PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
cacheManager.init();
cluster.getClusterControl().terminateAllServers();
new TimeLimitedTask<Void>(5, TimeUnit.SECONDS) {
@Override
Void runTask() throws Exception {
cacheManager.close();
return null;
}
}.run();
}
示例15: startServers
import org.ehcache.config.builders.CacheConfigurationBuilder; //导入依赖的package包/类
@Before
public void startServers() throws Exception {
CLUSTER.getClusterControl().startAllServers();
CLUSTER.getClusterControl().waitForActive();
CLUSTER.getClusterControl().waitForRunningPassivesInStandby();
final CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder
= CacheManagerBuilder.newCacheManagerBuilder()
.with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.getConnectionURI().resolve("/cm-replication"))
.timeouts(TimeoutsBuilder.timeouts() // we need to give some time for the failover to occur
.read(Duration.ofMinutes(1))
.write(Duration.ofMinutes(1)))
.autoCreate()
.defaultServerResource("primary-server-resource"));
CACHE_MANAGER = clusteredCacheManagerBuilder.build(true);
CacheConfiguration<Long, String> config = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder().heap(100, EntryUnit.ENTRIES)
.with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 4, MemoryUnit.MB)))
.add(ClusteredStoreConfigurationBuilder.withConsistency(cacheConsistency))
.build();
CACHE1 = CACHE_MANAGER.createCache("clustered-cache", config);
CACHE2 = CACHE_MANAGER.createCache("another-cache", config);
}