From ba81c3603c489bcd7086197bff22406d67ed21cf Mon Sep 17 00:00:00 2001 From: Ben Manes Date: Mon, 13 Jul 2015 09:32:37 -0700 Subject: [PATCH] Silently publish the JCache event This slight change is to ensure that the `ignoreSynchronous()` does not ignore other events already recorded. However, we want to ignore any events occuring on the background executor. Since these types of evictions are not user facing and may be asynchronous, they are not required to be blocking calls to user-facing threads. --- README.md | 3 ++ .../jcache/event/EventDispatcher.java | 36 +++++++++++++++---- .../jcache/event/JCacheEvictionListener.java | 5 ++- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index cc146d34c6..6a52c10566 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,9 @@ compile 'com.github.ben-manes.caffeine:caffeine:1.3.0' compile 'com.github.ben-manes.caffeine:guava:1.3.0' compile 'com.github.ben-manes.caffeine:jcache:1.3.0' compile 'com.github.ben-manes.caffeine:tracing-async:1.3.0' + +// Transitive requirement (if jars manually managed) +compile 'com.github.ben-manes.caffeine:tracing-api:1.3.0' ``` Snapshots of the development version are available in diff --git a/jcache/src/main/java/com/github/benmanes/caffeine/jcache/event/EventDispatcher.java b/jcache/src/main/java/com/github/benmanes/caffeine/jcache/event/EventDispatcher.java index bac87bec57..798f4e8650 100644 --- a/jcache/src/main/java/com/github/benmanes/caffeine/jcache/event/EventDispatcher.java +++ b/jcache/src/main/java/com/github/benmanes/caffeine/jcache/event/EventDispatcher.java @@ -104,7 +104,7 @@ public void deregister(CacheEntryListenerConfiguration configuration) { * @param value the entry's value */ public void publishCreated(Cache cache, K key, V value) { - publish(new JCacheEntryEvent<>(cache, EventType.CREATED, key, null, value)); + publish(new JCacheEntryEvent<>(cache, EventType.CREATED, key, null, value), false); } /** @@ -116,7 +116,7 @@ public void publishCreated(Cache cache, K key, V value) { * @param newValue the entry's new value */ public void publishUpdated(Cache cache, K key, V oldValue, V newValue) { - publish(new JCacheEntryEvent<>(cache, EventType.UPDATED, key, oldValue, newValue)); + publish(new JCacheEntryEvent<>(cache, EventType.UPDATED, key, oldValue, newValue), false); } /** @@ -127,7 +127,19 @@ public void publishUpdated(Cache cache, K key, V oldValue, V newValue) { * @param value the entry's value */ public void publishRemoved(Cache cache, K key, V value) { - publish(new JCacheEntryEvent<>(cache, EventType.REMOVED, key, null, value)); + publish(new JCacheEntryEvent<>(cache, EventType.REMOVED, key, null, value), false); + } + + /** + * Publishes a remove event for the entry to all of the interested listeners. This method does + * not register the synchronous listener's future with {@link #awaitSynchronous()}. + * + * @param cache the cache where the entry was removed + * @param key the entry's key + * @param value the entry's value + */ + public void publishRemovedQuietly(Cache cache, K key, V value) { + publish(new JCacheEntryEvent<>(cache, EventType.REMOVED, key, null, value), true); } /** @@ -138,7 +150,19 @@ public void publishRemoved(Cache cache, K key, V value) { * @param value the entry's value */ public void publishExpired(Cache cache, K key, V value) { - publish(new JCacheEntryEvent<>(cache, EventType.EXPIRED, key, value, null)); + publish(new JCacheEntryEvent<>(cache, EventType.EXPIRED, key, value, null), false); + } + + /** + * Publishes a expire event for the entry to all of the interested listeners. This method does + * not register the synchronous listener's future with {@link #awaitSynchronous()}. + * + * @param cache the cache where the entry expired + * @param key the entry's key + * @param value the entry's value + */ + public void publishExpiredQuietly(Cache cache, K key, V value) { + publish(new JCacheEntryEvent<>(cache, EventType.EXPIRED, key, value, null), true); } /** @@ -167,7 +191,7 @@ public void ignoreSynchronous() { } /** Broadcasts the event to all of the interested listener's dispatch queues. */ - private void publish(JCacheEntryEvent event) { + private void publish(JCacheEntryEvent event, boolean quiet) { dispatchQueues.keySet().stream() .filter(registration -> registration.getCacheEntryFilter().evaluate(event)) .filter(registration -> registration.getCacheEntryListener().isCompatible(event)) @@ -177,7 +201,7 @@ private void publish(JCacheEntryEvent event) { Runnable action = () -> registration.getCacheEntryListener().dispatch(event); return queue.thenRunAsync(action, exectuor); }); - if ((future != null) && registration.isSynchronous()) { + if ((future != null) && registration.isSynchronous() && !quiet) { return future; } return null; diff --git a/jcache/src/main/java/com/github/benmanes/caffeine/jcache/event/JCacheEvictionListener.java b/jcache/src/main/java/com/github/benmanes/caffeine/jcache/event/JCacheEvictionListener.java index 409a381b78..bcf78862d3 100644 --- a/jcache/src/main/java/com/github/benmanes/caffeine/jcache/event/JCacheEvictionListener.java +++ b/jcache/src/main/java/com/github/benmanes/caffeine/jcache/event/JCacheEvictionListener.java @@ -58,11 +58,10 @@ public void write(K key, Expirable value) {} public void delete(K key, Expirable value, RemovalCause cause) { if (cause.wasEvicted()) { if (cause == RemovalCause.EXPIRED) { - dispatcher.publishExpired(cache, key, value.get()); + dispatcher.publishExpiredQuietly(cache, key, value.get()); } else { - dispatcher.publishRemoved(cache, key, value.get()); + dispatcher.publishRemovedQuietly(cache, key, value.get()); } - dispatcher.ignoreSynchronous(); statistics.recordEvictions(1L); } }