userver: cache::LruCacheWrapper< Key, Value, Hash, Equal > Class Template Reference
Loading...
Searching...
No Matches
cache::LruCacheWrapper< 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::LruCacheWrapper< Key, Value, Hash, Equal >

Wrapper around ExpirableLruCache that binds an update function for convenience.

auto cache_ptr = std::make_shared<cache::ExpirableLruCache<std::string, int>>(1, 1);
cache::LruCacheWrapper wrapper(cache_ptr, [](const std::string& /*key*/) {
static std::atomic<int> counter{0};
return ++counter;
});
const std::string key = "my-key";
EXPECT_EQ(std::nullopt, wrapper.GetOptional(key));
EXPECT_EQ(1, wrapper.Get(key));
EXPECT_EQ(std::make_optional(1), wrapper.GetOptional(key));
wrapper.InvalidateByKey(key);
EXPECT_EQ(2, wrapper.Get(key));
EXPECT_EQ(std::make_optional(2), wrapper.GetOptional(key));

Definition at line 492 of file expirable_lru_cache.hpp.

Public Types

using Cache = ExpirableLruCache<Key, Value, Hash, Equal>
 The underlying cache type.
 
using ReadMode = typename Cache::ReadMode
 Read mode for Get (same as Cache::ReadMode)
 

Public Member Functions

 LruCacheWrapper (std::shared_ptr< Cache > cache, typename Cache::UpdateValueFunc update_func)
 Constructs wrapper with shared cache and update function.
 
Value Get (const Key &key, ReadMode read_mode=ReadMode::kUseCache)
 Returns cached value or computes it if key is missing in cache.
 
std::optional< Value > GetOptional (const Key &key)
 Returns cached value or std::nullopt if key is missing in cache.
 
void InvalidateByKey (const Key &key)
 Erases key from cache.
 
template<typename Predicate>
void InvalidateByKeyIf (const Key &key, Predicate pred)
 Erases key from cache if pred returns true for the current value.
 
void UpdateInBackground (const Key &key)
 Schedules background update of cached value for the key.
 
std::shared_ptr< CacheGetCache ()
 Returns raw cache pointer.
 

Member Typedef Documentation

◆ Cache

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

The underlying cache type.

Definition at line 495 of file expirable_lru_cache.hpp.

◆ ReadMode

template<typename Key, typename Value, typename Hash = std::hash<Key>, typename Equal = std::equal_to<Key>>
using cache::LruCacheWrapper< Key, Value, Hash, Equal >::ReadMode = typename Cache::ReadMode

Read mode for Get (same as Cache::ReadMode)

Definition at line 497 of file expirable_lru_cache.hpp.

Constructor & Destructor Documentation

◆ LruCacheWrapper()

template<typename Key, typename Value, typename Hash = std::hash<Key>, typename Equal = std::equal_to<Key>>
cache::LruCacheWrapper< Key, Value, Hash, Equal >::LruCacheWrapper ( std::shared_ptr< Cache > cache,
typename Cache::UpdateValueFunc update_func )
inline

Constructs wrapper with shared cache and update function.

The same update_func is used for all Get and GetOptional calls.

Parameters
cacheShared pointer to ExpirableLruCache.
update_funcFunction to compute value when key is missing or expired.

Definition at line 507 of file expirable_lru_cache.hpp.

Member Function Documentation

◆ Get()

template<typename Key, typename Value, typename Hash = std::hash<Key>, typename Equal = std::equal_to<Key>>
Value cache::LruCacheWrapper< Key, Value, Hash, Equal >::Get ( const Key & key,
ReadMode read_mode = ReadMode::kUseCache )
inline

Returns cached value or computes it if key is missing in cache.

Parameters
keyCache key.
read_modeIf kUseCache, caches the computed value.
Returns
Cached or freshly computed value.

Definition at line 517 of file expirable_lru_cache.hpp.

◆ GetCache()

template<typename Key, typename Value, typename Hash = std::hash<Key>, typename Equal = std::equal_to<Key>>
std::shared_ptr< Cache > cache::LruCacheWrapper< Key, Value, Hash, Equal >::GetCache ( )
inline

Returns raw cache pointer.

Returns
Shared pointer to the underlying ExpirableLruCache.
Note
For internal use.

Definition at line 551 of file expirable_lru_cache.hpp.

◆ GetOptional()

template<typename Key, typename Value, typename Hash = std::hash<Key>, typename Equal = std::equal_to<Key>>
std::optional< Value > cache::LruCacheWrapper< Key, Value, Hash, Equal >::GetOptional ( const Key & key)
inline

Returns cached value or std::nullopt if key is missing in cache.

Parameters
keyCache key.
Returns
Value if key is in cache and not expired, otherwise std::nullopt.

Definition at line 525 of file expirable_lru_cache.hpp.

◆ InvalidateByKey()

template<typename Key, typename Value, typename Hash = std::hash<Key>, typename Equal = std::equal_to<Key>>
void cache::LruCacheWrapper< Key, Value, Hash, Equal >::InvalidateByKey ( const Key & key)
inline

Erases key from cache.

Parameters
keyCache key to erase.

Definition at line 530 of file expirable_lru_cache.hpp.

◆ InvalidateByKeyIf()

template<typename Key, typename Value, typename Hash = std::hash<Key>, typename Equal = std::equal_to<Key>>
template<typename Predicate>
void cache::LruCacheWrapper< Key, Value, Hash, Equal >::InvalidateByKeyIf ( const Key & key,
Predicate pred )
inline

Erases key from cache if pred returns true for the current value.

Parameters
keyCache key.
predPredicate invoked with current value.

Definition at line 538 of file expirable_lru_cache.hpp.

◆ UpdateInBackground()

template<typename Key, typename Value, typename Hash = std::hash<Key>, typename Equal = std::equal_to<Key>>
void cache::LruCacheWrapper< Key, Value, Hash, Equal >::UpdateInBackground ( const Key & key)
inline

Schedules background update of cached value for the key.

Parameters
keyCache key to update.

Definition at line 545 of file expirable_lru_cache.hpp.


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