userver: cache::LruMap< T, U, Hash, Equal > Class Template Reference
Loading...
Searching...
No Matches
cache::LruMap< T, U, Hash, Equal > Class Template Referencefinal

#include <userver/cache/lru_map.hpp>

Detailed Description

template<typename T, typename U, typename Hash = std::hash<T>, typename Equal = std::equal_to<T>>
class cache::LruMap< T, U, Hash, Equal >

LRU key value storage (LRU cache), thread safety matches Standard Library thread safety

Definition at line 19 of file lru_map.hpp.

Public Member Functions

 LruMap (size_t max_size, const Hash &hash=Hash(), const Equal &equal=Equal())
 
 LruMap (LruMap &&lru) noexcept=default
 
 LruMap (const LruMap &lru)=delete
 
LruMapoperator= (LruMap &&lru) noexcept=default
 
LruMapoperator= (const LruMap &lru)=delete
 
bool Put (const T &key, U value)
 
template<typename... Args>
U * Emplace (const T &key, Args &&... args)
 
void Erase (const T &key)
 Removes key from LRU.
 
U * Get (const T &key)
 
GetOr (const T &key, const U &default_value)
 
U * GetLeastUsed ()
 
void SetMaxSize (size_t new_max_size)
 Sets the max size of the LRU, truncates values if new_max_size < GetSize()
 
void Clear ()
 Removes all the elements.
 
template<typename Function >
void VisitAll (Function &&func) const
 Call Function(const T&, const U&) for all items.
 
template<typename Function >
void VisitAll (Function &&func)
 Call Function(const T&, U&) for all items.
 
size_t GetSize () const
 
std::size_t GetCapacity () const
 

Constructor & Destructor Documentation

◆ LruMap()

template<typename T , typename U , typename Hash = std::hash<T>, typename Equal = std::equal_to<T>>
cache::LruMap< T, U, Hash, Equal >::LruMap ( size_t max_size,
const Hash & hash = Hash(),
const Equal & equal = Equal() )
inlineexplicit

Definition at line 21 of file lru_map.hpp.

Member Function Documentation

◆ Clear()

template<typename T , typename U , typename Hash = std::hash<T>, typename Equal = std::equal_to<T>>
void cache::LruMap< T, U, Hash, Equal >::Clear ( )
inline

Removes all the elements.

Definition at line 69 of file lru_map.hpp.

◆ Emplace()

template<typename T , typename U , typename Hash = std::hash<T>, typename Equal = std::equal_to<T>>
template<typename... Args>
U * cache::LruMap< T, U, Hash, Equal >::Emplace ( const T & key,
Args &&... args )
inline

Returns pointer to value if the key is in LRU and updates its usage; constructs and adds a new key/value otherwise.

Warning
Returned pointer may be freed on the next map access!

Definition at line 38 of file lru_map.hpp.

◆ Erase()

template<typename T , typename U , typename Hash = std::hash<T>, typename Equal = std::equal_to<T>>
void cache::LruMap< T, U, Hash, Equal >::Erase ( const T & key)
inline

Removes key from LRU.

Definition at line 43 of file lru_map.hpp.

◆ Get()

template<typename T , typename U , typename Hash = std::hash<T>, typename Equal = std::equal_to<T>>
U * cache::LruMap< T, U, Hash, Equal >::Get ( const T & key)
inline

Returns pointer to value if the key is in LRU and updates its usage; returns nullptr otherwise.

Warning
Returned pointer may be freed on the next map access!

Definition at line 48 of file lru_map.hpp.

◆ GetCapacity()

template<typename T , typename U , typename Hash = std::hash<T>, typename Equal = std::equal_to<T>>
std::size_t cache::LruMap< T, U, Hash, Equal >::GetCapacity ( ) const
inline

Definition at line 85 of file lru_map.hpp.

◆ GetLeastUsed()

template<typename T , typename U , typename Hash = std::hash<T>, typename Equal = std::equal_to<T>>
U * cache::LruMap< T, U, Hash, Equal >::GetLeastUsed ( )
inline

Returns pointer to the least recently used value; returns nullptr if LRU is empty.

Warning
Returned pointer may be freed on the next map access!

Definition at line 61 of file lru_map.hpp.

◆ GetOr()

template<typename T , typename U , typename Hash = std::hash<T>, typename Equal = std::equal_to<T>>
U cache::LruMap< T, U, Hash, Equal >::GetOr ( const T & key,
const U & default_value )
inline

Returns value by key and updates its usage; returns default_value otherwise without modifying the cache.

Definition at line 52 of file lru_map.hpp.

◆ GetSize()

template<typename T , typename U , typename Hash = std::hash<T>, typename Equal = std::equal_to<T>>
size_t cache::LruMap< T, U, Hash, Equal >::GetSize ( ) const
inline

Definition at line 83 of file lru_map.hpp.

◆ Put()

template<typename T , typename U , typename Hash = std::hash<T>, typename Equal = std::equal_to<T>>
bool cache::LruMap< T, U, Hash, Equal >::Put ( const T & key,
U value )
inline

Adds or rewrites key/value, updates its usage

Returns
true if key is a new one

Definition at line 32 of file lru_map.hpp.

◆ SetMaxSize()

template<typename T , typename U , typename Hash = std::hash<T>, typename Equal = std::equal_to<T>>
void cache::LruMap< T, U, Hash, Equal >::SetMaxSize ( size_t new_max_size)
inline

Sets the max size of the LRU, truncates values if new_max_size < GetSize()

Definition at line 64 of file lru_map.hpp.

◆ VisitAll() [1/2]

template<typename T , typename U , typename Hash = std::hash<T>, typename Equal = std::equal_to<T>>
template<typename Function >
void cache::LruMap< T, U, Hash, Equal >::VisitAll ( Function && func)
inline

Call Function(const T&, U&) for all items.

Definition at line 79 of file lru_map.hpp.

◆ VisitAll() [2/2]

template<typename T , typename U , typename Hash = std::hash<T>, typename Equal = std::equal_to<T>>
template<typename Function >
void cache::LruMap< T, U, Hash, Equal >::VisitAll ( Function && func) const
inline

Call Function(const T&, const U&) for all items.

Definition at line 73 of file lru_map.hpp.


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