Java Code Examples for org.ehcache.Cache#removeAll()
The following examples show how to use
org.ehcache.Cache#removeAll() .
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: SimpleEhcacheTest.java From ehcache3 with Apache License 2.0 | 5 votes |
@Test public void testSimpleRemoveAll() throws Exception { Cache<Number, CharSequence> testCache = cacheManager.createCache("testCache", newCacheConfigurationBuilder(Number.class, CharSequence.class, heap(10))); testCache.put(1, "one"); testCache.put(2, "two"); testCache.put(3, "three"); testCache.removeAll(new HashSet<Number>(Arrays.asList(1, 2))); assertThat(testCache.get(1), is(nullValue())); assertThat(testCache.get(2), is(nullValue())); assertThat(testCache.get(3), is(notNullValue())); }
Example 2
Source File: EhcacheBulkMethodsITest.java From ehcache3 with Apache License 2.0 | 5 votes |
@Test public void testRemoveAll_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); for (int i = 0; i < 3; i++) { myCache.put("key" + i, "value" + i); } Set<String> fewKeysSet = new HashSet<String>() { { add("key0"); add("key2"); } }; // the call to removeAll myCache.removeAll(fewKeysSet); for (int i = 0; i < 3; i++) { if (i == 0 || i == 2) { assertThat(myCache.get("key" + i), is(nullValue())); } else { assertThat(myCache.get("key" + i), is("value" + i)); } } }
Example 3
Source File: EhcacheBulkMethodsITest.java From ehcache3 with Apache License 2.0 | 4 votes |
@Test public void testRemoveAll_cache_writer_throws_exception() throws Exception { CacheConfigurationBuilder cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, heap(100)); CacheLoaderWriterProvider cacheLoaderWriterProvider = mock(CacheLoaderWriterProvider.class); CacheLoaderWriter cacheLoaderWriterThatThrows = mock(CacheLoaderWriter.class); doThrow(new Exception("Simulating an exception from the cache writer")).when(cacheLoaderWriterThatThrows).deleteAll(ArgumentMatchers.any(Iterable.class)); when(cacheLoaderWriterProvider.createCacheLoaderWriter(anyString(), ArgumentMatchers.any(CacheConfiguration.class))).thenReturn(cacheLoaderWriterThatThrows); CacheManagerBuilder<CacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder().using(cacheLoaderWriterProvider); CacheConfiguration<String, String> cacheConfiguration = cacheConfigurationBuilder.withLoaderWriter(cacheLoaderWriterThatThrows) .build(); CacheManager cacheManager = managerBuilder.withCache("myCache", cacheConfiguration).build(true); Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class); for (int i = 0; i < 3; i++) { myCache.put("key" + i, "value" + i); } doThrow(new RuntimeException("We should not have called .write() but .writeAll()")).when(cacheLoaderWriterThatThrows).write(ArgumentMatchers .any(), ArgumentMatchers.any()); Set<String> fewKeysSet = new HashSet<String>() { { add("key0"); add("key2"); } }; // the call to removeAll try { myCache.removeAll(fewKeysSet); fail(); } catch (BulkCacheWritingException bcwe) { assertThat(bcwe.getFailures().size(), is(2)); assertThat(bcwe.getSuccesses().size(), is(0)); } }
Example 4
Source File: EhcacheBulkMethodsITest.java From ehcache3 with Apache License 2.0 | 4 votes |
@Test public void testRemoveAll_with_store_that_throws() throws Exception { CacheConfigurationBuilder cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, heap(100)); CacheConfiguration<String, String> cacheConfiguration = cacheConfigurationBuilder .build(); CacheLoaderWriterProvider cacheLoaderWriterProvider = mock(CacheLoaderWriterProvider.class); CacheLoaderWriter cacheLoaderWriter = mock(CacheLoaderWriter.class); when(cacheLoaderWriterProvider.createCacheLoaderWriter(anyString(), ArgumentMatchers.any(CacheConfiguration.class))).thenReturn(cacheLoaderWriter); CacheManagerBuilder<CacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder().using(cacheLoaderWriterProvider).using(new CustomStoreProvider()); CacheManager cacheManager = managerBuilder.withCache("myCache", cacheConfiguration).build(true); Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class); for (int i = 0; i < 3; i++) { myCache.put("key" + i, "value" + i); } doThrow(new RuntimeException("We should not have called .write() but .writeAll()")).when(cacheLoaderWriter).write(ArgumentMatchers .any(), ArgumentMatchers.any()); Set<String> fewKeysSet = new HashSet<String>() { { add("key0"); add("key2"); } }; // the call to removeAll myCache.removeAll(fewKeysSet); for (int i = 0; i < 3; i++) { if (i == 0 || i == 2) { assertThat(myCache.get("key" + i), is(nullValue())); } else { assertThat(myCache.get("key" + i), is("value" + i)); } } Set set = new HashSet(){{add("key0"); add("key2");}}; verify(cacheLoaderWriter).deleteAll(set); }
Example 5
Source File: AbstractWriteBehindTestBase.java From ehcache3 with Apache License 2.0 | 4 votes |
@Test public void testBulkWrites() throws Exception { WriteBehindTestLoaderWriter<String, String> loaderWriter = new WriteBehindTestLoaderWriter<>(); CacheLoaderWriterProvider cacheLoaderWriterProvider = getMockedCacheLoaderWriterProvider(loaderWriter); try (CacheManager cacheManager = managerBuilder().using(cacheLoaderWriterProvider).build(true)) { Cache<String, String> testCache = cacheManager.createCache("testBulkWrites", CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, heap(100)) .withLoaderWriter(loaderWriter) .withService(newUnBatchedWriteBehindConfiguration().concurrencyLevel(3).queueSize(10).build()) .build()); CountDownLatch countDownLatch = new CountDownLatch(20); loaderWriter.setLatch(countDownLatch); for (int i = 0; i < 10; i++) testCache.put("test" + i, "test" + i); Map<String, String> entries = new HashMap<>(10); Set<String> keys = new HashSet<>(10); for (int i = 10; i < 20; i++) { entries.put("test" + i, "test" + i); keys.add("test" + i); } testCache.putAll(entries); countDownLatch.await(5, SECONDS); for (int i = 0; i < 20; i++) { assertThat("Key : " + i, loaderWriter.getData().get("test" + i), contains("test" + i)); } CountDownLatch countDownLatch1 = new CountDownLatch(10); loaderWriter.setLatch(countDownLatch1); testCache.removeAll(keys); countDownLatch1.await(5, SECONDS); assertThat(loaderWriter.getData().size(), is(20)); for (int i = 0; i < 10; i++) { assertThat("Key : " + i, loaderWriter.getData().get("test" + i), contains("test" + i)); } for (int i = 10; i < 20; i++) { assertThat("Key : " + i, loaderWriter.getData().get("test" + i), contains("test" + i, null)); } } }
Example 6
Source File: BasicClusteredCacheOpsTest.java From ehcache3 with Apache License 2.0 | 4 votes |
@Test public void basicClusteredBulk() throws Exception { final CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder = newCacheManagerBuilder() .with(cluster(CLUSTER.getConnectionURI().resolve("/bulk-cm")).autoCreate(c -> c)) .withCache("clustered-cache", newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.newResourcePoolsBuilder() .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 2, MemoryUnit.MB))) .withService(new ClusteredStoreConfiguration(Consistency.STRONG))); try (PersistentCacheManager cacheManager1 = clusteredCacheManagerBuilder.build(true)) { try (PersistentCacheManager cacheManager2 = clusteredCacheManagerBuilder.build(true)) { final Cache<Long, String> cache1 = cacheManager1.getCache("clustered-cache", Long.class, String.class); final Cache<Long, String> cache2 = cacheManager2.getCache("clustered-cache", Long.class, String.class); Map<Long, String> entriesMap = new HashMap<>(); entriesMap.put(1L, "one"); entriesMap.put(2L, "two"); entriesMap.put(3L, "three"); cache1.putAll(entriesMap); Set<Long> keySet = new HashSet<>(Arrays.asList(1L, 2L, 3L)); Map<Long, String> all = cache2.getAll(keySet); assertThat(all.get(1L), is("one")); assertThat(all.get(2L), is("two")); assertThat(all.get(3L), is("three")); Map<Long, String> entries1 = new HashMap<>(); assertThat(cache1, iterableWithSize(3)); cache1.forEach(e -> entries1.putIfAbsent(e.getKey(), e.getValue())); assertThat(entries1, hasEntry(1L, "one")); assertThat(entries1, hasEntry(2L, "two")); assertThat(entries1, hasEntry(3L, "three")); Map<Long, String> entries2 = new HashMap<>(); assertThat(cache2, iterableWithSize(3)); cache2.forEach(e -> entries2.putIfAbsent(e.getKey(), e.getValue())); assertThat(entries2, hasEntry(1L, "one")); assertThat(entries2, hasEntry(2L, "two")); assertThat(entries2, hasEntry(3L, "three")); cache2.removeAll(keySet); all = cache1.getAll(keySet); assertThat(all.get(1L), nullValue()); assertThat(all.get(2L), nullValue()); assertThat(all.get(3L), nullValue()); } } }
Example 7
Source File: BasicClusteredLoaderWriterTest.java From ehcache3 with Apache License 2.0 | 3 votes |
@Test public void testBulkOps() { TestCacheLoaderWriter loaderWriter = new TestCacheLoaderWriter(); CacheConfiguration<Long, String> cacheConfiguration = getCacheConfiguration(loaderWriter); try (CacheManager cacheManager = CacheManagerBuilder .newCacheManagerBuilder() .with(cluster(CLUSTER_URI).autoCreate(c -> c)) .withCache("cache-1", cacheConfiguration) .build(true)) { Cache<Long, String> cache = cacheManager.getCache("cache-1", Long.class, String.class); Map<Long, String> mappings = new HashMap<>(); for (int i = 1; i <= 5; i++) { mappings.put((long) i, "" + i); } cache.putAll(mappings); assertThat(loaderWriter.storeMap.keySet(), containsInAnyOrder(mappings.keySet().toArray())); cache.clear(); Map<Long, String> loadedData = cache.getAll(mappings.keySet()); assertThat(mappings.keySet(), containsInAnyOrder(loadedData.keySet().toArray())); cache.removeAll(mappings.keySet()); assertThat(loaderWriter.storeMap.isEmpty(), is(true)); } }