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
3/// @file userver/cache/lru_map.hpp
4/// @brief @copybrief cache::LruMap
5
6#include <userver/cache/impl/lru.hpp>
7
8USERVER_NAMESPACE_BEGIN
9
10/// Utilities for caching
11namespace cache {
12
13/// @ingroup userver_universal userver_containers
14///
15/// LRU key value storage (LRU cache), thread safety matches Standard Library
16/// thread safety
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
33 /// Adds or rewrites key/value, updates its usage
34 /// @returns true if key is a new one
35 bool Put(const T& key, U value) { return impl_.Put(key, std::move(value)); }
36
37 /// Returns pointer to value if the key is in LRU and updates its usage;
38 /// constructs and adds a new key/value otherwise.
39 /// @warning Returned pointer may be freed on the next map access!
40 template <typename... Args>
41 U* Emplace(const T& key, Args&&... args) {
42 return impl_.Emplace(key, std::forward<Args>(args)...);
43 }
44
45 /// Removes key from LRU
46 void Erase(const T& key) { impl_.Erase(key); }
47
48 /// Returns pointer to value if the key is in LRU and updates its usage;
49 /// returns nullptr otherwise.
50 /// @warning Returned pointer may be freed on the next map access!
51 U* Get(const T& key) { return impl_.Get(key); }
52
53 /// Returns pointer to value if the key is in LRU and updates its usage;
54 /// returns nullptr otherwise.
55 /// @warning Returned pointer may be freed on the next map access!
56 template <class Key>
57 U* GetTransparent(const Key& key) {
58 return impl_.GetTransparent(key);
59 }
60
61 /// Returns value by key and updates its usage; returns default_value
62 /// otherwise without modifying the cache.
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
71 /// Returns pointer to the least recently used value;
72 /// returns nullptr if LRU is empty.
73 /// @warning Returned pointer may be freed on the next map access!
74 U* GetLeastUsed() { return impl_.GetLeastUsedValue(); }
75
76 /// Sets the max size of the LRU, truncates values if new_max_size < GetSize()
77 void SetMaxSize(size_t new_max_size) { return impl_.SetMaxSize(new_max_size); }
78
79 /// Removes all the elements
80 void Clear() { return impl_.Clear(); }
81
82 /// Call Function(const T&, const U&) for all items
83 template <typename Function>
84 void VisitAll(Function&& func) const {
85 impl_.VisitAll(std::forward<Function>(func));
86 }
87
88 /// Call Function(const T&, U&) for all items
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