blob: b2c7f19483ff1c989c4e3d77ce3f55e7dce91c0c [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 */
16
17package org.onosproject.store.primitives.impl;
18
19import java.util.concurrent.CompletableFuture;
20import java.util.function.BiFunction;
21import java.util.function.Predicate;
22
23import org.onosproject.store.service.AsyncConsistentMap;
Madan Jampani62f15332016-02-01 13:18:39 -080024import org.onosproject.store.service.MapEventListener;
Madan Jampani10073672016-01-21 19:13:59 -080025import org.onosproject.store.service.Versioned;
26
27import com.google.common.cache.CacheBuilder;
28import com.google.common.cache.CacheLoader;
29import com.google.common.cache.LoadingCache;
30
31/**
32 * {@code AsyncConsistentMap} that caches entries on read.
33 * <p>
34 * The cache entries are automatically invalidated when updates are detected either locally or
35 * remotely.
36 * <p> This implementation only attempts to serve cached entries for {@link AsyncConsistentMap#get get}
37 * calls. All other calls skip the cache and directly go the backing map.
38 *
39 * @param <K> key type
40 * @param <V> value type
41 */
42public class CachingAsyncConsistentMap<K, V> extends DelegatingAsyncConsistentMap<K, V> {
43
44 private final LoadingCache<K, CompletableFuture<Versioned<V>>> cache =
45 CacheBuilder.newBuilder()
46 .maximumSize(10000) // TODO: make configurable
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 });
54
Madan Jampani62f15332016-02-01 13:18:39 -080055 private final MapEventListener<K, V> cacheInvalidator = event -> cache.invalidate(event.key());
56
Madan Jampani10073672016-01-21 19:13:59 -080057 public CachingAsyncConsistentMap(AsyncConsistentMap<K, V> backingMap) {
58 super(backingMap);
Madan Jampani62f15332016-02-01 13:18:39 -080059 super.addListener(cacheInvalidator);
60 }
61
62 @Override
63 public CompletableFuture<Void> destroy() {
64 return super.destroy().thenCompose(v -> removeListener(cacheInvalidator));
Madan Jampani10073672016-01-21 19:13:59 -080065 }
66
67 @Override
68 public CompletableFuture<Versioned<V>> get(K key) {
69 return cache.getUnchecked(key);
70 }
71
72 @Override
73 public CompletableFuture<Versioned<V>> computeIf(K key,
74 Predicate<? super V> condition,
75 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
76 return super.computeIf(key, condition, remappingFunction)
77 .whenComplete((r, e) -> cache.invalidate(key));
78 }
79
80 @Override
81 public CompletableFuture<Versioned<V>> put(K key, V value) {
82 return super.put(key, value)
83 .whenComplete((r, e) -> cache.invalidate(key));
84 }
85
86 @Override
87 public CompletableFuture<Versioned<V>> putAndGet(K key, V value) {
Simon Hunt5829c342016-03-07 17:01:43 -080088 return super.putAndGet(key, value)
Madan Jampani10073672016-01-21 19:13:59 -080089 .whenComplete((r, e) -> cache.invalidate(key));
90 }
91
92 @Override
93 public CompletableFuture<Versioned<V>> remove(K key) {
94 return super.remove(key)
95 .whenComplete((r, e) -> cache.invalidate(key));
96 }
97
98 @Override
99 public CompletableFuture<Void> clear() {
100 return super.clear()
101 .whenComplete((r, e) -> cache.invalidateAll());
102 }
103
104 @Override
105 public CompletableFuture<Boolean> remove(K key, V value) {
106 return super.remove(key, value)
107 .whenComplete((r, e) -> {
108 if (r) {
109 cache.invalidate(key);
110 }
111 });
112 }
113
114 @Override
115 public CompletableFuture<Boolean> remove(K key, long version) {
116 return super.remove(key, version)
117 .whenComplete((r, e) -> {
118 if (r) {
119 cache.invalidate(key);
120 }
121 });
122 }
123
124 @Override
125 public CompletableFuture<Versioned<V>> replace(K key, V value) {
126 return super.replace(key, value)
127 .whenComplete((r, e) -> cache.invalidate(key));
128 }
129
130 @Override
131 public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) {
132 return super.replace(key, oldValue, newValue)
133 .whenComplete((r, e) -> {
134 if (r) {
135 cache.invalidate(key);
136 }
137 });
138 }
139
140 @Override
141 public CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue) {
142 return super.replace(key, oldVersion, newValue)
143 .whenComplete((r, e) -> {
144 if (r) {
145 cache.invalidate(key);
146 }
147 });
148 }
149}