本文整理汇总了Java中org.ehcache.config.CacheConfiguration类的典型用法代码示例。如果您正苦于以下问题:Java CacheConfiguration类的具体用法?Java CacheConfiguration怎么用?Java CacheConfiguration使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CacheConfiguration类属于org.ehcache.config包,在下文中一共展示了CacheConfiguration类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: before
import org.ehcache.config.CacheConfiguration; //导入依赖的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.CacheConfiguration; //导入依赖的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.CacheConfiguration; //导入依赖的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: cacheManager
import org.ehcache.config.CacheConfiguration; //导入依赖的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));
}
示例5: init
import org.ehcache.config.CacheConfiguration; //导入依赖的package包/类
@Before
public void init() {
CacheConfiguration<Long, String> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, heap(10))
.build();
service = new DefaultSharedManagementService();
cacheManager1 = CacheManagerBuilder.newCacheManagerBuilder()
.withCache("aCache1", cacheConfiguration)
.using(service)
.using(config1 = new DefaultManagementRegistryConfiguration().setCacheManagerAlias("myCM1"))
.build(true);
cacheManager2 = CacheManagerBuilder.newCacheManagerBuilder()
.withCache("aCache2", cacheConfiguration)
.withCache("aCache3", cacheConfiguration)
.using(service)
.using(config2 = new DefaultManagementRegistryConfiguration().setCacheManagerAlias("myCM2"))
.build(true);
// this serie of calls make sure the registry still works after a full init / close / init loop
cacheManager1.close();
cacheManager1.init();
cacheManager2.close();
cacheManager2.init();
}
示例6: testCopyValueOnRead
import org.ehcache.config.CacheConfiguration; //导入依赖的package包/类
@Test
public void testCopyValueOnRead() throws Exception {
CacheConfiguration<Long, Person> cacheConfiguration = baseConfig
.add(new DefaultCopierConfiguration<>(PersonOnReadCopier.class, DefaultCopierConfiguration.Type.VALUE))
.build();
Cache<Long, Person> cache = cacheManager.createCache("cache", cacheConfiguration);
Person original = new Person("Bar", 24);
cache.put(1l, original);
Person retrieved = cache.get(1l);
assertNotSame(original, retrieved);
assertThat(retrieved.name, equalTo("Bar"));
assertThat(retrieved.age, equalTo(24));
original.age = 56;
retrieved = cache.get(1l);
assertThat(retrieved.age, equalTo(56));
assertNotSame(cache.get(1l), cache.get(1l));
}
示例7: testCopyValueOnWrite
import org.ehcache.config.CacheConfiguration; //导入依赖的package包/类
@Test
public void testCopyValueOnWrite() throws Exception {
CacheConfiguration<Long, Person> cacheConfiguration = baseConfig
.add(new DefaultCopierConfiguration<>(PersonOnWriteCopier.class, DefaultCopierConfiguration.Type.VALUE))
.build();
Cache<Long, Person> cache = cacheManager.createCache("cache", cacheConfiguration);
Person original = new Person("Bar", 24);
cache.put(1l, original);
Person retrieved = cache.get(1l);
assertNotSame(original, retrieved);
assertThat(retrieved.name, equalTo("Bar"));
assertThat(retrieved.age, equalTo(24));
original.age = 56;
retrieved = cache.get(1l);
assertThat(retrieved.age, equalTo(24));
assertSame(cache.get(1l), cache.get(1l));
}
示例8: testIdentityCopier
import org.ehcache.config.CacheConfiguration; //导入依赖的package包/类
@Test
public void testIdentityCopier() throws Exception {
CacheConfiguration<Long, Person> cacheConfiguration = baseConfig.build();
Cache<Long, Person> cache = cacheManager.createCache("cache", cacheConfiguration);
Person original = new Person("Bar", 24);
cache.put(1l, original);
Person retrieved = cache.get(1l);
assertSame(original, retrieved);
original.age = 25;
retrieved = cache.get(1l);
assertSame(original, retrieved);
assertSame(cache.get(1l), cache.get(1l));
}
示例9: testSerializingCopier
import org.ehcache.config.CacheConfiguration; //导入依赖的package包/类
@Test
public void testSerializingCopier() throws Exception {
CacheConfiguration<Long, Person> cacheConfiguration = baseConfig
.add(new DefaultCopierConfiguration<>(SerializingCopier.<Person>asCopierClass(), DefaultCopierConfiguration.Type.VALUE))
.add(new DefaultSerializerConfiguration<>(PersonSerializer.class, DefaultSerializerConfiguration.Type.VALUE))
.build();
Cache<Long, Person> cache = cacheManager.createCache("cache", cacheConfiguration);
Person original = new Person("Bar", 24);
cache.put(1l, original);
Person retrieved = cache.get(1l);
assertNotSame(original, retrieved);
assertThat(retrieved.name, equalTo("Bar"));
assertThat(retrieved.age, equalTo(24));
original.age = 56;
retrieved = cache.get(1l);
assertThat(retrieved.age, equalTo(24));
assertNotSame(cache.get(1l), cache.get(1l));
}
示例10: testOverSizedObjectPutFailsWithOnHeapAsAuthority
import org.ehcache.config.CacheConfiguration; //导入依赖的package包/类
@Test
public void testOverSizedObjectPutFailsWithOnHeapAsAuthority() {
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.withDefaultSizeOfMaxObjectSize(500, MemoryUnit.B).build(true);
CacheConfiguration<String, String> cacheConfiguration = CacheConfigurationBuilder
.newCacheConfigurationBuilder(String.class, String.class,
newResourcePoolsBuilder()
.heap(100, MemoryUnit.KB).build())
.build();
Cache<String, String> cache = cacheManager.createCache("cache",
cacheConfiguration);
cache.put("key1", getOverSizedObject());
assertThat(cache.get("key1"), nullValue());
cache.put("key1", "value1");
cache.replace("key1", getOverSizedObject());
assertThat(cache.get("key1"), nullValue());
}
示例11: testPutAll_without_cache_writer
import org.ehcache.config.CacheConfiguration; //导入依赖的package包/类
@Test
public void testPutAll_without_cache_writer() throws Exception {
CacheConfigurationBuilder cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
heap(100));
CacheConfiguration<String, String> cacheConfiguration = cacheConfigurationBuilder.build();
CacheManagerBuilder<CacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder();
CacheManager cacheManager = managerBuilder.withCache("myCache", cacheConfiguration).build(true);
Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class);
HashMap<String, String> stringStringHashMap = new HashMap<>();
for (int i = 0; i < 3; i++) {
stringStringHashMap.put("key" + i, "value" + i);
}
// the call to putAll
myCache.putAll(stringStringHashMap);
for (int i = 0; i < 3; i++) {
assertThat(myCache.get("key" + i), is("value" + i));
}
}
示例12: clusteredCacheManagerWithDynamicallyAddedCacheExample
import org.ehcache.config.CacheConfiguration; //导入依赖的package包/类
@Test
public void clusteredCacheManagerWithDynamicallyAddedCacheExample() throws Exception {
// tag::clusteredCacheManagerWithDynamicallyAddedCacheExample[]
CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder
= CacheManagerBuilder.newCacheManagerBuilder()
.with(ClusteringServiceConfigurationBuilder.cluster(URI.create("terracotta://localhost/my-application"))
.autoCreate()
.defaultServerResource("primary-server-resource")
.resourcePool("resource-pool-a", 28, MemoryUnit.MB));
PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
cacheManager.init();
try {
CacheConfiguration<Long, String> config = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 2, MemoryUnit.MB))).build();
Cache<Long, String> cache = cacheManager.createCache("clustered-cache", config);
} finally {
cacheManager.close();
}
// end::clusteredCacheManagerWithDynamicallyAddedCacheExample[]
}
示例13: before
import org.ehcache.config.CacheConfiguration; //导入依赖的package包/类
@Before
public void before() throws Exception {
CacheConfiguration<Integer, String> cacheConfiguration =
CacheConfigurationBuilder
.newCacheConfigurationBuilder(Integer.class, String.class, resources)
.withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofMillis(TIME_TO_EXPIRATION)))
.build();
StatisticsService statisticsService = new DefaultStatisticsService();
cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.withCache("cache", cacheConfiguration)
.using(new DefaultPersistenceConfiguration(diskPath.newFolder()))
.using(statisticsService)
.using(new TimeSourceConfiguration(timeSource))
.build(true);
cache = cacheManager.getCache("cache", Integer.class, String.class);
// Get the tier statistic.
tierStatistics = statisticsService.getCacheStatistics("cache")
.getTierStatistics().get(tierName);
}
示例14: before
import org.ehcache.config.CacheConfiguration; //导入依赖的package包/类
@Before
public void before() throws Exception {
CacheConfiguration<Integer, String> cacheConfiguration =
CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, String.class, resources).build();
StatisticsService statisticsService = new DefaultStatisticsService();
cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.withCache("cache", cacheConfiguration)
.using(new DefaultPersistenceConfiguration(diskPath.newFolder()))
.using(statisticsService)
.build(true);
cache = cacheManager.getCache("cache", Integer.class, String.class);
cacheStatistics = statisticsService.getCacheStatistics("cache");
}
示例15: configEhcache2Jsr107
import org.ehcache.config.CacheConfiguration; //导入依赖的package包/类
@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
}
}