#include <userver/cache/nway_lru_cache.hpp>
N-way LRU cache with per-way mutexes and optional dump support.
Definition at line 23 of file nway_lru_cache.hpp.
Public Member Functions | |
| NWayLRU (size_t ways, size_t way_size, const Hash &hash=Hash(), const Equal &equal=Equal()) | |
| Constructs an N-way LRU cache with the given number of ways and maximum elements per way. | |
| void | Put (const T &key, U value) |
| Stores value by key. | |
| template<typename Validator> | |
| std::optional< U > | Get (const T &key, Validator validator) |
| Returns cached value by key if validator returns true for it. | |
| std::optional< U > | Get (const T &key) |
| Returns cached value by key if present; otherwise std::nullopt. | |
| U | GetOr (const T &key, const U &default_value) |
| Returns cached value by key if present; otherwise returns default_value. | |
| void | Invalidate () |
| Removes all entries from the cache. | |
| void | InvalidateByKey (const T &key) |
| Removes the entry for the given key if present. | |
| template<typename Function> | |
| void | VisitAll (Function func) const |
Iterates over all items, invoking func for each (key, value). | |
| size_t | GetSize () const |
| Returns the total number of elements in the cache across all ways. | |
| void | UpdateWaySize (size_t way_size) |
| Updates maximum elements per way. | |
| void | Write (dump::Writer &writer) const |
| Serializes the cache contents to the dump writer. | |
| void | Read (dump::Reader &reader) |
| Deserializes the cache contents from the dump reader. | |
| void | SetDumper (std::shared_ptr< dump::Dumper > dumper) |
| Sets the dumper; it will be notified of any cache updates. | |
| cache::NWayLRU< T, U, Hash, Eq >::NWayLRU | ( | size_t | ways, |
| size_t | way_size, | ||
| const Hash & | hash = Hash(), | ||
| const Equal & | equal = Equal() ) |
Constructs an N-way LRU cache with the given number of ways and maximum elements per way.
The maximum total number of elements is ways * way_size.
| ways | Number of ways (a.k.a. shards, internal hash-maps), into which elements are distributed based on their hash. Each shard is protected by an individual mutex. Larger ways means more internal hash-map instances and more memory usage, but less contention. A good starting point is ways=16. If you encounter contention, you can increase ways to something on the order of 256 or whatever your RAM constraints allow. |
| way_size | Maximum allowed amount of elements per way. When the size of a way reaches this number, existing elements are deleted according to the LRU policy. |
| hash | Instance of Hash function to use, in case of a custom stateful Hash. |
| equal | Instance of Equal function to use, in case of a custom stateful Equal. |
| std::logic_error | if ways is zero. |
Definition at line 136 of file nway_lru_cache.hpp.
|
inline |
Returns cached value by key if present; otherwise std::nullopt.
| key | Key to look up. |
Definition at line 66 of file nway_lru_cache.hpp.
| std::optional< U > cache::NWayLRU< T, U, Hash, Eq >::Get | ( | const T & | key, |
| Validator | validator ) |
Returns cached value by key if validator returns true for it.
| key | Key to look up. |
| validator | Callable that takes the cached value and returns false to invalidate the entry. |
Definition at line 165 of file nway_lru_cache.hpp.
| U cache::NWayLRU< T, U, Hash, Eq >::GetOr | ( | const T & | key, |
| const U & | default_value ) |
Returns cached value by key if present; otherwise returns default_value.
| key | Key to look up. |
| default_value | Value to return when key is not found. |
default_value. Definition at line 191 of file nway_lru_cache.hpp.
| size_t cache::NWayLRU< T, U, Hash, Eq >::GetSize | ( | ) | const |
Returns the total number of elements in the cache across all ways.
Definition at line 216 of file nway_lru_cache.hpp.
| void cache::NWayLRU< T, U, Hash, Eq >::Invalidate | ( | ) |
Removes all entries from the cache.
Definition at line 198 of file nway_lru_cache.hpp.
| void cache::NWayLRU< T, U, Hash, Eq >::InvalidateByKey | ( | const T & | key | ) |
Removes the entry for the given key if present.
| key | Key whose entry is to be removed. |
Definition at line 181 of file nway_lru_cache.hpp.
| void cache::NWayLRU< T, U, Hash, Eq >::Put | ( | const T & | key, |
| U | value ) |
Stores value by key.
| key | Key to store the value under. |
| value | Value to store. |
Definition at line 154 of file nway_lru_cache.hpp.
| void cache::NWayLRU< T, U, Hash, Equal >::Read | ( | dump::Reader & | reader | ) |
Deserializes the cache contents from the dump reader.
| reader | Source dump::Reader. |
Definition at line 262 of file nway_lru_cache.hpp.
| void cache::NWayLRU< T, U, Hash, Equal >::SetDumper | ( | std::shared_ptr< dump::Dumper > | dumper | ) |
Sets the dumper; it will be notified of any cache updates.
| dumper | Instance of dump::Dumper, or nullptr to clear. |
Definition at line 284 of file nway_lru_cache.hpp.
| void cache::NWayLRU< T, U, Hash, Eq >::UpdateWaySize | ( | size_t | way_size | ) |
Updates maximum elements per way.
| way_size | New maximum elements per way; see NWayLRU constructor for the semantics. |
Definition at line 226 of file nway_lru_cache.hpp.
| void cache::NWayLRU< T, U, Hash, Eq >::VisitAll | ( | Function | func | ) | const |
Iterates over all items, invoking func for each (key, value).
| func | Callable to invoke for each item (e.g. void(const T&, const U&)). |
Definition at line 208 of file nway_lru_cache.hpp.
| void cache::NWayLRU< T, U, Hash, Equal >::Write | ( | dump::Writer & | writer | ) | const |
Serializes the cache contents to the dump writer.
| writer | Target dump::Writer. |
Definition at line 246 of file nway_lru_cache.hpp.