#include <userver/cache/expirable_lru_cache.hpp>
Class for expirable LRU cache. Use cache::LruMap for not expirable LRU Cache.
Example usage:
Definition at line 58 of file 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) |
using cache::ExpirableLruCache< Key, Value, Hash, Equal >::UpdateValueFunc = std::function<Value(const Key&)> |
Definition at line 60 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 63 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() ) |
For the description of ways
and way_size
, see the cache::NWayLRU::NWayLRU constructor.
Definition at line 163 of file expirable_lru_cache.hpp.
cache::ExpirableLruCache< Key, Value, Hash, Equal >::~ExpirableLruCache | ( | ) |
Definition at line 172 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 197 of file expirable_lru_cache.hpp.
|
noexcept |
Definition at line 182 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 226 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 284 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 249 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 262 of file expirable_lru_cache.hpp.
size_t cache::ExpirableLruCache< Key, Value, Hash, Equal >::GetSizeApproximate | ( | ) | const |
Definition at line 318 of file expirable_lru_cache.hpp.
const impl::ExpirableLruCacheStatistics & cache::ExpirableLruCache< Key, Value, Hash, Equal >::GetStatistics | ( | ) | const |
Definition at line 313 of file expirable_lru_cache.hpp.
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::Invalidate | ( | ) |
Clear cache.
Definition at line 323 of file expirable_lru_cache.hpp.
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::InvalidateByKey | ( | const Key & | key | ) |
Erase key from cache.
Definition at line 328 of file expirable_lru_cache.hpp.
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::Put | ( | const Key & | key, |
const Value & | value ) |
Definition at line 303 of file expirable_lru_cache.hpp.
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::Put | ( | const Key & | key, |
Value && | value ) |
Definition at line 308 of file expirable_lru_cache.hpp.
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::Read | ( | dump::Reader & | reader | ) |
Definition at line 408 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 192 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 414 of file expirable_lru_cache.hpp.
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::SetMaxLifetime | ( | std::chrono::milliseconds | max_lifetime | ) |
Definition at line 187 of file expirable_lru_cache.hpp.
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::SetWaySize | ( | size_t | way_size | ) |
For the description of way_size
, see the cache::NWayLRU::NWayLRU constructor.
Definition at line 177 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 333 of file expirable_lru_cache.hpp.
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::Write | ( | dump::Writer & | writer | ) | const |
Definition at line 402 of file expirable_lru_cache.hpp.