userver: cache::ExpirableLruCache< Key, Value, Hash, Equal > Class Template Reference
Loading...
Searching...
No Matches
cache::ExpirableLruCache< Key, Value, Hash, Equal > Class Template Referencefinal

#include <userver/cache/expirable_lru_cache.hpp>

Detailed Description

template<typename Key, typename Value, typename Hash = std::hash<Key>, typename Equal = std::equal_to<Key>>
class cache::ExpirableLruCache< Key, Value, Hash, Equal >

Class for expirable LRU cache. Use cache::LruMap for not expirable LRU Cache.

Example usage:

using Key = std::string;
using Value = int;
Cache cache(/*ways*/ 1, /*way_size*/ 1);
cache.SetMaxLifetime(std::chrono::seconds(3)); // by default not bounded
utils::datetime::MockNowSet(std::chrono::system_clock::now());
Key key1 = "first-key";
Key key2 = "second-key";
cache.Put(key1, 41);
EXPECT_EQ(41, cache.GetOptionalNoUpdate(key1));
cache.Put(key2, 42);
EXPECT_EQ(std::nullopt, cache.GetOptionalNoUpdate(key1));
EXPECT_EQ(42, cache.GetOptionalNoUpdate(key2));
utils::datetime::MockSleep(std::chrono::seconds(2));
EXPECT_EQ(42, cache.GetOptionalNoUpdate(key2));
utils::datetime::MockSleep(std::chrono::seconds(2));
EXPECT_EQ(std::nullopt, cache.GetOptionalNoUpdate(key2));

Definition at line 61 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)
 

Member Typedef Documentation

◆ UpdateValueFunc

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename Equal = std::equal_to<Key>>
using cache::ExpirableLruCache< Key, Value, Hash, Equal >::UpdateValueFunc = std::function<Value(const Key&)>

Definition at line 63 of file expirable_lru_cache.hpp.

Member Enumeration Documentation

◆ ReadMode

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename Equal = std::equal_to<Key>>
enum class cache::ExpirableLruCache::ReadMode
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.

Constructor & Destructor Documentation

◆ ExpirableLruCache()

template<typename Key , typename Value , typename Hash , typename Equal >
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 173 of file expirable_lru_cache.hpp.

◆ ~ExpirableLruCache()

template<typename Key , typename Value , typename Hash , typename Equal >
cache::ExpirableLruCache< Key, Value, Hash, Equal >::~ExpirableLruCache ( )

Definition at line 179 of file expirable_lru_cache.hpp.

Member Function Documentation

◆ Get()

template<typename Key , typename Value , typename Hash , typename Equal >
Value cache::ExpirableLruCache< Key, Value, Hash, Equal >::Get ( const Key & key,
const UpdateValueFunc & update_func,
ReadMode read_mode = ReadMode::kUseCache )
Returns
GetOptional("key", update_func) if it is not std::nullopt. Otherwise the result of update_func(key) is returned, and additionally stored in cache if "read_mode" is kUseCache.

Definition at line 207 of file expirable_lru_cache.hpp.

◆ GetMaxLifetime()

template<typename Key , typename Value , typename Hash , typename Equal >
std::chrono::milliseconds cache::ExpirableLruCache< Key, Value, Hash, Equal >::GetMaxLifetime ( ) const
noexcept

Definition at line 190 of file expirable_lru_cache.hpp.

◆ GetOptional()

template<typename Key , typename Value , typename Hash , typename Equal >
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.

Returns
value by key if key is in cache and not expired, or std::nullopt otherwise

Definition at line 232 of file expirable_lru_cache.hpp.

◆ GetOptionalNoUpdate()

template<typename Key , typename Value , typename Hash , typename Equal >
std::optional< Value > cache::ExpirableLruCache< Key, Value, Hash, Equal >::GetOptionalNoUpdate ( const Key & key)

GetOptional, but without value updates.

Definition at line 293 of file expirable_lru_cache.hpp.

◆ GetOptionalUnexpirable()

template<typename Key , typename Value , typename Hash , typename Equal >
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 257 of file expirable_lru_cache.hpp.

◆ GetOptionalUnexpirableWithUpdate()

template<typename Key , typename Value , typename Hash , typename Equal >
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 272 of file expirable_lru_cache.hpp.

◆ GetSizeApproximate()

template<typename Key , typename Value , typename Hash , typename Equal >
size_t cache::ExpirableLruCache< Key, Value, Hash, Equal >::GetSizeApproximate ( ) const

Definition at line 331 of file expirable_lru_cache.hpp.

◆ GetStatistics()

template<typename Key , typename Value , typename Hash , typename Equal >
const impl::ExpirableLruCacheStatistics & cache::ExpirableLruCache< Key, Value, Hash, Equal >::GetStatistics ( ) const

Definition at line 326 of file expirable_lru_cache.hpp.

◆ Invalidate()

template<typename Key , typename Value , typename Hash , typename Equal >
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::Invalidate ( )

Clear cache.

Definition at line 336 of file expirable_lru_cache.hpp.

◆ InvalidateByKey()

template<typename Key , typename Value , typename Hash , typename Equal >
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::InvalidateByKey ( const Key & key)

Erase key from cache.

Definition at line 341 of file expirable_lru_cache.hpp.

◆ Put() [1/2]

template<typename Key , typename Value , typename Hash , typename Equal >
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::Put ( const Key & key,
const Value & value )

Definition at line 313 of file expirable_lru_cache.hpp.

◆ Put() [2/2]

template<typename Key , typename Value , typename Hash , typename Equal >
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::Put ( const Key & key,
Value && value )

Definition at line 319 of file expirable_lru_cache.hpp.

◆ Read()

template<typename Key , typename Value , typename Hash , typename Equal >
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::Read ( dump::Reader & reader)

Definition at line 429 of file expirable_lru_cache.hpp.

◆ SetBackgroundUpdate()

template<typename Key , typename Value , typename Hash , typename Equal >
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 201 of file expirable_lru_cache.hpp.

◆ SetDumper()

template<typename Key , typename Value , typename Hash , typename Equal >
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 435 of file expirable_lru_cache.hpp.

◆ SetMaxLifetime()

template<typename Key , typename Value , typename Hash , typename Equal >
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::SetMaxLifetime ( std::chrono::milliseconds max_lifetime)

Definition at line 195 of file expirable_lru_cache.hpp.

◆ SetWaySize()

template<typename Key , typename Value , typename Hash , typename Equal >
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 184 of file expirable_lru_cache.hpp.

◆ UpdateInBackground()

template<typename Key , typename Value , typename Hash , typename Equal >
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 347 of file expirable_lru_cache.hpp.

◆ Write()

template<typename Key , typename Value , typename Hash , typename Equal >
void cache::ExpirableLruCache< Key, Value, Hash, Equal >::Write ( dump::Writer & writer) const

Definition at line 422 of file expirable_lru_cache.hpp.


The documentation for this class was generated from the following file: