CachingAsyncConsistentMap: evict failed get responses from cache
AtomixLeaderElector: evict failed getLeadership responses from cache immediately

Change-Id: I084abbe8f4669b98f85bc60dc6af9240630b7ce0
diff --git a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/CachingAsyncConsistentMap.java b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/CachingAsyncConsistentMap.java
index 34d4e21..75d46c0 100644
--- a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/CachingAsyncConsistentMap.java
+++ b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/CachingAsyncConsistentMap.java
@@ -95,7 +95,12 @@
 
     @Override
     public CompletableFuture<Versioned<V>> get(K key) {
-        return cache.getUnchecked(key);
+        return cache.getUnchecked(key)
+                .whenComplete((r, e) -> {
+                    if (e != null) {
+                        cache.invalidate(key);
+                    }
+                });
     }
 
     @Override
@@ -133,11 +138,11 @@
     @Override
     public CompletableFuture<Boolean> remove(K key, V value) {
         return super.remove(key, value)
-                    .whenComplete((r, e) -> {
-                        if (r) {
-                            cache.invalidate(key);
-                        }
-                    });
+                .whenComplete((r, e) -> {
+                    if (r) {
+                        cache.invalidate(key);
+                    }
+                });
     }
 
     @Override
@@ -153,7 +158,7 @@
     @Override
     public CompletableFuture<Versioned<V>> replace(K key, V value) {
         return super.replace(key, value)
-                    .whenComplete((r, e) -> cache.invalidate(key));
+                .whenComplete((r, e) -> cache.invalidate(key));
     }
 
     @Override
diff --git a/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/AtomixLeaderElector.java b/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/AtomixLeaderElector.java
index 6baa835..be5d9ca 100644
--- a/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/AtomixLeaderElector.java
+++ b/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/AtomixLeaderElector.java
@@ -135,7 +135,12 @@
 
     @Override
     public CompletableFuture<Leadership> getLeadership(String topic) {
-        return cache.getUnchecked(topic);
+        return cache.getUnchecked(topic)
+                .whenComplete((r, e) -> {
+                    if (e != null) {
+                        cache.invalidate(topic);
+                    }
+                });
     }
 
     @Override