Class for expirable LRU cache. Use cache::LruMap for not expirable LRU Cache. More...
#include <userver/cache/expirable_lru_cache.hpp>
Public Types | |
enum class | ReadMode { kSkipCache , kUseCache } |
Cache read mode. More... | |
using | UpdateValueFunc = std::function< Value(const Key &)> |
Public Member Functions | |
ExpirableLruCache (size_t ways, size_t way_size, const Hash &hash=Hash(), const Equal &equal=Equal()) | |
void | SetWaySize (size_t way_size) |
std::chrono::milliseconds | GetMaxLifetime () const noexcept |
void | SetMaxLifetime (std::chrono::milliseconds max_lifetime) |
void | SetBackgroundUpdate (BackgroundUpdateMode background_update) |
Value | Get (const Key &key, const UpdateValueFunc &update_func, ReadMode read_mode=ReadMode::kUseCache) |
std::optional< Value > | GetOptional (const Key &key, const UpdateValueFunc &update_func) |
std::optional< Value > | GetOptionalUnexpirable (const Key &key) |
std::optional< Value > | GetOptionalUnexpirableWithUpdate (const Key &key, const UpdateValueFunc &update_func) |
std::optional< Value > | GetOptionalNoUpdate (const Key &key) |
void | Put (const Key &key, const Value &value) |
void | Put (const Key &key, Value &&value) |
const impl::ExpirableLruCacheStatistics & | GetStatistics () const |
size_t | GetSizeApproximate () const |
void | Invalidate () |
Clear cache. | |
void | InvalidateByKey (const Key &key) |
Erase key from cache. | |
void | UpdateInBackground (const Key &key, UpdateValueFunc update_func) |
Add async task for updating value by update_func(key) | |
void | Write (dump::Writer &writer) const |
void | Read (dump::Reader &reader) |
void | SetDumper (std::shared_ptr< dump::Dumper > dumper) |
Class for expirable LRU cache. Use cache::LruMap for not expirable LRU Cache.
Example usage:
Definition at line 61 of file expirable_lru_cache.hpp.
using cache::ExpirableLruCache< Key, Value, Hash, Equal >::UpdateValueFunc = std::function<Value(const Key&)> |
Definition at line 63 of file expirable_lru_cache.hpp.
|
strong |
Cache read mode.
Enumerator | |
---|---|
kSkipCache | Do not cache value got from update function. |
kUseCache | Cache value got from update function. |
Definition at line 66 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() |
||
) |
Definition at line 169 of file expirable_lru_cache.hpp.
cache::ExpirableLruCache< Key, Value, Hash, Equal >::~ExpirableLruCache | ( | ) |
Definition at line 175 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 |
||
) |
Definition at line 203 of file expirable_lru_cache.hpp.
|
noexcept |
Definition at line 186 of file expirable_lru_cache.hpp.
std::optional< Value > cache::ExpirableLruCache< Key, Value, Hash, Equal >::GetOptional | ( | const Key & | key, |
const UpdateValueFunc & | update_func | ||
) |
Update value in cache by "update_func" if background update mode is kEnabled and "key" is in cache and not expired but its lifetime ends soon.
Definition at line 228 of file expirable_lru_cache.hpp.
std::optional< Value > cache::ExpirableLruCache< Key, Value, Hash, Equal >::GetOptionalNoUpdate | ( | const Key & | key | ) |
GetOptional, but without value updates.
Definition at line 289 of file expirable_lru_cache.hpp.
std::optional< Value > cache::ExpirableLruCache< Key, Value, Hash, Equal >::GetOptionalUnexpirable | ( | const Key & | key | ) |
GetOptional, but without expiry checks and value updates.
Used during fallback in FallbackELruCache.
Definition at line 253 of file expirable_lru_cache.hpp.
std::optional< Value > cache::ExpirableLruCache< Key, Value, Hash, Equal >::GetOptionalUnexpirableWithUpdate | ( | const Key & | key, |
const UpdateValueFunc & | update_func | ||
) |
GetOptional, but without expiry check.
Used during fallback in FallbackELruCache.
Definition at line 268 of file expirable_lru_cache.hpp.
size_t cache::ExpirableLruCache< Key, Value, Hash, Equal >::GetSizeApproximate | ( | ) | const |
Definition at line 327 of file expirable_lru_cache.hpp.
const impl::ExpirableLruCacheStatistics & cache::ExpirableLruCache< Key, Value, Hash, Equal >::GetStatistics | ( | ) | const |
Definition at line 322 of file expirable_lru_cache.hpp.
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::Invalidate | ( | ) |
Clear cache.
Definition at line 332 of file expirable_lru_cache.hpp.
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::InvalidateByKey | ( | const Key & | key | ) |
Erase key from cache.
Definition at line 337 of file expirable_lru_cache.hpp.
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::Put | ( | const Key & | key, |
const Value & | value | ||
) |
Definition at line 309 of file expirable_lru_cache.hpp.
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::Put | ( | const Key & | key, |
Value && | value | ||
) |
Definition at line 315 of file expirable_lru_cache.hpp.
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::Read | ( | dump::Reader & | reader | ) |
Definition at line 425 of file expirable_lru_cache.hpp.
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::SetBackgroundUpdate | ( | BackgroundUpdateMode | background_update | ) |
Sets background update mode. If "background_update" mode is kDisabled, expiring values are not updated in background (asynchronously) or are updated if "background_update" is kEnabled.
Definition at line 197 of file expirable_lru_cache.hpp.
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::SetDumper | ( | std::shared_ptr< dump::Dumper > | dumper | ) |
The dump::Dumper will be notified of any cache updates. This method is not thread-safe.
Definition at line 431 of file expirable_lru_cache.hpp.
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::SetMaxLifetime | ( | std::chrono::milliseconds | max_lifetime | ) |
Definition at line 191 of file expirable_lru_cache.hpp.
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::SetWaySize | ( | size_t | way_size | ) |
Definition at line 180 of file expirable_lru_cache.hpp.
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::UpdateInBackground | ( | const Key & | key, |
UpdateValueFunc | update_func | ||
) |
Add async task for updating value by update_func(key)
Definition at line 343 of file expirable_lru_cache.hpp.
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::Write | ( | dump::Writer & | writer | ) | const |
Definition at line 418 of file expirable_lru_cache.hpp.