userver: userver/cache/nway_lru_cache.hpp Source File
Loading...
Searching...
No Matches
nway_lru_cache.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <functional>
7#include <mutex>
8#include <optional>
9#include <utility>
10
11#include <boost/container_hash/hash.hpp>
12
19
20USERVER_NAMESPACE_BEGIN
21
22namespace cache {
23
28template <typename T, typename U, typename Hash = std::hash<T>, typename Equal = std::equal_to<T>>
29class NWayLRU final {
30public:
51 NWayLRU(size_t ways, size_t way_size, const Hash& hash = Hash(), const Equal& equal = Equal());
52
56 void Put(const T& key, U value);
57
65 template <typename Validator>
66 std::optional<U> Get(const T& key, Validator validator);
67
72 std::optional<U> Get(const T& key) {
73 return Get(key, [](const U&) { return true; });
74 }
75
81 U GetOr(const T& key, const U& default_value);
82
84 void Invalidate();
85
88 void InvalidateByKey(const T& key);
89
93 template <typename Function>
94 void VisitAll(Function func) const;
95
98 size_t GetSize() const;
99
103 void UpdateWaySize(size_t way_size);
104
107 void Write(dump::Writer& writer) const;
108
112 void Read(dump::Reader& reader);
113
117 void SetDumper(std::shared_ptr<dump::Dumper> dumper);
118
119private:
120 template <typename, typename, typename, typename>
121 friend class ExpirableLruCache;
122
123 using ItemMutex = concurrent::ItemMutex<T, Equal>;
124
125 struct Way {
126 Way(size_t way_size, const Hash& hash, const Equal& equal)
127 : mutex_data(0, utils::CachedHashKeyEqual<Equal>{equal}),
128 cache(way_size, hash, equal)
129 {}
130
131 // The same mutex protects the LRU and the set of keys being updated.
132 mutable typename ItemMutex::MutexDatum mutex_data;
133 LruMap<T, U, Hash, Equal> cache;
134 };
135
136 Way& GetWay(const T& key) { return GetWayByHash(hash_fn_(key)); }
137
138 Way& GetWayByHash(size_t hash);
139
140 ItemMutex GetMutexForKey(const T& key) {
141 const size_t hash = hash_fn_(key);
142 return ItemMutex(GetWayByHash(hash).mutex_data, {.hash = hash, .key = key});
143 }
144
145 void NotifyDumper();
146
147 utils::FixedArray<Way> caches_;
148 [[no_unique_address]] Hash hash_fn_;
149 std::shared_ptr<dump::Dumper> dumper_{nullptr};
150};
151
152template <typename T, typename U, typename Hash, typename Eq>
153NWayLRU<T, U, Hash, Eq>::NWayLRU(size_t ways, size_t way_size, const Hash& hash, const Eq& equal)
154 : caches_(ways, way_size, hash, equal),
155 hash_fn_(hash)
156{
157 if (ways == 0) {
158 throw std::logic_error("Ways must be positive");
159 }
160}
161
162template <typename T, typename U, typename Hash, typename Eq>
163void NWayLRU<T, U, Hash, Eq>::Put(const T& key, U value) {
164 auto& way = GetWay(key);
165 {
166 const std::unique_lock lock{way.mutex_data.mutex};
167 way.cache.Put(key, std::move(value));
168 }
169 NotifyDumper();
170}
171
172template <typename T, typename U, typename Hash, typename Eq>
173template <typename Validator>
174std::optional<U> NWayLRU<T, U, Hash, Eq>::Get(const T& key, Validator validator) {
175 auto& way = GetWay(key);
176 const std::unique_lock lock{way.mutex_data.mutex};
177 auto* value = way.cache.Get(key);
178
179 if (value) {
180 if (validator(*value)) {
181 return *value;
182 }
183 way.cache.Erase(key);
184 }
185
186 return std::nullopt;
187}
188
189template <typename T, typename U, typename Hash, typename Eq>
191 auto& way = GetWay(key);
192 {
193 const std::unique_lock lock{way.mutex_data.mutex};
194 way.cache.Erase(key);
195 }
196 NotifyDumper();
197}
198
199template <typename T, typename U, typename Hash, typename Eq>
200U NWayLRU<T, U, Hash, Eq>::GetOr(const T& key, const U& default_value) {
201 auto& way = GetWay(key);
202 const std::unique_lock lock{way.mutex_data.mutex};
203 return way.cache.GetOr(key, default_value);
204}
205
206template <typename T, typename U, typename Hash, typename Eq>
208 for (auto& way : caches_) {
209 const std::unique_lock lock{way.mutex_data.mutex};
210 way.cache.Clear();
211 }
212 NotifyDumper();
213}
214
215template <typename T, typename U, typename Hash, typename Eq>
216template <typename Function>
217void NWayLRU<T, U, Hash, Eq>::VisitAll(Function func) const {
218 for (const auto& way : caches_) {
219 const std::unique_lock lock{way.mutex_data.mutex};
220 way.cache.VisitAll(func);
221 }
222}
223
224template <typename T, typename U, typename Hash, typename Eq>
226 size_t size{0};
227 for (const auto& way : caches_) {
228 const std::unique_lock lock{way.mutex_data.mutex};
229 size += way.cache.GetSize();
230 }
231 return size;
232}
233
234template <typename T, typename U, typename Hash, typename Eq>
236 for (auto& way : caches_) {
237 const std::unique_lock lock{way.mutex_data.mutex};
238 way.cache.SetMaxSize(way_size);
239 }
240}
241
242template <typename T, typename U, typename Hash, typename Eq>
243typename NWayLRU<T, U, Hash, Eq>::Way& NWayLRU<T, U, Hash, Eq>::GetWayByHash(size_t seed) {
248 boost::hash_combine(seed, 0);
249 auto n = seed % caches_.size();
250 return caches_[n];
251}
252
253template <typename T, typename U, typename Hash, typename Equal>
255 writer.Write(caches_.size());
256
257 for (const Way& way : caches_) {
258 const std::unique_lock lock{way.mutex_data.mutex};
259
260 writer.Write(way.cache.GetSize());
261
262 way.cache.VisitAll([&writer](const T& key, const U& value) {
263 writer.Write(key);
264 writer.Write(value);
265 });
266 }
267}
268
269template <typename T, typename U, typename Hash, typename Equal>
271 Invalidate();
272
273 const auto ways = reader.Read<std::size_t>();
274 for (std::size_t i = 0; i < ways; ++i) {
275 const auto elements_in_way = reader.Read<std::size_t>();
276 for (std::size_t j = 0; j < elements_in_way; ++j) {
277 auto key = reader.Read<T>();
278 auto value = reader.Read<U>();
279 Put(std::move(key), std::move(value));
280 }
281 }
282}
283
284template <typename T, typename U, typename Hash, typename Equal>
285void NWayLRU<T, U, Hash, Equal>::NotifyDumper() {
286 if (dumper_ != nullptr) {
287 dumper_->OnUpdateCompleted();
288 }
289}
290
291template <typename T, typename U, typename Hash, typename Equal>
292void NWayLRU<T, U, Hash, Equal>::SetDumper(std::shared_ptr<dump::Dumper> dumper) {
293 dumper_ = std::move(dumper);
294}
295
296} // namespace cache
297
298USERVER_NAMESPACE_END