Github   Telegram
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | List of all members
cache::ExpirableLruCache< Key, Value, Hash, Equal > Class Template Referencefinal

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. More...
 
void InvalidateByKey (const Key &key)
 Erase key from cache. More...
 
void UpdateInBackground (const Key &key, UpdateValueFunc update_func)
 Add async task for updating value by update_func(key) More...
 

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 32 of file expirable_lru_cache.hpp.

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 34 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 37 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() 
)

Definition at line 137 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 143 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 171 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
noexcept

Definition at line 154 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 196 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 257 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 221 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 236 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

Definition at line 295 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

Definition at line 290 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 300 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 305 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 277 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 283 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 165 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 159 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)

Definition at line 148 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 311 of file expirable_lru_cache.hpp.


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