blob: aaa01e4fcb8c4377d5a42ab17b4e029eb9ca74c6 [file] [log] [blame]
Madan Jampani10073672016-01-21 19:13:59 -08001/*
2 * Copyright 2016 Open Networking Laboratory
3 *
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;
24import 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> {
42
43 private final LoadingCache<K, CompletableFuture<Versioned<V>>> cache =
44 CacheBuilder.newBuilder()
45 .maximumSize(10000) // TODO: make configurable
46 .build(new CacheLoader<K, CompletableFuture<Versioned<V>>>() {
47 @Override
48 public CompletableFuture<Versioned<V>> load(K key)
49 throws Exception {
50 return CachingAsyncConsistentMap.super.get(key);
51 }
52 });
53
54 public CachingAsyncConsistentMap(AsyncConsistentMap<K, V> backingMap) {
55 super(backingMap);
56 super.addListener(event -> cache.invalidate(event.key()));
57 }
58
59 @Override
60 public CompletableFuture<Versioned<V>> get(K key) {
61 return cache.getUnchecked(key);
62 }
63
64 @Override
65 public CompletableFuture<Versioned<V>> computeIf(K key,
66 Predicate<? super V> condition,
67 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
68 return super.computeIf(key, condition, remappingFunction)
69 .whenComplete((r, e) -> cache.invalidate(key));
70 }
71
72 @Override
73 public CompletableFuture<Versioned<V>> put(K key, V value) {
74 return super.put(key, value)
75 .whenComplete((r, e) -> cache.invalidate(key));
76 }
77
78 @Override
79 public CompletableFuture<Versioned<V>> putAndGet(K key, V value) {
80 return super.put(key, value)
81 .whenComplete((r, e) -> cache.invalidate(key));
82 }
83
84 @Override
85 public CompletableFuture<Versioned<V>> remove(K key) {
86 return super.remove(key)
87 .whenComplete((r, e) -> cache.invalidate(key));
88 }
89
90 @Override
91 public CompletableFuture<Void> clear() {
92 return super.clear()
93 .whenComplete((r, e) -> cache.invalidateAll());
94 }
95
96 @Override
97 public CompletableFuture<Boolean> remove(K key, V value) {
98 return super.remove(key, value)
99 .whenComplete((r, e) -> {
100 if (r) {
101 cache.invalidate(key);
102 }
103 });
104 }
105
106 @Override
107 public CompletableFuture<Boolean> remove(K key, long version) {
108 return super.remove(key, version)
109 .whenComplete((r, e) -> {
110 if (r) {
111 cache.invalidate(key);
112 }
113 });
114 }
115
116 @Override
117 public CompletableFuture<Versioned<V>> replace(K key, V value) {
118 return super.replace(key, value)
119 .whenComplete((r, e) -> cache.invalidate(key));
120 }
121
122 @Override
123 public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) {
124 return super.replace(key, oldValue, newValue)
125 .whenComplete((r, e) -> {
126 if (r) {
127 cache.invalidate(key);
128 }
129 });
130 }
131
132 @Override
133 public CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue) {
134 return super.replace(key, oldVersion, newValue)
135 .whenComplete((r, e) -> {
136 if (r) {
137 cache.invalidate(key);
138 }
139 });
140 }
141}