1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
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 
17 #pragma once
18 
19 #include <utility>
20 
21 #include <folly/Optional.h>
22 #include <folly/container/EvictingCacheMap.h>
23 #include <folly/dynamic.h>
24 #include <wangle/client/persistence/PersistentCacheCommon.h>
25 
26 namespace wangle {
27 
28 /**
29  * A threadsafe cache map that delegates to an EvictingCacheMap and maintains
30  * a version of the data.
31  */
32 template <typename K, typename V, typename MutexT>
33 class LRUInMemoryCache {
34  public:
35   /**
36    * Create with the specified capacity.
37    */
LRUInMemoryCache(size_t capacity)38   explicit LRUInMemoryCache(size_t capacity) : cache_(capacity) {}
39   ~LRUInMemoryCache() = default;
40 
41   folly::Optional<V> get(const K& key);
42   void put(const K& key, const V& val);
43   bool remove(const K& key);
44   size_t size() const;
45   void clear();
46 
47   CacheDataVersion getVersion() const;
48 
49   /**
50    * Loads the list of kv pairs into the cache and bumps version.
51    * Returns the new cache version.
52    */
53   CacheDataVersion loadData(const folly::dynamic& kvPairs) noexcept;
54 
55   /**
56    * Get the cache data as a list of kv pairs along with the version
57    */
58   folly::Optional<std::pair<folly::dynamic, CacheDataVersion>>
59   convertToKeyValuePairs() noexcept;
60 
61   /**
62    * Determine if the cache has changed since the specified version
63    */
hasChangedSince(CacheDataVersion version)64   bool hasChangedSince(CacheDataVersion version) const {
65     return getVersion() != version;
66   }
67 
68  private:
69   // must be called under a write lock
incrementVersion()70   void incrementVersion() {
71     ++version_;
72     // if a uint64_t is incremented a billion times a second, it will still take
73     // 585 years to wrap around, so don't bother
74   }
75 
76   folly::EvictingCacheMap<K, V> cache_;
77   // Version always starts at 1
78   CacheDataVersion version_{kDefaultInitCacheDataVersion};
79   // mutable so we can take read locks in const methods
80   mutable MutexT cacheLock_;
81 };
82 
83 } // namespace wangle
84 
85 #include <wangle/client/persistence/LRUInMemoryCache-inl.h>
86