7#include <boost/container_hash/hash.hpp>
9#include <userver/cache/lru_map.hpp>
10#include <userver/dump/dumper.hpp>
11#include <userver/dump/operations.hpp>
12#include <userver/engine/mutex.hpp>
14USERVER_NAMESPACE_BEGIN
19template <
typename T,
typename U,
typename Hash = std::hash<T>,
typename Equal = std::equal_to<T>>
38 NWayLRU(size_t ways, size_t way_size,
const Hash& hash = Hash(),
const Equal& equal = Equal());
40 void Put(
const T& key, U value);
42 template <
typename Validator>
43 std::optional<U> Get(
const T& key, Validator validator);
45 std::optional<U> Get(
const T& key) {
46 return Get(key, [](
const U&) {
return true; });
49 U GetOr(
const T& key,
const U& default_value);
53 void InvalidateByKey(
const T& key);
56 template <
typename Function>
59 size_t GetSize()
const;
74 Way(Way&& other)
noexcept : cache(std::move(other.cache)) {}
77 Way(
const Hash& hash,
const Equal& equal) : cache(1, hash, equal) {}
79 mutable engine::Mutex mutex;
80 LruMap<T, U, Hash, Equal> cache;
83 Way& GetWay(
const T& key);
87 std::vector<Way> caches_;
89 std::shared_ptr<
dump::Dumper> dumper_{
nullptr};
20class NWayLRU final {
…};
92template <
typename T,
typename U,
typename Hash,
typename Eq>
93NWayLRU<T, U, Hash, Eq>::
NWayLRU(size_t ways, size_t way_size,
const Hash& hash,
const Eq& equal)
94 : caches_(), hash_fn_(hash) {
95 caches_.reserve(ways);
96 for (size_t i = 0; i < ways; ++i) caches_.emplace_back(hash, equal);
97 if (ways == 0)
throw std::logic_error(
"Ways must be positive");
99 for (
auto& way : caches_) way.cache.SetMaxSize(way_size);
93NWayLRU<T, U, Hash, Eq>::
NWayLRU(size_t ways, size_t way_size,
const Hash& hash,
const Eq& equal) {
…}
102template <
typename T,
typename U,
typename Hash,
typename Eq>
103void NWayLRU<T, U, Hash, Eq>::Put(
const T& key, U value) {
104 auto& way = GetWay(key);
106 std::unique_lock<engine::Mutex> lock(way.mutex);
107 way.cache.Put(key, std::move(value));
112template <
typename T,
typename U,
typename Hash,
typename Eq>
113template <
typename Validator>
114std::optional<U> NWayLRU<T, U, Hash, Eq>::Get(
const T& key, Validator validator) {
115 auto& way = GetWay(key);
116 std::unique_lock<engine::Mutex> lock(way.mutex);
117 auto* value = way.cache.Get(key);
120 if (validator(*value))
return *value;
121 way.cache.Erase(key);
127template <
typename T,
typename U,
typename Hash,
typename Eq>
128void NWayLRU<T, U, Hash, Eq>::InvalidateByKey(
const T& key) {
129 auto& way = GetWay(key);
131 std::unique_lock<engine::Mutex> lock(way.mutex);
132 way.cache.Erase(key);
137template <
typename T,
typename U,
typename Hash,
typename Eq>
138U NWayLRU<T, U, Hash, Eq>::GetOr(
const T& key,
const U& default_value) {
139 auto& way = GetWay(key);
140 std::unique_lock<engine::Mutex> lock(way.mutex);
141 return way.cache.GetOr(key, default_value);
144template <
typename T,
typename U,
typename Hash,
typename Eq>
145void NWayLRU<T, U, Hash, Eq>::Invalidate() {
146 for (
auto& way : caches_) {
147 std::unique_lock<engine::Mutex> lock(way.mutex);
153template <
typename T,
typename U,
typename Hash,
typename Eq>
154template <
typename Function>
155void NWayLRU<T, U, Hash, Eq>::
VisitAll(Function func)
const {
156 for (
const auto& way : caches_) {
157 std::unique_lock<engine::Mutex> lock(way.mutex);
158 way.cache.VisitAll(func);
155void NWayLRU<T, U, Hash, Eq>::
VisitAll(Function func)
const {
…}
162template <
typename T,
typename U,
typename Hash,
typename Eq>
163size_t NWayLRU<T, U, Hash, Eq>::GetSize()
const {
165 for (
const auto& way : caches_) {
166 std::unique_lock<engine::Mutex> lock(way.mutex);
167 size += way.cache.GetSize();
172template <
typename T,
typename U,
typename Hash,
typename Eq>
174 for (
auto& way : caches_) {
175 std::unique_lock<engine::Mutex> lock(way.mutex);
176 way.cache.SetMaxSize(way_size);
180template <
typename T,
typename U,
typename Hash,
typename Eq>
181typename NWayLRU<T, U, Hash, Eq>::Way& NWayLRU<T, U, Hash, Eq>::GetWay(
const T& key) {
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) {
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) {
230template <
typename T,
typename U,
typename Hash,
typename Equal>
231void NWayLRU<T, U, Hash, Equal>::
SetDumper(std::shared_ptr<
dump::Dumper> dumper) {
232 dumper_ = std::move(dumper);
231void NWayLRU<T, U, Hash, Equal>::
SetDumper(std::shared_ptr<
dump::Dumper> dumper) {
…}