userver: userver/cache/lru_map.hpp Source File
Loading...
Searching...
No Matches
lru_map.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <userver/cache/impl/lru.hpp>
7
8USERVER_NAMESPACE_BEGIN
9
11namespace cache {
12
17template <
18 typename T,
19 typename U,
20 typename Hash = std::hash<impl::StringToStringView<T>>,
21 typename Equal = std::equal_to<impl::StringToStringView<T>>>
22class LruMap final {
23public:
24 explicit LruMap(size_t max_size, const Hash& hash = Hash(), const Equal& equal = Equal())
25 : impl_(max_size, hash, equal)
26 {}
27
28 LruMap(LruMap&& lru) noexcept = default;
29 LruMap(const LruMap& lru) = delete;
30 LruMap& operator=(LruMap&& lru) noexcept = default;
31 LruMap& operator=(const LruMap& lru) = delete;
32
35 bool Put(const T& key, U value) { return impl_.Put(key, std::move(value)); }
36
40 template <typename... Args>
41 U* Emplace(const T& key, Args&&... args) {
42 return impl_.Emplace(key, std::forward<Args>(args)...);
43 }
44
46 void Erase(const T& key) { impl_.Erase(key); }
47
51 U* Get(const T& key) { return impl_.Get(key); }
52
56 template <class Key>
57 U* GetTransparent(const Key& key) {
58 return impl_.GetTransparent(key);
59 }
60
63 U GetOr(const T& key, const U& default_value) {
64 auto* ptr = impl_.Get(key);
65 if (ptr) {
66 return *ptr;
67 }
68 return default_value;
69 }
70
74 U* GetLeastUsed() { return impl_.GetLeastUsedValue(); }
75
77 void SetMaxSize(size_t new_max_size) { return impl_.SetMaxSize(new_max_size); }
78
80 void Clear() { return impl_.Clear(); }
81
83 template <typename Function>
84 void VisitAll(Function&& func) const {
85 impl_.VisitAll(std::forward<Function>(func));
86 }
87
89 template <typename Function>
90 void VisitAll(Function&& func) {
91 impl_.VisitAll(std::forward<Function>(func));
92 }
93
94 size_t GetSize() const { return impl_.GetSize(); }
95
96 std::size_t GetCapacity() const { return impl_.GetCapacity(); }
97
98private:
99 impl::LruBase<T, U, Hash, Equal> impl_;
100};
101
102} // namespace cache
103
104USERVER_NAMESPACE_END