CachingAsyncConsistentMap: When changes are detected update cache with new value
Change-Id: I51307a8bff953389feeb8928f591151058d49eab
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 75d46c0..5c0eaef 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
@@ -51,7 +51,7 @@
private final LoadingCache<K, CompletableFuture<Versioned<V>>> cache;
- private final MapEventListener<K, V> cacheInvalidator;
+ private final MapEventListener<K, V> cacheUpdater;
private final Consumer<Status> statusListener;
/**
@@ -74,7 +74,14 @@
cache = CacheBuilder.newBuilder()
.maximumSize(cacheSize)
.build(CacheLoader.from(CachingAsyncConsistentMap.super::get));
- cacheInvalidator = event -> cache.invalidate(event.key());
+ cacheUpdater = event -> {
+ Versioned<V> newValue = event.newValue();
+ if (newValue == null) {
+ cache.invalidate(event.key());
+ } else {
+ cache.put(event.key(), CompletableFuture.completedFuture(newValue));
+ }
+ };
statusListener = status -> {
log.debug("{} status changed to {}", this.name(), status);
// If the status of the underlying map is SUSPENDED or INACTIVE
@@ -83,14 +90,14 @@
cache.invalidateAll();
}
};
- super.addListener(cacheInvalidator);
+ super.addListener(cacheUpdater);
super.addStatusChangeListener(statusListener);
}
@Override
public CompletableFuture<Void> destroy() {
super.removeStatusChangeListener(statusListener);
- return super.destroy().thenCompose(v -> removeListener(cacheInvalidator));
+ return super.destroy().thenCompose(v -> removeListener(cacheUpdater));
}
@Override