Grok 20.3.2
grk::LRUCache< Key, Value > Class Template Reference

Thread-safe LRU cache with configurable capacity and eviction callback. More...

#include <LRUCache.h>

Public Types

using SizeFunc = std::function<size_t(const Value&)>
using EvictFunc = std::function<void(const Key&, Value&&)>

Public Member Functions

 LRUCache (size_t maxBytes, SizeFunc sizeFunc, EvictFunc evictFunc=nullptr)
 Construct an LRU cache.
 ~LRUCache ()=default
 LRUCache (const LRUCache &)=delete
LRUCacheoperator= (const LRUCache &)=delete
 LRUCache (LRUCache &&)=default
LRUCacheoperator= (LRUCache &&)=default
void put (const Key &key, Value value)
 Insert or update an entry.
Value * get (const Key &key)
 Look up an entry.
bool contains (const Key &key) const
 Check if a key exists without promoting it in the LRU order.
bool erase (const Key &key)
 Remove a specific entry.
void clear ()
 Clear all entries.
size_t size () const
size_t currentBytes () const
size_t maxBytes () const
void setMaxBytes (size_t maxBytes)

Private Types

using ListType = std::list<std::pair<Key, Value>>

Private Member Functions

void evictWhileOverCapacity ()

Private Attributes

size_t maxBytes_
size_t currentBytes_
SizeFunc sizeFunc_
EvictFunc evictFunc_
ListType list_
std::unordered_map< Key, typename ListType::iterator > map_
std::mutex mutex_

Detailed Description

template<typename Key, typename Value>
class grk::LRUCache< Key, Value >

Thread-safe LRU cache with configurable capacity and eviction callback.

Key must be hashable. Value must be moveable. The sizeFunc callback returns the cost (in bytes) of a value, enabling memory-based eviction rather than count-based.

Template Parameters
Keykey type (must be hashable)
Valuevalue type (must be moveable)

Member Typedef Documentation

◆ EvictFunc

template<typename Key, typename Value>
using grk::LRUCache< Key, Value >::EvictFunc = std::function<void(const Key&, Value&&)>

◆ ListType

template<typename Key, typename Value>
using grk::LRUCache< Key, Value >::ListType = std::list<std::pair<Key, Value>>
private

◆ SizeFunc

template<typename Key, typename Value>
using grk::LRUCache< Key, Value >::SizeFunc = std::function<size_t(const Value&)>

Constructor & Destructor Documentation

◆ LRUCache() [1/3]

template<typename Key, typename Value>
grk::LRUCache< Key, Value >::LRUCache ( size_t maxBytes,
SizeFunc sizeFunc,
EvictFunc evictFunc = nullptr )
inline

Construct an LRU cache.

Parameters
maxByteshigh water mark in bytes (0 = unlimited)
sizeFuncreturns size in bytes for a given value
evictFunccalled when an entry is evicted (optional)

References currentBytes_, evictFunc_, maxBytes(), maxBytes_, and sizeFunc_.

Referenced by LRUCache(), LRUCache(), operator=(), and operator=().

◆ ~LRUCache()

template<typename Key, typename Value>
grk::LRUCache< Key, Value >::~LRUCache ( )
default

◆ LRUCache() [2/3]

template<typename Key, typename Value>
grk::LRUCache< Key, Value >::LRUCache ( const LRUCache< Key, Value > & )
delete

References LRUCache().

◆ LRUCache() [3/3]

template<typename Key, typename Value>
grk::LRUCache< Key, Value >::LRUCache ( LRUCache< Key, Value > && )
default

References LRUCache().

Member Function Documentation

◆ clear()

template<typename Key, typename Value>
void grk::LRUCache< Key, Value >::clear ( )
inline

Clear all entries.

References currentBytes_, list_, map_, and mutex_.

◆ contains()

template<typename Key, typename Value>
bool grk::LRUCache< Key, Value >::contains ( const Key & key) const
inline

Check if a key exists without promoting it in the LRU order.

References map_, and mutex_.

◆ currentBytes()

template<typename Key, typename Value>
size_t grk::LRUCache< Key, Value >::currentBytes ( ) const
inline

References currentBytes_, and mutex_.

◆ erase()

template<typename Key, typename Value>
bool grk::LRUCache< Key, Value >::erase ( const Key & key)
inline

Remove a specific entry.

References currentBytes_, list_, map_, mutex_, and sizeFunc_.

◆ evictWhileOverCapacity()

template<typename Key, typename Value>
void grk::LRUCache< Key, Value >::evictWhileOverCapacity ( )
inlineprivate

References currentBytes_, evictFunc_, list_, map_, maxBytes_, and sizeFunc_.

Referenced by put(), and setMaxBytes().

◆ get()

template<typename Key, typename Value>
Value * grk::LRUCache< Key, Value >::get ( const Key & key)
inline

Look up an entry.

Moves it to the front (most recently used).

Parameters
keycache key
Returns
pointer to the value, or nullptr if not found. Pointer is valid until next put/erase/clear.

References list_, map_, and mutex_.

◆ maxBytes()

template<typename Key, typename Value>
size_t grk::LRUCache< Key, Value >::maxBytes ( ) const
inline

References maxBytes_.

Referenced by LRUCache(), and setMaxBytes().

◆ operator=() [1/2]

template<typename Key, typename Value>
LRUCache & grk::LRUCache< Key, Value >::operator= ( const LRUCache< Key, Value > & )
delete

References LRUCache().

◆ operator=() [2/2]

template<typename Key, typename Value>
LRUCache & grk::LRUCache< Key, Value >::operator= ( LRUCache< Key, Value > && )
default

References LRUCache().

◆ put()

template<typename Key, typename Value>
void grk::LRUCache< Key, Value >::put ( const Key & key,
Value value )
inline

Insert or update an entry.

Evicts LRU entries if over capacity.

Parameters
keycache key
valuecache value (moved in)

References currentBytes_, evictWhileOverCapacity(), list_, map_, mutex_, and sizeFunc_.

◆ setMaxBytes()

template<typename Key, typename Value>
void grk::LRUCache< Key, Value >::setMaxBytes ( size_t maxBytes)
inline

◆ size()

template<typename Key, typename Value>
size_t grk::LRUCache< Key, Value >::size ( ) const
inline

References map_, and mutex_.

Member Data Documentation

◆ currentBytes_

template<typename Key, typename Value>
size_t grk::LRUCache< Key, Value >::currentBytes_
private

◆ evictFunc_

template<typename Key, typename Value>
EvictFunc grk::LRUCache< Key, Value >::evictFunc_
private

Referenced by evictWhileOverCapacity(), and LRUCache().

◆ list_

template<typename Key, typename Value>
ListType grk::LRUCache< Key, Value >::list_
private

◆ map_

template<typename Key, typename Value>
std::unordered_map<Key, typename ListType::iterator> grk::LRUCache< Key, Value >::map_
private

◆ maxBytes_

template<typename Key, typename Value>
size_t grk::LRUCache< Key, Value >::maxBytes_
private

◆ mutex_

template<typename Key, typename Value>
std::mutex grk::LRUCache< Key, Value >::mutex_
mutableprivate

◆ sizeFunc_

template<typename Key, typename Value>
SizeFunc grk::LRUCache< Key, Value >::sizeFunc_
private

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