sangyun-han
Committed by Gerrit Code Review

Add constructor for configuration of CacheSize

Change-Id: Ifdac489d87ad1b1ab23e43f05980f9cf0e5b834e
......@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.primitives.impl;
import java.util.concurrent.CompletableFuture;
......@@ -40,10 +39,11 @@ import com.google.common.cache.LoadingCache;
* @param <V> value type
*/
public class CachingAsyncConsistentMap<K, V> extends DelegatingAsyncConsistentMap<K, V> {
private int maxCacheSize = 10000;
private final LoadingCache<K, CompletableFuture<Versioned<V>>> cache =
CacheBuilder.newBuilder()
.maximumSize(10000) // TODO: make configurable
.maximumSize(maxCacheSize)
.build(new CacheLoader<K, CompletableFuture<Versioned<V>>>() {
@Override
public CompletableFuture<Versioned<V>> load(K key)
......@@ -54,11 +54,28 @@ public class CachingAsyncConsistentMap<K, V> extends DelegatingAsyncConsistentMa
private final MapEventListener<K, V> cacheInvalidator = event -> cache.invalidate(event.key());
/**
* Default constructor.
*
* @param backingMap a distributed, strongly consistent map for backing
*/
public CachingAsyncConsistentMap(AsyncConsistentMap<K, V> backingMap) {
super(backingMap);
super.addListener(cacheInvalidator);
}
/**
* Constructor to configure cache size of LoadingCache.
*
* @param backingMap a distributed, strongly consistent map for backing
* @param cacheSize the maximum size of the cache
*/
public CachingAsyncConsistentMap(AsyncConsistentMap<K, V> backingMap, int cacheSize) {
super(backingMap);
super.addListener(cacheInvalidator);
maxCacheSize = cacheSize;
}
@Override
public CompletableFuture<Void> destroy() {
return super.destroy().thenCompose(v -> removeListener(cacheInvalidator));
......