#include <userver/cache/expirable_lru_cache.hpp>
Class for expirable LRU cache.
Use cache::LruMap for non-expirable LRU cache.
Definition at line 58 of file expirable_lru_cache.hpp.
Public Types | |
| enum class | ReadMode { kSkipCache , kUseCache } |
| Cache read mode for Get. More... | |
| using | UpdateValueFunc = std::function<Value(const Key&)> |
| Type of function used to compute or refresh a value for a key. | |
Public Member Functions | |
| ExpirableLruCache (size_t ways, size_t way_size, const Hash &hash=Hash(), const Equal &equal=Equal()) | |
| Constructs the cache. | |
| ~ExpirableLruCache () | |
| Destroys the cache and waits for all background update tasks to complete. | |
| void | SetWaySize (size_t way_size) |
| Sets maximum size per way. | |
| std::chrono::milliseconds | GetMaxLifetime () const noexcept |
| Returns the maximum lifetime of a cached value before it is considered expired. | |
| void | SetMaxLifetime (std::chrono::milliseconds max_lifetime) |
| Sets the maximum lifetime of a cached value before it is considered expired. | |
| void | SetBackgroundUpdate (BackgroundUpdateMode background_update) |
| Sets background update mode. | |
| Value | Get (const Key &key, const UpdateValueFunc &update_func, ReadMode read_mode=ReadMode::kUseCache) |
| Returns value by key, or computes and optionally caches it. | |
| std::optional< Value > | GetOptional (const Key &key, const UpdateValueFunc &update_func) |
| Returns value by key if present and not expired; may trigger background update. | |
| std::optional< Value > | GetOptionalUnexpirable (const Key &key) |
| GetOptional without expiry checks and without value updates. | |
| std::optional< Value > | GetOptionalUnexpirableWithUpdate (const Key &key, const UpdateValueFunc &update_func) |
| GetOptional without expiry check; may trigger background update. | |
| std::optional< Value > | GetOptionalNoUpdate (const Key &key) |
| GetOptional without triggering value updates (no background refresh). | |
| std::optional< impl::ExpirableValue< Value > > | GetOptionalNoUpdateWithLastUpdateTime (const Key &key) |
| GetOptionalNoUpdate, but returns the value together with its last update time. | |
| void | Put (const Key &key, const Value &value) |
| Inserts or updates the value for the given key with current timestamp. | |
| void | Put (const Key &key, Value &&value) |
Inserts or updates the value for the given key with current timestamp (value is moved). | |
| const impl::ExpirableLruCacheStatistics & | GetStatistics () const |
| Returns cache statistics (hits, misses, background updates, etc.). | |
| size_t | GetSizeApproximate () const |
| Returns approximate number of entries in the cache. | |
| void | Invalidate () |
| Clears the cache (removes all entries). | |
| void | InvalidateByKey (const Key &key) |
| Erases the entry for the given key. | |
| template<typename Predicate> | |
| void | InvalidateByKeyIf (const Key &key, Predicate pred) |
Erases the key from cache if pred returns true for the current value. | |
| void | UpdateInBackground (const Key &key, UpdateValueFunc update_func) |
Schedules an async task to update the value by update_func(key). | |
| void | Write (dump::Writer &writer) const |
| Serializes cache state to the dump writer. | |
| void | Read (dump::Reader &reader) |
| Restores cache state from the dump reader. | |
| void | SetDumper (std::shared_ptr< dump::Dumper > dumper) |
| Sets the dumper that will be notified of cache updates. | |
| using cache::ExpirableLruCache< Key, Value, Hash, Equal >::UpdateValueFunc = std::function<Value(const Key&)> |
Type of function used to compute or refresh a value for a key.
Definition at line 61 of file expirable_lru_cache.hpp.
|
strong |
Cache read mode for Get.
| Enumerator | |
|---|---|
| kSkipCache | Do not cache value got from update function. |
| kUseCache | Cache value got from update function. |
Definition at line 64 of file expirable_lru_cache.hpp.
| cache::ExpirableLruCache< Key, Value, Hash, Equal >::ExpirableLruCache | ( | size_t | ways, |
| size_t | way_size, | ||
| const Hash & | hash = Hash(), | ||
| const Equal & | equal = Equal() ) |
Constructs the cache.
| ways | Number of ways (shards). See cache::NWayLRU. |
| way_size | Maximum size per way. See cache::NWayLRU. |
| hash | Hash functor for keys. |
| equal | Equality functor for keys. |
Definition at line 249 of file expirable_lru_cache.hpp.
| cache::ExpirableLruCache< Key, Value, Hash, Equal >::~ExpirableLruCache | ( | ) |
Destroys the cache and waits for all background update tasks to complete.
Definition at line 255 of file expirable_lru_cache.hpp.
| Value cache::ExpirableLruCache< Key, Value, Hash, Equal >::Get | ( | const Key & | key, |
| const UpdateValueFunc & | update_func, | ||
| ReadMode | read_mode = ReadMode::kUseCache ) |
Returns value by key, or computes and optionally caches it.
Equivalent to GetOptional(key, update_func); if that is std::nullopt, returns update_func(key) and stores it in cache when read_mode is ReadMode::kUseCache.
| key | Cache key. |
| update_func | Function to compute value when key is missing or expired. |
| read_mode | If kUseCache, caches the result of update_func. |
Definition at line 284 of file expirable_lru_cache.hpp.
|
noexcept |
Returns the maximum lifetime of a cached value before it is considered expired.
Definition at line 265 of file expirable_lru_cache.hpp.
| std::optional< Value > cache::ExpirableLruCache< Key, Value, Hash, Equal >::GetOptional | ( | const Key & | key, |
| const UpdateValueFunc & | update_func ) |
Returns value by key if present and not expired; may trigger background update.
If background update is kEnabled and the entry is near expiry, schedules an async update via update_func. Does not block on that update.
| key | Cache key. |
| update_func | Function used for background refresh when entry is near expiry. |
Definition at line 312 of file expirable_lru_cache.hpp.
| std::optional< Value > cache::ExpirableLruCache< Key, Value, Hash, Equal >::GetOptionalNoUpdate | ( | const Key & | key | ) |
GetOptional without triggering value updates (no background refresh).
| key | Cache key. |
Definition at line 391 of file expirable_lru_cache.hpp.
| std::optional< impl::ExpirableValue< Value > > cache::ExpirableLruCache< Key, Value, Hash, Equal >::GetOptionalNoUpdateWithLastUpdateTime | ( | const Key & | key | ) |
GetOptionalNoUpdate, but returns the value together with its last update time.
| key | Cache key. |
Definition at line 375 of file expirable_lru_cache.hpp.
| std::optional< Value > cache::ExpirableLruCache< Key, Value, Hash, Equal >::GetOptionalUnexpirable | ( | const Key & | key | ) |
GetOptional without expiry checks and without value updates.
| key | Cache key. |
Definition at line 335 of file expirable_lru_cache.hpp.
| std::optional< Value > cache::ExpirableLruCache< Key, Value, Hash, Equal >::GetOptionalUnexpirableWithUpdate | ( | const Key & | key, |
| const UpdateValueFunc & | update_func ) |
GetOptional without expiry check; may trigger background update.
| key | Cache key. |
| update_func | Function used for background refresh when entry is near expiry. |
Definition at line 352 of file expirable_lru_cache.hpp.
| size_t cache::ExpirableLruCache< Key, Value, Hash, Equal >::GetSizeApproximate | ( | ) | const |
Returns approximate number of entries in the cache.
Definition at line 415 of file expirable_lru_cache.hpp.
| const impl::ExpirableLruCacheStatistics & cache::ExpirableLruCache< Key, Value, Hash, Equal >::GetStatistics | ( | ) | const |
Returns cache statistics (hits, misses, background updates, etc.).
Definition at line 410 of file expirable_lru_cache.hpp.
| void cache::ExpirableLruCache< Key, Value, Hash, Equal >::Invalidate | ( | ) |
Clears the cache (removes all entries).
Definition at line 420 of file expirable_lru_cache.hpp.
| void cache::ExpirableLruCache< Key, Value, Hash, Equal >::InvalidateByKey | ( | const Key & | key | ) |
Erases the entry for the given key.
| key | Cache key to erase. |
Definition at line 425 of file expirable_lru_cache.hpp.
| void cache::ExpirableLruCache< Key, Value, Hash, Equal >::InvalidateByKeyIf | ( | const Key & | key, |
| Predicate | pred ) |
Erases the key from cache if pred returns true for the current value.
| key | Cache key. |
| pred | Predicate invoked with current value; entry is erased only if it returns true and value is not expired. |
Definition at line 431 of file expirable_lru_cache.hpp.
| void cache::ExpirableLruCache< Key, Value, Hash, Equal >::Put | ( | const Key & | key, |
| const Value & | value ) |
Inserts or updates the value for the given key with current timestamp.
| key | Cache key. |
| value | Value to store. |
Definition at line 400 of file expirable_lru_cache.hpp.
| void cache::ExpirableLruCache< Key, Value, Hash, Equal >::Put | ( | const Key & | key, |
| Value && | value ) |
Inserts or updates the value for the given key with current timestamp (value is moved).
| key | Cache key. |
| value | Value to store (moved). |
Definition at line 405 of file expirable_lru_cache.hpp.
| void cache::ExpirableLruCache< Key, Value, Hash, Equal >::Read | ( | dump::Reader & | reader | ) |
Restores cache state from the dump reader.
Used for dump::Dumper integration.
| reader | Dump reader to read from. |
Definition at line 565 of file expirable_lru_cache.hpp.
| void cache::ExpirableLruCache< Key, Value, Hash, Equal >::SetBackgroundUpdate | ( | BackgroundUpdateMode | background_update | ) |
Sets background update mode.
If background_update is kDisabled, expiring values are not updated in background; if kEnabled, they are updated asynchronously when lifetime is near end.
| background_update | Either kDisabled or kEnabled. |
Definition at line 275 of file expirable_lru_cache.hpp.
| void cache::ExpirableLruCache< Key, Value, Hash, Equal >::SetDumper | ( | std::shared_ptr< dump::Dumper > | dumper | ) |
Sets the dumper that will be notified of cache updates.
| dumper | Shared pointer to dump::Dumper. |
Definition at line 571 of file expirable_lru_cache.hpp.
| void cache::ExpirableLruCache< Key, Value, Hash, Equal >::SetMaxLifetime | ( | std::chrono::milliseconds | max_lifetime | ) |
Sets the maximum lifetime of a cached value before it is considered expired.
| max_lifetime | Max lifetime; zero disables expiration. |
Definition at line 270 of file expirable_lru_cache.hpp.
| void cache::ExpirableLruCache< Key, Value, Hash, Equal >::SetWaySize | ( | size_t | way_size | ) |
Sets maximum size per way.
| way_size | Maximum size per way. See cache::NWayLRU. |
Definition at line 260 of file expirable_lru_cache.hpp.
| void cache::ExpirableLruCache< Key, Value, Hash, Equal >::UpdateInBackground | ( | const Key & | key, |
| UpdateValueFunc | update_func ) |
Schedules an async task to update the value by update_func(key).
| key | Cache key to update. |
| update_func | Function to compute the new value. |
Definition at line 444 of file expirable_lru_cache.hpp.
| void cache::ExpirableLruCache< Key, Value, Hash, Equal >::Write | ( | dump::Writer & | writer | ) | const |
Serializes cache state to the dump writer.
Used for dump::Dumper integration.
| writer | Dump writer to write to. |
Definition at line 559 of file expirable_lru_cache.hpp.