Committed by
Gerrit Code Review
Add constructor for configuration of CacheSize
Change-Id: Ifdac489d87ad1b1ab23e43f05980f9cf0e5b834e
Showing
1 changed file
with
19 additions
and
2 deletions
... | @@ -13,7 +13,6 @@ | ... | @@ -13,7 +13,6 @@ |
13 | * See the License for the specific language governing permissions and | 13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. | 14 | * limitations under the License. |
15 | */ | 15 | */ |
16 | - | ||
17 | package org.onosproject.store.primitives.impl; | 16 | package org.onosproject.store.primitives.impl; |
18 | 17 | ||
19 | import java.util.concurrent.CompletableFuture; | 18 | import java.util.concurrent.CompletableFuture; |
... | @@ -40,10 +39,11 @@ import com.google.common.cache.LoadingCache; | ... | @@ -40,10 +39,11 @@ import com.google.common.cache.LoadingCache; |
40 | * @param <V> value type | 39 | * @param <V> value type |
41 | */ | 40 | */ |
42 | public class CachingAsyncConsistentMap<K, V> extends DelegatingAsyncConsistentMap<K, V> { | 41 | public class CachingAsyncConsistentMap<K, V> extends DelegatingAsyncConsistentMap<K, V> { |
42 | + private int maxCacheSize = 10000; | ||
43 | 43 | ||
44 | private final LoadingCache<K, CompletableFuture<Versioned<V>>> cache = | 44 | private final LoadingCache<K, CompletableFuture<Versioned<V>>> cache = |
45 | CacheBuilder.newBuilder() | 45 | CacheBuilder.newBuilder() |
46 | - .maximumSize(10000) // TODO: make configurable | 46 | + .maximumSize(maxCacheSize) |
47 | .build(new CacheLoader<K, CompletableFuture<Versioned<V>>>() { | 47 | .build(new CacheLoader<K, CompletableFuture<Versioned<V>>>() { |
48 | @Override | 48 | @Override |
49 | public CompletableFuture<Versioned<V>> load(K key) | 49 | public CompletableFuture<Versioned<V>> load(K key) |
... | @@ -54,11 +54,28 @@ public class CachingAsyncConsistentMap<K, V> extends DelegatingAsyncConsistentMa | ... | @@ -54,11 +54,28 @@ public class CachingAsyncConsistentMap<K, V> extends DelegatingAsyncConsistentMa |
54 | 54 | ||
55 | private final MapEventListener<K, V> cacheInvalidator = event -> cache.invalidate(event.key()); | 55 | private final MapEventListener<K, V> cacheInvalidator = event -> cache.invalidate(event.key()); |
56 | 56 | ||
57 | + /** | ||
58 | + * Default constructor. | ||
59 | + * | ||
60 | + * @param backingMap a distributed, strongly consistent map for backing | ||
61 | + */ | ||
57 | public CachingAsyncConsistentMap(AsyncConsistentMap<K, V> backingMap) { | 62 | public CachingAsyncConsistentMap(AsyncConsistentMap<K, V> backingMap) { |
58 | super(backingMap); | 63 | super(backingMap); |
59 | super.addListener(cacheInvalidator); | 64 | super.addListener(cacheInvalidator); |
60 | } | 65 | } |
61 | 66 | ||
67 | + /** | ||
68 | + * Constructor to configure cache size of LoadingCache. | ||
69 | + * | ||
70 | + * @param backingMap a distributed, strongly consistent map for backing | ||
71 | + * @param cacheSize the maximum size of the cache | ||
72 | + */ | ||
73 | + public CachingAsyncConsistentMap(AsyncConsistentMap<K, V> backingMap, int cacheSize) { | ||
74 | + super(backingMap); | ||
75 | + super.addListener(cacheInvalidator); | ||
76 | + maxCacheSize = cacheSize; | ||
77 | + } | ||
78 | + | ||
62 | @Override | 79 | @Override |
63 | public CompletableFuture<Void> destroy() { | 80 | public CompletableFuture<Void> destroy() { |
64 | return super.destroy().thenCompose(v -> removeListener(cacheInvalidator)); | 81 | return super.destroy().thenCompose(v -> removeListener(cacheInvalidator)); | ... | ... |
-
Please register or login to post a comment