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