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