7#include <userver/cache/lru_map.hpp>
8#include <userver/dump/dumper.hpp>
9#include <userver/dump/operations.hpp>
10#include <userver/engine/mutex.hpp>
12USERVER_NAMESPACE_BEGIN
17template <
typename T,
typename U,
typename Hash = std::hash<T>,
18 typename Equal = std::equal_to<T>>
34 NWayLRU(size_t ways, size_t way_size,
const Hash& hash = Hash(),
35 const Equal& equal = Equal());
37 void Put(
const T& key, U value);
39 template <
typename Validator>
40 std::optional<U> Get(
const T& key, Validator validator);
42 std::optional<U> Get(
const T& key) {
43 return Get(key, [](
const U&) {
return true; });
46 U GetOr(
const T& key,
const U& default_value);
50 void InvalidateByKey(
const T& key);
53 template <
typename Function>
56 size_t GetSize()
const;
71 Way(Way&& other)
noexcept : cache(std::move(other.cache)) {}
74 Way(
const Hash& hash,
const Equal& equal) : cache(1, hash, equal) {}
76 mutable engine::Mutex mutex;
77 LruMap<T, U, Hash, Equal> cache;
80 Way& GetWay(
const T& key);
84 std::vector<Way> caches_;
86 std::shared_ptr<
dump::Dumper> dumper_{
nullptr};
89template <
typename T,
typename U,
typename Hash,
typename Eq>
90NWayLRU<T, U, Hash, Eq>::
NWayLRU(size_t ways, size_t way_size,
const Hash& hash,
93 caches_.reserve(ways);
94 for (size_t i = 0; i < ways; ++i) caches_.emplace_back(hash, equal);
95 if (ways == 0)
throw std::logic_error(
"Ways must be positive");
97 for (
auto& way : caches_) way.cache.SetMaxSize(way_size);
100template <
typename T,
typename U,
typename Hash,
typename Eq>
101void NWayLRU<T, U, Hash, Eq>::Put(
const T& key, U value) {
102 auto& way = GetWay(key);
104 std::unique_lock<engine::Mutex> lock(way.mutex);
105 way.cache.Put(key, std::move(value));
110template <
typename T,
typename U,
typename Hash,
typename Eq>
111template <
typename Validator>
112std::optional<U> NWayLRU<T, U, Hash, Eq>::Get(
const T& key,
113 Validator validator) {
114 auto& way = GetWay(key);
115 std::unique_lock<engine::Mutex> lock(way.mutex);
116 auto* value = way.cache.Get(key);
119 if (validator(*value))
return *value;
120 way.cache.Erase(key);
126template <
typename T,
typename U,
typename Hash,
typename Eq>
127void NWayLRU<T, U, Hash, Eq>::InvalidateByKey(
const T& key) {
128 auto& way = GetWay(key);
130 std::unique_lock<engine::Mutex> lock(way.mutex);
131 way.cache.Erase(key);
136template <
typename T,
typename U,
typename Hash,
typename Eq>
137U NWayLRU<T, U, Hash, Eq>::GetOr(
const T& key,
const U& default_value) {
138 auto& way = GetWay(key);
139 std::unique_lock<engine::Mutex> lock(way.mutex);
140 return way.cache.GetOr(key, default_value);
143template <
typename T,
typename U,
typename Hash,
typename Eq>
144void NWayLRU<T, U, Hash, Eq>::Invalidate() {
145 for (
auto& way : caches_) {
146 std::unique_lock<engine::Mutex> lock(way.mutex);
152template <
typename T,
typename U,
typename Hash,
typename Eq>
153template <
typename Function>
154void NWayLRU<T, U, Hash, Eq>::
VisitAll(Function func)
const {
155 for (
const auto& way : caches_) {
156 std::unique_lock<engine::Mutex> lock(way.mutex);
157 way.cache.VisitAll(func);
161template <
typename T,
typename U,
typename Hash,
typename Eq>
162size_t NWayLRU<T, U, Hash, Eq>::GetSize()
const {
164 for (
const auto& way : caches_) {
165 std::unique_lock<engine::Mutex> lock(way.mutex);
166 size += way.cache.GetSize();
171template <
typename T,
typename U,
typename Hash,
typename Eq>
172void NWayLRU<T, U, Hash, Eq>::
UpdateWaySize(size_t way_size) {
173 for (
auto& way : caches_) {
174 std::unique_lock<engine::Mutex> lock(way.mutex);
175 way.cache.SetMaxSize(way_size);
179template <
typename T,
typename U,
typename Hash,
typename Eq>
180typename NWayLRU<T, U, Hash, Eq>::Way& NWayLRU<T, U, Hash, Eq>::GetWay(
186 auto seed = hash_fn_(key);
187 boost::hash_combine(seed, 0);
188 auto n = seed % caches_.size();
192template <
typename T,
typename U,
typename Hash,
typename Equal>
193void NWayLRU<T, U, Hash, Equal>::Write(
dump::
Writer& writer)
const {
194 writer.Write(caches_.size());
196 for (
const Way& way : caches_) {
197 std::unique_lock<engine::Mutex> lock(way.mutex);
199 writer.Write(way.cache.GetSize());
201 way.cache.VisitAll([&writer](
const T& key,
const U& value) {
208template <
typename T,
typename U,
typename Hash,
typename Equal>
209void NWayLRU<T, U, Hash, Equal>::Read(
dump::
Reader& reader) {
212 const auto ways = reader.Read<std::size_t>();
213 for (std::size_t i = 0; i < ways; ++i) {
214 const auto elements_in_way = reader.Read<std::size_t>();
215 for (std::size_t j = 0; j < elements_in_way; ++j) {
216 auto key = reader.Read<T>();
217 auto value = reader.Read<U>();
218 Put(std::move(key), std::move(value));
223template <
typename T,
typename U,
typename Hash,
typename Equal>
224void NWayLRU<T, U, Hash, Equal>::NotifyDumper() {
225 if (dumper_ !=
nullptr) {
226 dumper_->OnUpdateCompleted();
230template <
typename T,
typename U,
typename Hash,
typename Equal>
231void NWayLRU<T, U, Hash, Equal>::
SetDumper(
232 std::shared_ptr<
dump::Dumper> dumper) {
233 dumper_ = std::move(dumper);