Java Code Examples for org.ehcache.Cache#remove()
The following examples show how to use
org.ehcache.Cache#remove() .
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: TranslateEhcacheManager.java From sagacity-sqltoy with Apache License 2.0 | 6 votes |
@Override public void clear(String cacheName, String cacheKey) { synchronized (cacheName) { if (cacheManager == null) { return; } Cache<String, HashMap> cache = cacheManager.getCache(cacheName, String.class, HashMap.class); // 缓存没有配置,自动创建缓存不建议使用 if (cache != null) { if (StringUtil.isBlank(cacheKey)) { cache.clear(); } else { cache.remove(cacheKey); } } } }
Example 2
Source File: AbstractWriteBehindTestBase.java From ehcache3 with Apache License 2.0 | 6 votes |
@Test public void testBatchedDeletedKeyReturnsNull() throws Exception { @SuppressWarnings("unchecked") CacheLoaderWriter<String, String> loaderWriter = mock(CacheLoaderWriter.class); when(loaderWriter.load("key")).thenReturn("value"); CacheLoaderWriterProvider cacheLoaderWriterProvider = getMockedCacheLoaderWriterProvider(loaderWriter); try (CacheManager cacheManager = managerBuilder().using(cacheLoaderWriterProvider).build(true)) { Cache<String, String> testCache = cacheManager.createCache("testBatchedDeletedKeyReturnsNull", configurationBuilder() .withLoaderWriter(loaderWriter) .withService(newBatchedWriteBehindConfiguration(Long.MAX_VALUE, SECONDS, 2).build()) .build()); assertThat(testCache.get("key"), is("value")); testCache.remove("key"); assertThat(testCache.get("key"), nullValue()); } }
Example 3
Source File: AbstractWriteBehindTestBase.java From ehcache3 with Apache License 2.0 | 6 votes |
@Test public void testWrites() 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("testWrites", CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, heap(10)) .withLoaderWriter(loaderWriter) .withService(newUnBatchedWriteBehindConfiguration().concurrencyLevel(3).queueSize(10).build()) .build()); CountDownLatch countDownLatch = new CountDownLatch(4); loaderWriter.setLatch(countDownLatch); testCache.put("test1", "test1"); testCache.put("test2", "test2"); testCache.put("test3", "test3"); testCache.remove("test2"); countDownLatch.await(2, SECONDS); assertThat(loaderWriter.getData().get("test1"), contains("test1")); assertThat(loaderWriter.getData().get("test2"), contains("test2", null)); assertThat(loaderWriter.getData().get("test3"), contains("test3")); } }
Example 4
Source File: GettingStarted.java From ehcache3 with Apache License 2.0 | 6 votes |
@Test public void testCacheEventListener() { // tag::cacheEventListener[] CacheEventListenerConfigurationBuilder cacheEventListenerConfiguration = CacheEventListenerConfigurationBuilder .newEventListenerConfiguration(new ListenerObject(), EventType.CREATED, EventType.UPDATED) // <1> .unordered().asynchronous(); // <2> final CacheManager manager = CacheManagerBuilder.newCacheManagerBuilder() .withCache("foo", CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, ResourcePoolsBuilder.heap(10)) .withService(cacheEventListenerConfiguration) // <3> ).build(true); final Cache<String, String> cache = manager.getCache("foo", String.class, String.class); cache.put("Hello", "World"); // <4> cache.put("Hello", "Everyone"); // <5> cache.remove("Hello"); // <6> // end::cacheEventListener[] manager.close(); }
Example 5
Source File: IntegrationConfigurationTest.java From ehcache3 with Apache License 2.0 | 6 votes |
@Test public void testCacheEventListener() throws Exception { Configuration configuration = new XmlConfiguration(this.getClass().getResource("/configs/ehcache-cacheEventListener.xml")); assertThat(configuration.getCacheConfigurations().containsKey("bar"), is(true)); final CacheManager cacheManager = CacheManagerBuilder.newCacheManager(configuration); cacheManager.init(); final Cache<Number, String> cache = cacheManager.getCache("bar", Number.class, String.class); cache.put(10, "dog"); assertThat(TestCacheEventListener.FIRED_EVENT.getType(), is(EventType.CREATED)); cache.put(10, "cat"); assertThat(TestCacheEventListener.FIRED_EVENT.getType(), is(EventType.UPDATED)); cache.remove(10); assertThat(TestCacheEventListener.FIRED_EVENT.getType(), is(EventType.REMOVED)); cache.put(10, "dog"); resetValues(); assertThat(configuration.getCacheConfigurations().containsKey("template1"), is(true)); final Cache<Number, String> templateCache = cacheManager.getCache("template1", Number.class, String.class); templateCache.put(10,"cat"); assertThat(TestCacheEventListener.FIRED_EVENT, nullValue()); templateCache.put(10, "dog"); assertThat(TestCacheEventListener.FIRED_EVENT.getType(), is(EventType.UPDATED)); }
Example 6
Source File: XACacheTest.java From ehcache3 with Apache License 2.0 | 6 votes |
private void putIfAbsentAssertions(BitronixTransactionManager transactionManager, Cache<Long, String> txCache1) throws Exception { transactionManager.begin(); { assertThat(txCache1.putIfAbsent(1L, "one"), is(nullValue())); assertThat(txCache1.putIfAbsent(1L, "un"), equalTo("one")); } transactionManager.commit(); assertMapping(transactionManager, txCache1, 1L, "one"); transactionManager.begin(); { assertThat(txCache1.putIfAbsent(1L, "eins"), equalTo("one")); txCache1.remove(1L); assertThat(txCache1.putIfAbsent(1L, "een"), is(nullValue())); } transactionManager.commit(); assertMapping(transactionManager, txCache1, 1L, "een"); }
Example 7
Source File: XACacheTest.java From ehcache3 with Apache License 2.0 | 5 votes |
@Test public void testRecoveryWithInflightTx() throws Exception { CacheConfigurationBuilder<Long, String> cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, newResourcePoolsBuilder() .heap(10, EntryUnit.ENTRIES) .offheap(10, MemoryUnit.MB)); cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .withCache("txCache1", cacheConfigurationBuilder.withService(new XAStoreConfiguration("txCache1")).build()) .withCache("txCache2", cacheConfigurationBuilder.withService(new XAStoreConfiguration("txCache2")).build()) .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class)) .build(true); Cache<Long, String> txCache1 = cacheManager.getCache("txCache1", Long.class, String.class); Cache<Long, String> txCache2 = cacheManager.getCache("txCache2", Long.class, String.class); transactionManager.begin(); { txCache1.put(1L, "one"); txCache2.put(1L, "un"); } transactionManager.commit(); transactionManager.begin(); { txCache1.remove(1L); txCache2.remove(1L); } transactionManager.getCurrentTransaction().addTransactionStatusChangeListener((oldStatus, newStatus) -> { if (newStatus == Status.STATUS_PREPARED) { Recoverer recoverer = TransactionManagerServices.getRecoverer(); recoverer.run(); assertThat(recoverer.getCommittedCount(), is(0)); assertThat(recoverer.getRolledbackCount(), is(0)); } }); transactionManager.commit(); }
Example 8
Source File: SimpleEhcacheTest.java From ehcache3 with Apache License 2.0 | 5 votes |
@Test public void testSimpleRemove() 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.remove(1); assertThat(testCache.get(1), is(nullValue())); assertThat(testCache.get(2), is(notNullValue())); }
Example 9
Source File: BasicClusteredLoaderWriterTest.java From ehcache3 with Apache License 2.0 | 5 votes |
@Test public void testLoaderWriterMultipleClients() { TestCacheLoaderWriter loaderWriter = new TestCacheLoaderWriter(); CacheConfiguration<Long, String> cacheConfiguration = getCacheConfiguration(loaderWriter); try (CacheManager cacheManager1 = CacheManagerBuilder .newCacheManagerBuilder() .with(cluster(CLUSTER_URI).autoCreate(c -> c)) .withCache("cache-1", cacheConfiguration) .build(true)) { try (CacheManager cacheManager2 = CacheManagerBuilder .newCacheManagerBuilder() .with(cluster(CLUSTER_URI).autoCreate(c -> c)) .withCache("cache-1", cacheConfiguration) .build(true)) { Cache<Long, String> client1 = cacheManager1.getCache("cache-1", Long.class, String.class); Cache<Long, String> client2 = cacheManager2.getCache("cache-1", Long.class, String.class); client1.put(1L, "1"); client2.put(1L, "2"); assertThat(client1.get(1L), is("2")); assertThat(loaderWriter.storeMap.get(1L), is("2")); client1.remove(1L); assertThat(client2.get(1L), nullValue()); assertThat(loaderWriter.storeMap.get(1L), nullValue()); } } }
Example 10
Source File: SyncService.java From moon-api-gateway with MIT License | 5 votes |
/** * Clears the application information from the cache. * * @param appInfo Application information object. The apikey and appId variables are required. * @return Whether the operation was successful. Not yet applied. */ private boolean deleteApp(AppInfo appInfo) { Cache<String, Integer> appDistinction = apiExposeSpec.getAppDistinctionCache(); appDistinction.remove(appInfo.getApiKey()); Cache<Integer, AppInfo> appInfoCaches = apiExposeSpec.getAppInfoCache(); appInfoCaches.remove(appInfo.getAppId()); return true; }
Example 11
Source File: GettingStarted.java From ehcache3 with Apache License 2.0 | 5 votes |
@Test public void registerListenerAtRuntime() { CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .withCache("cache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10L))) .build(true); Cache<Long, String> cache = cacheManager.getCache("cache", Long.class, String.class); // tag::registerListenerAtRuntime[] ListenerObject listener = new ListenerObject(); // <1> cache.getRuntimeConfiguration().registerCacheEventListener(listener, EventOrdering.ORDERED, EventFiring.ASYNCHRONOUS, EnumSet.of(EventType.CREATED, EventType.REMOVED)); // <2> cache.put(1L, "one"); cache.put(2L, "two"); cache.remove(1L); cache.remove(2L); cache.getRuntimeConfiguration().deregisterCacheEventListener(listener); // <3> cache.put(1L, "one again"); cache.remove(1L); // end::registerListenerAtRuntime[] cacheManager.close(); }
Example 12
Source File: AbstractWriteBehindTestBase.java From ehcache3 with Apache License 2.0 | 5 votes |
@Test public void testWriteOrdering() 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("testWriteOrdering", configurationBuilder() .withLoaderWriter(loaderWriter) .withService(newBatchedWriteBehindConfiguration(Long.MAX_VALUE, SECONDS, 8).build()) .build()); CountDownLatch countDownLatch = new CountDownLatch(8); loaderWriter.setLatch(countDownLatch); testCache.remove("key"); testCache.put("key", "value1"); testCache.remove("key"); testCache.put("key", "value2"); testCache.remove("key"); testCache.put("key", "value3"); testCache.remove("key"); testCache.put("key", "value4"); countDownLatch.await(4, SECONDS); assertThat(loaderWriter.getData() .get("key"), contains(null, "value1", null, "value2", null, "value3", null, "value4")); } }
Example 13
Source File: BasicClusteredCacheOpsTest.java From ehcache3 with Apache License 2.0 | 5 votes |
@Test public void basicCacheCRUD() throws Exception { final CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder = CacheManagerBuilder.newCacheManagerBuilder() .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.getConnectionURI().resolve("/crud-cm")) .autoCreate(server -> server.defaultServerResource("primary-server-resource"))); final 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", 1, MemoryUnit.MB))).build(); Cache<Long, String> cache = cacheManager.createCache("clustered-cache", config); cache.put(1L, "The one"); assertThat(cache.containsKey(2L), is(false)); cache.put(2L, "The two"); assertThat(cache.containsKey(2L), is(true)); cache.put(1L, "Another one"); cache.put(3L, "The three"); assertThat(cache.get(1L), equalTo("Another one")); assertThat(cache.get(2L), equalTo("The two")); assertThat(cache.get(3L), equalTo("The three")); cache.remove(1L); assertThat(cache.get(1L), is(nullValue())); cache.clear(); assertThat(cache.get(1L), is(nullValue())); assertThat(cache.get(2L), is(nullValue())); assertThat(cache.get(3L), is(nullValue())); } finally { cacheManager.close(); } }
Example 14
Source File: AbstractWriteBehindTestBase.java From ehcache3 with Apache License 2.0 | 5 votes |
@Test public void testUnBatchedDeletedKeyReturnsNull() throws Exception { 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("testUnBatchedDeletedKeyReturnsNull", 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 15
Source File: XACacheTest.java From ehcache3 with Apache License 2.0 | 4 votes |
@Test public void testEndToEnd() throws Exception { CacheConfigurationBuilder<Long, String> cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, newResourcePoolsBuilder() .heap(10, EntryUnit.ENTRIES) .offheap(10, MemoryUnit.MB)); cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class)) .withCache("txCache1", cacheConfigurationBuilder.withService(new XAStoreConfiguration("txCache1")).build()) .withCache("txCache2", cacheConfigurationBuilder.withService(new XAStoreConfiguration("txCache2")).build()) .withCache("nonTxCache", cacheConfigurationBuilder.build()) .build(true); Cache<Long, String> txCache1 = cacheManager.getCache("txCache1", Long.class, String.class); Cache<Long, String> txCache2 = cacheManager.getCache("txCache2", Long.class, String.class); Cache<Long, String> nonTxCache = cacheManager.getCache("nonTxCache", Long.class, String.class); nonTxCache.put(1L, "eins"); assertThat(nonTxCache.get(1L), equalTo("eins")); try { txCache1.put(1L, "one"); fail("expected XACacheException"); } catch (XACacheException e) { // expected } transactionManager.begin(); { txCache1.put(1L, "one"); } transactionManager.commit(); transactionManager.begin(); { txCache1.get(1L); txCache2.get(1L); } transactionManager.commit(); transactionManager.begin(); { String s = txCache1.get(1L); assertThat(s, equalTo("one")); txCache1.remove(1L); Transaction suspended = transactionManager.suspend(); transactionManager.begin(); { txCache2.put(1L, "uno"); String s2 = txCache1.get(1L); assertThat(s2, equalTo("one")); } transactionManager.commit(); transactionManager.resume(suspended); String s1 = txCache2.get(1L); assertThat(s1, equalTo("uno")); } transactionManager.commit(); }
Example 16
Source File: ClusteredEventsTest.java From ehcache3 with Apache License 2.0 | 4 votes |
@Test public void testNonExpiringEventSequence() throws TimeoutException { CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder = newCacheManagerBuilder() .with(cluster(CLUSTER_URI).autoCreate(s -> s.defaultServerResource("primary-server-resource"))) .withCache(runningTest.getMethodName(), newCacheConfigurationBuilder(Long.class, String.class, newResourcePoolsBuilder().with(clusteredDedicated(16, MemoryUnit.MB)))); try (PersistentCacheManager driver = clusteredCacheManagerBuilder.build(true)) { Cache<Long, String> driverCache = driver.getCache(runningTest.getMethodName(), Long.class, String.class); try (PersistentCacheManager observer = clusteredCacheManagerBuilder.build(true)) { Cache<Long, String> observerCache = observer.getCache(runningTest.getMethodName(), Long.class, String.class); List<CacheEvent<? extends Long, ? extends String>> driverEvents = new ArrayList<>(); driverCache.getRuntimeConfiguration().registerCacheEventListener(driverEvents::add, EventOrdering.ORDERED, EventFiring.ASYNCHRONOUS, allOf(EventType.class)); List<CacheEvent<? extends Long, ? extends String>> observerEvents = new ArrayList<>(); observerCache.getRuntimeConfiguration().registerCacheEventListener(observerEvents::add, EventOrdering.ORDERED, EventFiring.ASYNCHRONOUS, allOf(EventType.class)); driverCache.put(1L, "foo"); driverCache.put(1L, "bar"); driverCache.remove(1L); driverCache.putIfAbsent(1L, "baz"); driverCache.replace(1L, "bat"); driverCache.replace(1L, "bat", "bag"); driverCache.remove(1L, "bag"); @SuppressWarnings("unchecked") Matcher<Iterable<? extends CacheEvent<? extends Long, ? extends String>>> expectedSequence = contains( created(1L, "foo"), updated(1L, "foo", "bar"), removed(1L, "bar"), created(1L, "baz"), updated(1L, "baz", "bat"), updated(1L, "bat", "bag"), removed(1L, "bag")); within(Duration.ofSeconds(10)).runsCleanly(() -> { assertThat(driverEvents, expectedSequence); assertThat(observerEvents, expectedSequence); }); } } }
Example 17
Source File: ClusteredEventsTest.java From ehcache3 with Apache License 2.0 | 4 votes |
@Test public void testExpiringEventSequence() throws TimeoutException { TestTimeSource timeSource = new TestTimeSource(); CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder = newCacheManagerBuilder() .using(new TimeSourceConfiguration(timeSource)) .with(cluster(CLUSTER_URI).autoCreate(s -> s.defaultServerResource("primary-server-resource"))) .withCache(runningTest.getMethodName(), newCacheConfigurationBuilder(Long.class, String.class, newResourcePoolsBuilder().with(clusteredDedicated(16, MemoryUnit.MB))) .withExpiry(timeToLiveExpiration(Duration.ofMillis(1000)))); try (PersistentCacheManager driver = clusteredCacheManagerBuilder.build(true)) { Cache<Long, String> driverCache = driver.getCache(runningTest.getMethodName(), Long.class, String.class); try (PersistentCacheManager observer = clusteredCacheManagerBuilder.build(true)) { Cache<Long, String> observerCache = observer.getCache(runningTest.getMethodName(), Long.class, String.class); List<CacheEvent<? extends Long, ? extends String>> driverEvents = new ArrayList<>(); driverCache.getRuntimeConfiguration().registerCacheEventListener(driverEvents::add, EventOrdering.ORDERED, EventFiring.ASYNCHRONOUS, allOf(EventType.class)); List<CacheEvent<? extends Long, ? extends String>> observerEvents = new ArrayList<>(); observerCache.getRuntimeConfiguration().registerCacheEventListener(observerEvents::add, EventOrdering.ORDERED, EventFiring.ASYNCHRONOUS, allOf(EventType.class)); driverCache.put(1L, "foo"); timeSource.advanceTime(1100); driverCache.putIfAbsent(1L, "bar"); timeSource.advanceTime(1100); driverCache.remove(1L); driverCache.put(1L, "baz"); timeSource.advanceTime(1100); assertThat(driverCache.get(1L), nullValue()); @SuppressWarnings("unchecked") Matcher<Iterable<? extends CacheEvent<? extends Long, ? extends String>>> expectedSequence = contains( created(1L, "foo"), expired(1L, "foo"), created(1L, "bar"), expired(1L, "bar"), created(1L, "baz"), expired(1L, "baz")); within(Duration.ofSeconds(10)).runsCleanly(() -> { assertThat(driverEvents, expectedSequence); assertThat(observerEvents, expectedSequence); }); } } }
Example 18
Source File: EhCacheHelper.java From PeonyFramwork with Apache License 2.0 | 4 votes |
public static boolean remove(String key) { Cache<String, CacheEntity> cache=getCache(null); cache.remove(key); return true; }
Example 19
Source File: BasicClusteredWriteBehindPassthroughTest.java From ehcache3 with Apache License 2.0 | 4 votes |
private void condRemove(Cache<Long, String> cache, String value, boolean addToCacheRecords) { cache.remove(KEY, value); if (addToCacheRecords) { cacheRecords.add(new Record(KEY, null)); } }
Example 20
Source File: BasicClusteredWriteBehindPassthroughTest.java From ehcache3 with Apache License 2.0 | 4 votes |
private void remove(Cache<Long, String> cache) { cache.remove(KEY); cacheRecords.add(new Record(KEY, null)); }