template<typename Key, typename Value, typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
liarsdice::data_structures::LRUCache class

High-performance LRU cache using boost::multi_index.

Template parameters
Key Key type
Value Value type
Hash Hash function for Key (default: std::hash<Key>)
KeyEqual Equality comparator for Key (default: std::equal_to<Key>)

Provides O(1) access and automatic eviction of least recently used items. Uses both sequenced and hashed indices for efficient operations.

Public types

using size_type = typename cache_container::size_type

Constructors, destructors, conversion operators

LRUCache(size_type max_size) explicit
Construct LRU cache with maximum capacity.

Public functions

auto put(const Key& key, const Value& value) -> bool
Insert or update a key-value pair.
auto get(const Key& key) -> boost::optional<Value>
Get value by key.
auto contains(const Key& key) const -> bool
Check if key exists without updating LRU order.
auto erase(const Key& key) -> bool
Remove entry by key.
void clear()
Clear all entries.
auto size() const -> size_type
Get current number of entries.
auto empty() const -> bool
Check if cache is empty.
auto full() const -> bool
Check if cache is full.
auto capacity() const -> size_type
Get maximum capacity.
auto get_stats() const -> std::tuple<size_t, size_t, size_t, double>
Get cache statistics.
auto get_keys() const -> std::vector<Key>
Get all cached keys in LRU order.
template<typename Func>
void for_each(Func func) const
Apply function to all entries in LRU order.
void resize(size_type new_size)
Resize cache capacity.

Function documentation

template<typename Key, typename Value, typename Hash, typename KeyEqual>
liarsdice::data_structures::LRUCache<Key, Value, Hash, KeyEqual>::LRUCache(size_type max_size) explicit

Construct LRU cache with maximum capacity.

Parameters
max_size Maximum number of entries

template<typename Key, typename Value, typename Hash, typename KeyEqual>
bool liarsdice::data_structures::LRUCache<Key, Value, Hash, KeyEqual>::put(const Key& key, const Value& value)

Insert or update a key-value pair.

Parameters
key The key
value The value
Returns True if a new entry was created, false if updated

template<typename Key, typename Value, typename Hash, typename KeyEqual>
boost::optional<Value> liarsdice::data_structures::LRUCache<Key, Value, Hash, KeyEqual>::get(const Key& key)

Get value by key.

Parameters
key The key to lookup
Returns Optional containing the value if found

template<typename Key, typename Value, typename Hash, typename KeyEqual>
bool liarsdice::data_structures::LRUCache<Key, Value, Hash, KeyEqual>::contains(const Key& key) const

Check if key exists without updating LRU order.

Parameters
key The key to check
Returns True if key exists

template<typename Key, typename Value, typename Hash, typename KeyEqual>
bool liarsdice::data_structures::LRUCache<Key, Value, Hash, KeyEqual>::erase(const Key& key)

Remove entry by key.

Parameters
key The key to remove
Returns True if entry was removed

template<typename Key, typename Value, typename Hash, typename KeyEqual>
size_type liarsdice::data_structures::LRUCache<Key, Value, Hash, KeyEqual>::size() const

Get current number of entries.

Returns Number of cached entries

template<typename Key, typename Value, typename Hash, typename KeyEqual>
bool liarsdice::data_structures::LRUCache<Key, Value, Hash, KeyEqual>::empty() const

Check if cache is empty.

Returns True if no entries cached

template<typename Key, typename Value, typename Hash, typename KeyEqual>
bool liarsdice::data_structures::LRUCache<Key, Value, Hash, KeyEqual>::full() const

Check if cache is full.

Returns True if at maximum capacity

template<typename Key, typename Value, typename Hash, typename KeyEqual>
size_type liarsdice::data_structures::LRUCache<Key, Value, Hash, KeyEqual>::capacity() const

Get maximum capacity.

Returns Maximum number of entries

template<typename Key, typename Value, typename Hash, typename KeyEqual>
std::tuple<size_t, size_t, size_t, double> liarsdice::data_structures::LRUCache<Key, Value, Hash, KeyEqual>::get_stats() const

Get cache statistics.

Returns Tuple of (hits, misses, evictions, hit_rate)

template<typename Key, typename Value, typename Hash, typename KeyEqual>
std::vector<Key> liarsdice::data_structures::LRUCache<Key, Value, Hash, KeyEqual>::get_keys() const

Get all cached keys in LRU order.

Returns Vector of keys (most recently used first)

template<typename Key, typename Value, typename Hash, typename KeyEqual> template<typename Func>
void liarsdice::data_structures::LRUCache<Key, Value, Hash, KeyEqual>::for_each(Func func) const

Apply function to all entries in LRU order.

Template parameters
Func Function type
Parameters
func Function to apply (key, value, access_count)

template<typename Key, typename Value, typename Hash, typename KeyEqual>
void liarsdice::data_structures::LRUCache<Key, Value, Hash, KeyEqual>::resize(size_type new_size)

Resize cache capacity.

Parameters
new_size New maximum capacity

If new_size < current size, least recently used entries are evicted