proxygen
LRUInMemoryCache-inl.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
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 #pragma once
17 
18 #include <folly/DynamicConverter.h>
19 #include <folly/Likely.h>
20 
21 namespace wangle {
22 
23 template<typename K, typename V, typename M>
25  // need to take a write lock since get modifies the LRU
26  typename wangle::CacheLockGuard<M>::Write writeLock(cacheLock_);
27  auto itr = cache_.find(key);
28  if (itr != cache_.end()) {
29  return folly::Optional<V>(itr->second);
30  }
31  return folly::none;
32 }
33 
34 template<typename K, typename V, typename M>
35 void LRUInMemoryCache<K, V, M>::put(const K& key, const V& val) {
36  typename wangle::CacheLockGuard<M>::Write writeLock(cacheLock_);
37  cache_.set(key, val);
38  incrementVersion();
39 }
40 
41 template<typename K, typename V, typename M>
43  typename wangle::CacheLockGuard<M>::Write writeLock(cacheLock_);
44  size_t nErased = cache_.erase(key);
45  if (nErased > 0) {
46  incrementVersion();
47  return true;
48  }
49  return false;
50 }
51 
52 template<typename K, typename V, typename M>
54  typename wangle::CacheLockGuard<M>::Read readLock(cacheLock_);
55  return cache_.size();
56 }
57 
58 template<typename K, typename V, typename M>
60  typename wangle::CacheLockGuard<M>::Write writeLock(cacheLock_);
61  if (cache_.empty()) {
62  return;
63  }
64  cache_.clear();
65  incrementVersion();
66 }
67 
68 template<typename K, typename V, typename M>
70  typename wangle::CacheLockGuard<M>::Read readLock(cacheLock_);
71  return version_;
72 }
73 
74 template<typename K, typename V, typename M>
77  bool updated = false;
78  typename wangle::CacheLockGuard<M>::Write writeLock(cacheLock_);
79  try {
80  for (const auto& kv : data) {
81  cache_.set(
82  folly::convertTo<K>(kv[0]),
83  folly::convertTo<V>(kv[1]));
84  updated = true;
85  }
86  } catch (const folly::TypeError& err) {
87  LOG(ERROR) << "Load cache failed with type error: "
88  << err.what();
89  } catch (const std::out_of_range& err) {
90  LOG(ERROR) << "Load cache failed with key error: "
91  << err.what();
92  } catch (const std::exception& err) {
93  LOG(ERROR) << "Load cache failed with error: "
94  << err.what();
95  }
96  if (updated) {
97  // we still need to increment the version
98  incrementVersion();
99  }
100  return version_;
101 }
102 
103 template<typename K, typename V, typename M>
106  typename wangle::CacheLockGuard<M>::Read readLock(cacheLock_);
107  try {
109  for (const auto& kv : cache_) {
110  dynObj.push_back(folly::toDynamic(std::make_pair(kv.first, kv.second)));
111  }
112  return std::make_pair(std::move(dynObj), version_);
113  } catch (const std::exception& err) {
114  LOG(ERROR) << "Converting cache to folly::dynamic failed with error: "
115  << err.what();
116  }
117  return folly::none;
118 }
119 
120 }
uint64_t CacheDataVersion
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
double val
Definition: String.cpp:273
requires E e noexcept(noexcept(s.error(std::move(e))))
folly::Optional< V > get(const K &key)
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
void push_back(dynamic const &)
Definition: dynamic-inl.h:969
static void array(EmptyArrayTag)
Definition: dynamic-inl.h:233
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
dynamic toDynamic(const T &)
constexpr None none
Definition: Optional.h:87