Java Code Examples for org.ehcache.CacheManager#createCache()
The following examples show how to use
org.ehcache.CacheManager#createCache() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IntegrationConfigurationTest.java From ehcache3 with Apache License 2.0 | 6 votes |
@Test public void testThreadPools() throws Exception { Configuration configuration = new XmlConfiguration(this.getClass().getResource("/configs/thread-pools.xml")); final CacheManager cacheManager = CacheManagerBuilder.newCacheManager(configuration); cacheManager.init(); try { Cache<String, String> cache = cacheManager.createCache("testThreadPools", newCacheConfigurationBuilder(String.class, String.class, heap(10)) .withService(new DefaultCacheLoaderWriterConfiguration(ThreadRememberingLoaderWriter.class)) .withService(newUnBatchedWriteBehindConfiguration().useThreadPool("small")) .build()); cache.put("foo", "bar"); ThreadRememberingLoaderWriter.USED.acquireUninterruptibly(); assertThat(ThreadRememberingLoaderWriter.LAST_SEEN_THREAD.getName(), containsString("[small]")); } finally { cacheManager.close(); } }
Example 2
Source File: OnHeapStoreByValueTest.java From ehcache3 with Apache License 2.0 | 6 votes |
@Test public void testStoreByValue() { CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build(false); cacheManager.init(); DefaultCopierConfiguration<String> copierConfiguration = new DefaultCopierConfiguration<>( SerializingCopier.<String>asCopierClass(), DefaultCopierConfiguration.Type.VALUE); final Cache<Long, String> cache1 = cacheManager.createCache("cache1", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, heap(1)) .build()); performAssertions(cache1, true); final Cache<Long, String> cache2 = cacheManager.createCache("cache2", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, heap(1)) .withService(copierConfiguration) .build()); performAssertions(cache2, false); final Cache<Long, String> cache3 = cacheManager.createCache("cache3", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, heap(1)) .build()); performAssertions(cache3, true); cacheManager.close(); }
Example 3
Source File: WriteBehindProviderFactoryTest.java From ehcache3 with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") @Test public void testAddingWriteBehindConfigurationAtCacheLevel() { CacheManagerBuilder<CacheManager> cacheManagerBuilder = CacheManagerBuilder.newCacheManagerBuilder(); WriteBehindConfiguration<?> writeBehindConfiguration = WriteBehindConfigurationBuilder.newBatchedWriteBehindConfiguration(Long.MAX_VALUE, SECONDS, 1) .concurrencyLevel(3) .queueSize(10) .build(); Class<CacheLoaderWriter<?, ?>> klazz = (Class<CacheLoaderWriter<?, ?>>) (Class) (SampleLoaderWriter.class); CacheManager cacheManager = cacheManagerBuilder.build(true); final Cache<Long, String> cache = cacheManager.createCache("cache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, heap(100)) .withService(writeBehindConfiguration) .withService(new DefaultCacheLoaderWriterConfiguration(klazz)) .build()); Collection<ServiceConfiguration<?, ?>> serviceConfiguration = cache.getRuntimeConfiguration() .getServiceConfigurations(); assertThat(serviceConfiguration, IsCollectionContaining.<ServiceConfiguration<?, ?>>hasItem(instanceOf(WriteBehindConfiguration.class))); cacheManager.close(); }
Example 4
Source File: IntegrationConfigurationTest.java From ehcache3 with Apache License 2.0 | 6 votes |
@Test public void testThreadPoolsUsingDefaultPool() throws Exception { Configuration configuration = new XmlConfiguration(this.getClass().getResource("/configs/thread-pools.xml")); final CacheManager cacheManager = CacheManagerBuilder.newCacheManager(configuration); cacheManager.init(); try { Cache<String, String> cache = cacheManager.createCache("testThreadPools", newCacheConfigurationBuilder(String.class, String.class, heap(10)) .withService(new DefaultCacheLoaderWriterConfiguration(ThreadRememberingLoaderWriter.class)) .withService(newUnBatchedWriteBehindConfiguration()) .build()); cache.put("foo", "bar"); ThreadRememberingLoaderWriter.USED.acquireUninterruptibly(); assertThat(ThreadRememberingLoaderWriter.LAST_SEEN_THREAD.getName(), containsString("[big]")); } finally { cacheManager.close(); } }
Example 5
Source File: IntegrationConfigurationTest.java From ehcache3 with Apache License 2.0 | 6 votes |
@Test public void testSerializers() throws Exception { Configuration configuration = new XmlConfiguration(this.getClass().getResource("/configs/default-serializer.xml")); final CacheManager cacheManager = CacheManagerBuilder.newCacheManager(configuration); cacheManager.init(); Cache<Long, Double> bar = cacheManager.getCache("bar", Long.class, Double.class); bar.put(1L, 1.0); assertThat(bar.get(1L), equalTo(1.0)); Cache<String, String> baz = cacheManager.getCache("baz", String.class, String.class); baz.put("1", "one"); assertThat(baz.get("1"), equalTo("one")); Cache<String, Object> bam = cacheManager.createCache("bam", newCacheConfigurationBuilder(String.class, Object.class, heap(10)).build()); bam.put("1", "one"); assertThat(bam.get("1"), equalTo((Object)"one")); cacheManager.close(); }
Example 6
Source File: OverSizeMappingTest.java From ehcache3 with Apache License 2.0 | 6 votes |
@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()); }
Example 7
Source File: ThreadPoolsTest.java From ehcache3 with Apache License 2.0 | 6 votes |
@Test public void testDefaultThreadPoolFailsWithExplicitConfig() throws Exception { PooledExecutionServiceConfiguration executionServiceConfiguration = new PooledExecutionServiceConfiguration(); executionServiceConfiguration.addDefaultPool("dflt", 2, 4); CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .using(new CacheEventDispatcherFactoryConfiguration("foo")) .using(executionServiceConfiguration) .build(true); try { cacheManager.createCache("testCache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, heap(10)) .build()); fail("expected IllegalStateException"); } catch (IllegalStateException ise) { // expected } cacheManager.close(); }
Example 8
Source File: ClusteredLoaderWriterTest.java From ehcache3 with Apache License 2.0 | 6 votes |
@Test public void testBasicOps() { client1 = cacheManager.createCache("basicops" + cacheConsistency.name(), configuration); assertThat(sor.isEmpty(), is(true)); Set<Long> keys = new HashSet<>(); ThreadLocalRandom.current().longs(10).forEach(x -> { keys.add(x); client1.put(x, Long.toString(x)); }); assertThat(sor.size(), is(10)); CacheManager anotherCacheManager = newCacheManager(); Cache<Long, String> client2 = anotherCacheManager.createCache("basicops" + cacheConsistency.name(), getCacheConfig()); Map<Long, String> all = client2.getAll(keys); assertThat(all.keySet(), containsInAnyOrder(keys.toArray())); keys.stream().limit(3).forEach(client2::remove); assertThat(sor.size(), is(7)); }
Example 9
Source File: TestEhcache.java From elastic-rabbitmq with MIT License | 6 votes |
@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(); }
Example 10
Source File: OverSizeMappingTest.java From ehcache3 with Apache License 2.0 | 5 votes |
@Test public void testOverSizedObjectGetsReturnedFromLowerTier() { CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .withDefaultSizeOfMaxObjectSize(500, MemoryUnit.B) .withDefaultSizeOfMaxObjectGraph(1000) .build(true); CacheConfiguration<String, String> objectSize = CacheConfigurationBuilder .newCacheConfigurationBuilder(String.class, String.class, newResourcePoolsBuilder() .heap(100, MemoryUnit.KB).offheap(10, MemoryUnit.MB).build()) .build(); Cache<String, String> objectSizeCache = cacheManager.createCache("objectSize", objectSize); objectSizeCache.put("key1", getOverSizedObject()); assertThat(objectSizeCache.get("key1"), equalTo(getOverSizedObject())); CacheConfiguration<String, ObjectSizeGreaterThanN> objectGraphSize = CacheConfigurationBuilder .newCacheConfigurationBuilder(String.class, ObjectSizeGreaterThanN.class, newResourcePoolsBuilder() .heap(100, MemoryUnit.KB).offheap(10, MemoryUnit.MB).build()) .build(); Cache<String, ObjectSizeGreaterThanN> objectGraphSizeCache = cacheManager.createCache("objectGraphSize", objectGraphSize); objectGraphSizeCache.put("key1", getObjectSizeGreaterThanN(1002)); assertThat(objectGraphSizeCache.get("key1"), equalTo(getObjectSizeGreaterThanN(1002))); }
Example 11
Source File: ThreadPoolsTest.java From ehcache3 with Apache License 2.0 | 5 votes |
@Test public void testDefaultThreadPoolSucceedsWithoutConfig() throws Exception { PooledExecutionServiceConfiguration executionServiceConfiguration = new PooledExecutionServiceConfiguration(); executionServiceConfiguration.addDefaultPool("dflt", 2, 4); CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .using(executionServiceConfiguration) .build(true); cacheManager.createCache("testCache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, heap(10)) .build()); cacheManager.close(); }
Example 12
Source File: ThreadPoolsTest.java From ehcache3 with Apache License 2.0 | 5 votes |
@Test public void testNoDefaultThreadPoolSucceedsWithExplicitConfig() throws Exception { PooledExecutionServiceConfiguration executionServiceConfiguration = new PooledExecutionServiceConfiguration(); executionServiceConfiguration.addPool("foo", 2, 4); CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .using(new CacheEventDispatcherFactoryConfiguration("foo")) .using(executionServiceConfiguration) .build(true); cacheManager.createCache("testCache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, heap(10)) .build()); cacheManager.close(); }
Example 13
Source File: ClusteredLoaderWriterTest.java From ehcache3 with Apache License 2.0 | 5 votes |
@Test public void testCASOps() { client1 = cacheManager.createCache("casops" + cacheConsistency.name(), configuration); assertThat(sor.isEmpty(), is(true)); Set<Long> keys = new HashSet<>(); ThreadLocalRandom.current().longs(10).forEach(x -> { keys.add(x); client1.put(x, Long.toString(x)); }); assertThat(sor.size(), is(10)); CacheManager anotherCacheManager = newCacheManager(); Cache<Long, String> client2 = anotherCacheManager.createCache("casops" + cacheConsistency.name(), getCacheConfig()); keys.forEach(x -> assertThat(client2.putIfAbsent(x, "Again" + x), is(Long.toString(x)))); assertThat(sor.size(), is(10)); keys.stream().limit(5).forEach(x -> assertThat(client2.replace(x , "Replaced" + x), is(Long.toString(x)))); assertThat(sor.size(), is(10)); keys.forEach(x -> client1.remove(x, Long.toString(x))); assertThat(sor.size(), is(5)); AtomicInteger success = new AtomicInteger(0); keys.forEach(x -> { if (client2.replace(x, "Replaced" + x, "Again")) { success.incrementAndGet(); } }); assertThat(success.get(), is(5)); }
Example 14
Source File: Ehcache3.java From caffeine with Apache License 2.0 | 5 votes |
@SuppressWarnings({"unchecked", "PMD.CloseResource"}) public Ehcache3(int maximumSize) { CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build(true); cache = (Cache<K, V>) cacheManager.createCache("benchmark", CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class, ResourcePoolsBuilder.newResourcePoolsBuilder() .heap(maximumSize, EntryUnit.ENTRIES)) .build()); }
Example 15
Source File: EventsFailureBehaviorTest.java From ehcache3 with Apache License 2.0 | 5 votes |
private static Cache<Long, byte[]> createCache(CacheManager cacheManager, CacheEventListener<?, ?> cacheEventListener, ExpiryPolicy<? super Long, ? super byte[]> expiryPolicy) { return cacheManager.createCache("cache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, byte[].class, ResourcePoolsBuilder.newResourcePoolsBuilder() .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 4, MemoryUnit.MB))) .withResilienceStrategy(new ThrowingResiliencyStrategy<>()) .withService(CacheEventListenerConfigurationBuilder .newEventListenerConfiguration(cacheEventListener, EnumSet.allOf(EventType.class)) .unordered().asynchronous()) .withExpiry(expiryPolicy) .build()); }
Example 16
Source File: EhcacheManagerTest.java From ehcache3 with Apache License 2.0 | 5 votes |
@Test public void testCachesAddedAtRuntimeGetReInited() { Store.Provider storeProvider = mock(Store.Provider.class); when(storeProvider.rank(any(Set.class), any(Collection.class))).thenReturn(1); Store store = mock(Store.class); CacheEventDispatcherFactory cacheEventNotificationListenerServiceProvider = mock(CacheEventDispatcherFactory.class); when(storeProvider.createStore(any(Store.Configuration.class), ArgumentMatchers.<ServiceConfiguration>any())).thenReturn(store); when(store.getConfigurationChangeListeners()).thenReturn(new ArrayList<>()); when(cacheEventNotificationListenerServiceProvider.createCacheEventDispatcher(store)).thenReturn(mock(CacheEventDispatcher.class)); CacheConfiguration<Long, String> cache1Configuration = new TestCacheConfig<>(Long.class, String.class); Map<String, CacheConfiguration<?, ?>> caches = newCacheMap(); caches.put("cache1", cache1Configuration); DefaultConfiguration config = new DefaultConfiguration(caches, null); CacheManager cacheManager = new EhcacheManager(config, Arrays.asList( storeProvider, mock(CacheLoaderWriterProvider.class), mock(WriteBehindProvider.class), cacheEventNotificationListenerServiceProvider, mock(CacheEventListenerProvider.class), mock(LocalPersistenceService.class), mock(ResilienceStrategyProvider.class) )); cacheManager.init(); CacheConfiguration<Long, String> cache2Configuration = new TestCacheConfig<>(Long.class, String.class, ResourcePoolsHelper.createResourcePools(100L)); cacheManager.createCache("cache2", cache2Configuration); cacheManager.removeCache("cache1"); cacheManager.close(); cacheManager.init(); try { assertThat(cacheManager.getCache("cache1", Long.class, String.class), nullValue()); assertThat(cacheManager.getCache("cache2", Long.class, String.class), notNullValue()); } finally { cacheManager.close(); } }
Example 17
Source File: DefaultCacheLoaderWriterProviderTest.java From ehcache3 with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") @Test public void testAddingCacheLoaderWriterConfigurationAtCacheLevel() { CacheManagerBuilder<CacheManager> cacheManagerBuilder = CacheManagerBuilder.newCacheManagerBuilder(); Class<CacheLoaderWriter<?, ?>> klazz = (Class<CacheLoaderWriter<?, ?>>) (Class) (MyLoader.class); CacheManager cacheManager = cacheManagerBuilder.build(true); final Cache<Long, String> cache = cacheManager.createCache("cache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, heap(100)) .withService(new DefaultCacheLoaderWriterConfiguration(klazz)) .build()); Collection<ServiceConfiguration<?, ?>> serviceConfiguration = cache.getRuntimeConfiguration() .getServiceConfigurations(); assertThat(serviceConfiguration, IsCollectionContaining.<ServiceConfiguration<?, ?>>hasItem(instanceOf(DefaultCacheLoaderWriterConfiguration.class))); cacheManager.close(); }
Example 18
Source File: DefaultCacheEventListenerProviderTest.java From ehcache3 with Apache License 2.0 | 5 votes |
@Test public void testAddingCacheEventListenerConfigurationAtCacheLevel() { CacheManagerBuilder<CacheManager> cacheManagerBuilder = CacheManagerBuilder.newCacheManagerBuilder(); CacheEventListenerConfiguration<?> cacheEventListenerConfiguration = CacheEventListenerConfigurationBuilder .newEventListenerConfiguration(ListenerObject.class, EventType.CREATED).unordered().asynchronous().build(); CacheManager cacheManager = cacheManagerBuilder.build(true); final Cache<Long, String> cache = cacheManager.createCache("cache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, heap(100)) .withService(cacheEventListenerConfiguration) .build()); Collection<ServiceConfiguration<?, ?>> serviceConfiguration = cache.getRuntimeConfiguration() .getServiceConfigurations(); assertThat(serviceConfiguration, IsCollectionContaining.<ServiceConfiguration<?, ?>>hasItem(instanceOf(DefaultCacheEventListenerConfiguration.class))); cacheManager.close(); }
Example 19
Source File: AbstractWriteBehindTestBase.java From ehcache3 with Apache License 2.0 | 5 votes |
@Test public void testUnBatchedOverwrittenKeyReturnsNewValue() throws Exception { final Semaphore semaphore = new Semaphore(0); @SuppressWarnings("unchecked") CacheLoaderWriter<String, String> loaderWriter = mock(CacheLoaderWriter.class); when(loaderWriter.load("key")).thenReturn("value"); doAnswer(invocation -> { semaphore.acquire(); return null; }).when(loaderWriter).delete("key"); CacheLoaderWriterProvider cacheLoaderWriterProvider = getMockedCacheLoaderWriterProvider(loaderWriter); CacheManager cacheManager = managerBuilder().using(cacheLoaderWriterProvider).build(true); try { Cache<String, String> testCache = cacheManager.createCache("testUnBatchedOverwrittenKeyReturnsNewValue", configurationBuilder() .withLoaderWriter(loaderWriter) .withService(newUnBatchedWriteBehindConfiguration().build()) .build()); assertThat(testCache.get("key"), is("value")); testCache.remove("key"); assertThat(testCache.get("key"), nullValue()); } finally { semaphore.release(); cacheManager.close(); } }
Example 20
Source File: EhcacheManagerTest.java From ehcache3 with Apache License 2.0 | 4 votes |
@Test public void testChangesToManagerAreReflectedInConfig() { Store.Provider storeProvider = mock(Store.Provider.class); when(storeProvider.rank(any(Set.class), any(Collection.class))).thenReturn(1); Store store = mock(Store.class); CacheEventDispatcherFactory cacheEventNotificationListenerServiceProvider = mock(CacheEventDispatcherFactory.class); when(storeProvider.createStore(any(Store.Configuration.class), ArgumentMatchers.<ServiceConfiguration>any())).thenReturn(store); when(store.getConfigurationChangeListeners()).thenReturn(new ArrayList<>()); when(cacheEventNotificationListenerServiceProvider.createCacheEventDispatcher(store)).thenReturn(mock(CacheEventDispatcher.class)); CacheConfiguration<Long, String> cache1Configuration = new TestCacheConfig<>(Long.class, String.class); Map<String, CacheConfiguration<?, ?>> caches = newCacheMap(); caches.put("cache1", cache1Configuration); DefaultConfiguration config = new DefaultConfiguration(caches, null); CacheManager cacheManager = new EhcacheManager(config, Arrays.asList(storeProvider, mock(CacheLoaderWriterProvider.class), mock(WriteBehindProvider.class), cacheEventNotificationListenerServiceProvider, mock(CacheEventListenerProvider.class), mock(LocalPersistenceService.class), mock(ResilienceStrategyProvider.class) )); cacheManager.init(); try { final CacheConfiguration<Long, String> cache2Configuration = new TestCacheConfig<>(Long.class, String.class, ResourcePoolsHelper .createResourcePools(100L)); final Cache<Long, String> cache = cacheManager.createCache("cache2", cache2Configuration); final CacheConfiguration<?, ?> cacheConfiguration = cacheManager.getRuntimeConfiguration() .getCacheConfigurations() .get("cache2"); assertThat(cacheConfiguration, notNullValue()); final CacheConfiguration<?, ?> runtimeConfiguration = cache.getRuntimeConfiguration(); assertThat(cacheConfiguration == runtimeConfiguration, is(true)); assertThat(cacheManager.getRuntimeConfiguration().getCacheConfigurations().get("cache1") == cacheManager.getCache("cache1", Long.class, String.class).getRuntimeConfiguration(), is(true)); cacheManager.removeCache("cache1"); assertThat(cacheManager.getRuntimeConfiguration().getCacheConfigurations().containsKey("cache1"), is(false)); } finally { cacheManager.close(); } }