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 LruMap(LruMap&& lru) noexcept = default;
28 LruMap(const LruMap& lru) = delete;
29 LruMap& operator=(LruMap&& lru) noexcept = default;
30 LruMap& operator=(const LruMap& lru) = delete;
31
32 /// Adds or rewrites key/value, updates its usage
33 /// @returns true if key is a new one
34 bool Put(const T& key, U value) { return impl_.Put(key, std::move(value)); }
35
36 /// Returns pointer to value if the key is in LRU and updates its usage;
37 /// constructs and adds a new key/value otherwise.
38 /// @warning Returned pointer may be freed on the next map access!
39 template <typename... Args>
40 U* Emplace(const T& key, Args&&... args) {
41 return impl_.Emplace(key, std::forward<Args>(args)...);
42 }
43
44 /// Removes key from LRU
45 void Erase(const T& key) { impl_.Erase(key); }
46
47 /// Returns pointer to value if the key is in LRU and updates its usage;
48 /// returns nullptr otherwise.
49 /// @warning Returned pointer may be freed on the next map access!
50 U* Get(const T& key) { return impl_.Get(key); }
51
52 /// Returns pointer to value if the key is in LRU and updates its usage;
53 /// returns nullptr otherwise.
54 /// @warning Returned pointer may be freed on the next map access!
55 template <class Key>
56 U* GetTransparent(const Key& key) {
57 return impl_.GetTransparent(key);
58 }
59
60 /// Returns value by key and updates its usage; returns default_value
61 /// otherwise without modifying the cache.
62 U GetOr(const T& key, const U& default_value) {
63 auto* ptr = impl_.Get(key);
64 if (ptr) return *ptr;
65 return default_value;
66 }
67
68 /// Returns pointer to the least recently used value;
69 /// returns nullptr if LRU is empty.
70 /// @warning Returned pointer may be freed on the next map access!
71 U* GetLeastUsed() { return impl_.GetLeastUsedValue(); }
72
73 /// Sets the max size of the LRU, truncates values if new_max_size < GetSize()
74 void SetMaxSize(size_t new_max_size) { return impl_.SetMaxSize(new_max_size); }
75
76 /// Removes all the elements
77 void Clear() { return impl_.Clear(); }
78
79 /// Call Function(const T&, const U&) for all items
80 template <typename Function>
81 void VisitAll(Function&& func) const {
82 impl_.VisitAll(std::forward<Function>(func));
83 }
84
85 /// Call Function(const T&, U&) for all items
86 template <typename Function>
87 void VisitAll(Function&& func) {
88 impl_.VisitAll(std::forward<Function>(func));
89 }
90
91 size_t GetSize() const { return impl_.GetSize(); }
92
93 std::size_t GetCapacity() const { return impl_.GetCapacity(); }
94
95private:
96 impl::LruBase<T, U, Hash, Equal> impl_;
97};
98
99} // namespace cache
100
101USERVER_NAMESPACE_END