Committed by
Gerrit Code Review
Add constructor for configuration of CacheSize
Change-Id: Ifdac489d87ad1b1ab23e43f05980f9cf0e5b834e
Showing
1 changed file
with
31 additions
and
14 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,25 +39,43 @@ import com.google.common.cache.LoadingCache; | ... | @@ -40,25 +39,43 @@ 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) |
| 50 | - throws Exception { | 50 | + throws Exception { |
| 51 | - return CachingAsyncConsistentMap.super.get(key); | 51 | + return CachingAsyncConsistentMap.super.get(key); |
| 52 | - } | 52 | + } |
| 53 | - }); | 53 | + }); |
| 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)); |
| ... | @@ -74,31 +91,31 @@ public class CachingAsyncConsistentMap<K, V> extends DelegatingAsyncConsistentMa | ... | @@ -74,31 +91,31 @@ public class CachingAsyncConsistentMap<K, V> extends DelegatingAsyncConsistentMa |
| 74 | Predicate<? super V> condition, | 91 | Predicate<? super V> condition, |
| 75 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { | 92 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
| 76 | return super.computeIf(key, condition, remappingFunction) | 93 | return super.computeIf(key, condition, remappingFunction) |
| 77 | - .whenComplete((r, e) -> cache.invalidate(key)); | 94 | + .whenComplete((r, e) -> cache.invalidate(key)); |
| 78 | } | 95 | } |
| 79 | 96 | ||
| 80 | @Override | 97 | @Override |
| 81 | public CompletableFuture<Versioned<V>> put(K key, V value) { | 98 | public CompletableFuture<Versioned<V>> put(K key, V value) { |
| 82 | return super.put(key, value) | 99 | return super.put(key, value) |
| 83 | - .whenComplete((r, e) -> cache.invalidate(key)); | 100 | + .whenComplete((r, e) -> cache.invalidate(key)); |
| 84 | } | 101 | } |
| 85 | 102 | ||
| 86 | @Override | 103 | @Override |
| 87 | public CompletableFuture<Versioned<V>> putAndGet(K key, V value) { | 104 | public CompletableFuture<Versioned<V>> putAndGet(K key, V value) { |
| 88 | return super.putAndGet(key, value) | 105 | return super.putAndGet(key, value) |
| 89 | - .whenComplete((r, e) -> cache.invalidate(key)); | 106 | + .whenComplete((r, e) -> cache.invalidate(key)); |
| 90 | } | 107 | } |
| 91 | 108 | ||
| 92 | @Override | 109 | @Override |
| 93 | public CompletableFuture<Versioned<V>> remove(K key) { | 110 | public CompletableFuture<Versioned<V>> remove(K key) { |
| 94 | return super.remove(key) | 111 | return super.remove(key) |
| 95 | - .whenComplete((r, e) -> cache.invalidate(key)); | 112 | + .whenComplete((r, e) -> cache.invalidate(key)); |
| 96 | } | 113 | } |
| 97 | 114 | ||
| 98 | @Override | 115 | @Override |
| 99 | public CompletableFuture<Void> clear() { | 116 | public CompletableFuture<Void> clear() { |
| 100 | return super.clear() | 117 | return super.clear() |
| 101 | - .whenComplete((r, e) -> cache.invalidateAll()); | 118 | + .whenComplete((r, e) -> cache.invalidateAll()); |
| 102 | } | 119 | } |
| 103 | 120 | ||
| 104 | @Override | 121 | @Override | ... | ... |
-
Please register or login to post a comment