blob: 8d49a831669afecc795d211876bb1fa87c92338b [file] [log] [blame]
Madan Jampani10073672016-01-21 19:13:59 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampani10073672016-01-21 19:13:59 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Madan Jampani10073672016-01-21 19:13:59 -080016package org.onosproject.store.primitives.impl;
17
18import java.util.concurrent.CompletableFuture;
19import java.util.function.BiFunction;
20import java.util.function.Predicate;
21
22import org.onosproject.store.service.AsyncConsistentMap;
Madan Jampani62f15332016-02-01 13:18:39 -080023import org.onosproject.store.service.MapEventListener;
Madan Jampani10073672016-01-21 19:13:59 -080024import org.onosproject.store.service.Versioned;
25
26import com.google.common.cache.CacheBuilder;
27import com.google.common.cache.CacheLoader;
28import com.google.common.cache.LoadingCache;
29
30/**
31 * {@code AsyncConsistentMap} that caches entries on read.
32 * <p>
33 * The cache entries are automatically invalidated when updates are detected either locally or
34 * remotely.
35 * <p> This implementation only attempts to serve cached entries for {@link AsyncConsistentMap#get get}
36 * calls. All other calls skip the cache and directly go the backing map.
37 *
38 * @param <K> key type
39 * @param <V> value type
40 */
41public class CachingAsyncConsistentMap<K, V> extends DelegatingAsyncConsistentMap<K, V> {
sangyun-hancce07c52016-04-06 11:07:59 +090042 private int maxCacheSize = 10000;
Madan Jampani10073672016-01-21 19:13:59 -080043
44 private final LoadingCache<K, CompletableFuture<Versioned<V>>> cache =
45 CacheBuilder.newBuilder()
sangyun-hancce07c52016-04-06 11:07:59 +090046 .maximumSize(maxCacheSize)
47 .build(new CacheLoader<K, CompletableFuture<Versioned<V>>>() {
48 @Override
49 public CompletableFuture<Versioned<V>> load(K key)
50 throws Exception {
51 return CachingAsyncConsistentMap.super.get(key);
52 }
53 });
Madan Jampani10073672016-01-21 19:13:59 -080054
Madan Jampani62f15332016-02-01 13:18:39 -080055 private final MapEventListener<K, V> cacheInvalidator = event -> cache.invalidate(event.key());
56
sangyun-hancce07c52016-04-06 11:07:59 +090057 /**
58 * Default constructor.
59 *
60 * @param backingMap a distributed, strongly consistent map for backing
61 */
Madan Jampani10073672016-01-21 19:13:59 -080062 public CachingAsyncConsistentMap(AsyncConsistentMap<K, V> backingMap) {
63 super(backingMap);
Madan Jampani62f15332016-02-01 13:18:39 -080064 super.addListener(cacheInvalidator);
65 }
66
sangyun-hancce07c52016-04-06 11:07:59 +090067 /**
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
Madan Jampani62f15332016-02-01 13:18:39 -080079 @Override
80 public CompletableFuture<Void> destroy() {
81 return super.destroy().thenCompose(v -> removeListener(cacheInvalidator));
Madan Jampani10073672016-01-21 19:13:59 -080082 }
83
84 @Override
85 public CompletableFuture<Versioned<V>> get(K key) {
86 return cache.getUnchecked(key);
87 }
88
89 @Override
90 public CompletableFuture<Versioned<V>> computeIf(K key,
91 Predicate<? super V> condition,
92 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
93 return super.computeIf(key, condition, remappingFunction)
sangyun-hancce07c52016-04-06 11:07:59 +090094 .whenComplete((r, e) -> cache.invalidate(key));
Madan Jampani10073672016-01-21 19:13:59 -080095 }
96
97 @Override
98 public CompletableFuture<Versioned<V>> put(K key, V value) {
99 return super.put(key, value)
sangyun-hancce07c52016-04-06 11:07:59 +0900100 .whenComplete((r, e) -> cache.invalidate(key));
Madan Jampani10073672016-01-21 19:13:59 -0800101 }
102
103 @Override
104 public CompletableFuture<Versioned<V>> putAndGet(K key, V value) {
Simon Hunt5829c342016-03-07 17:01:43 -0800105 return super.putAndGet(key, value)
sangyun-hancce07c52016-04-06 11:07:59 +0900106 .whenComplete((r, e) -> cache.invalidate(key));
Madan Jampani10073672016-01-21 19:13:59 -0800107 }
108
109 @Override
110 public CompletableFuture<Versioned<V>> remove(K key) {
111 return super.remove(key)
sangyun-hancce07c52016-04-06 11:07:59 +0900112 .whenComplete((r, e) -> cache.invalidate(key));
Madan Jampani10073672016-01-21 19:13:59 -0800113 }
114
115 @Override
116 public CompletableFuture<Void> clear() {
117 return super.clear()
sangyun-hancce07c52016-04-06 11:07:59 +0900118 .whenComplete((r, e) -> cache.invalidateAll());
Madan Jampani10073672016-01-21 19:13:59 -0800119 }
120
121 @Override
122 public CompletableFuture<Boolean> remove(K key, V value) {
123 return super.remove(key, value)
124 .whenComplete((r, e) -> {
125 if (r) {
126 cache.invalidate(key);
127 }
128 });
129 }
130
131 @Override
132 public CompletableFuture<Boolean> remove(K key, long version) {
133 return super.remove(key, version)
134 .whenComplete((r, e) -> {
135 if (r) {
136 cache.invalidate(key);
137 }
138 });
139 }
140
141 @Override
142 public CompletableFuture<Versioned<V>> replace(K key, V value) {
143 return super.replace(key, value)
144 .whenComplete((r, e) -> cache.invalidate(key));
145 }
146
147 @Override
148 public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) {
149 return super.replace(key, oldValue, newValue)
150 .whenComplete((r, e) -> {
151 if (r) {
152 cache.invalidate(key);
153 }
154 });
155 }
156
157 @Override
158 public CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue) {
159 return super.replace(key, oldVersion, newValue)
160 .whenComplete((r, e) -> {
161 if (r) {
162 cache.invalidate(key);
163 }
164 });
165 }
166}