game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
LRUCache< Key, Value > Class Template Reference

Thread-safe LRU cache with optional TTL support. More...

#include <lru_cache.h>

Classes

struct  Stats

Public Types

using Clock = std::chrono::steady_clock
using TimePoint = Clock::time_point

Public Member Functions

 LRUCache (const LRUCache &)=delete
 LRUCache (LRUCache &&)=delete
 LRUCache (size_t maxSize, int ttlSeconds=0)
 Construct an LRU cache.
void clear ()
 Clear all entries.
bool contains (const Key &key) const
 Check if key exists and is not expired (without updating LRU order).
bool empty () const
 Check if cache is empty.
std::optional< Value > get (const Key &key)
 Get a value from the cache.
Stats getStats () const
 Get cache statistics.
size_t maxSize () const
 Get maximum capacity.
LRUCache & operator= (const LRUCache &)=delete
LRUCache & operator= (LRUCache &&)=delete
size_t pruneExpired ()
 Remove expired entries (useful for periodic cleanup).
void put (const Key &key, const Value &value)
 Put a value in the cache.
void put (const Key &key, Value &&value)
 Put a value using move semantics.
bool remove (const Key &key)
 Remove a specific key from cache.
void resetStats ()
 Reset statistics counters.
size_t size () const
 Get current number of entries.

Detailed Description

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

Thread-safe LRU cache with optional TTL support.

Template Parameters
KeyType of cache keys (must be hashable)
ValueType of cached values

Definition at line 43 of file lru_cache.h.

Member Typedef Documentation

◆ Clock

template<typename Key, typename Value>
using LRUCache< Key, Value >::Clock = std::chrono::steady_clock

Definition at line 46 of file lru_cache.h.

◆ TimePoint

template<typename Key, typename Value>
using LRUCache< Key, Value >::TimePoint = Clock::time_point

Definition at line 47 of file lru_cache.h.

Constructor & Destructor Documentation

◆ LRUCache() [1/3]

template<typename Key, typename Value>
LRUCache< Key, Value >::LRUCache ( size_t maxSize,
int ttlSeconds = 0 )
inlineexplicit

Construct an LRU cache.

Parameters
maxSizeMaximum number of entries (must be >= 1)
ttlSecondsTime-to-live in seconds (0 = no expiration)

Definition at line 54 of file lru_cache.h.

References maxSize().

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

◆ LRUCache() [2/3]

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

References LRUCache().

◆ LRUCache() [3/3]

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

References LRUCache().

Member Function Documentation

◆ clear()

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

Clear all entries.

Definition at line 211 of file lru_cache.h.

◆ contains()

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

Check if key exists and is not expired (without updating LRU order).

Definition at line 174 of file lru_cache.h.

◆ empty()

template<typename Key, typename Value>
bool LRUCache< Key, Value >::empty ( ) const
inline

Check if cache is empty.

Definition at line 238 of file lru_cache.h.

◆ get()

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

Get a value from the cache.

Parameters
keyKey to look up
Returns
Optional containing value if found and not expired, empty otherwise

Moves the accessed entry to the front (most recently used).

Definition at line 72 of file lru_cache.h.

◆ getStats()

template<typename Key, typename Value>
Stats LRUCache< Key, Value >::getStats ( ) const
inline

Get cache statistics.

Definition at line 267 of file lru_cache.h.

◆ maxSize()

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

Get maximum capacity.

Definition at line 230 of file lru_cache.h.

Referenced by LRUCache().

◆ operator=() [1/2]

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

References LRUCache().

◆ operator=() [2/2]

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

References LRUCache().

◆ pruneExpired()

template<typename Key, typename Value>
size_t LRUCache< Key, Value >::pruneExpired ( )
inline

Remove expired entries (useful for periodic cleanup).

Returns
Number of entries removed

Definition at line 286 of file lru_cache.h.

◆ put() [1/2]

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

Put a value in the cache.

Parameters
keyKey to store under
valueValue to cache

If key exists, updates the value and moves to front. If cache is full, evicts the least recently used entry.

Definition at line 115 of file lru_cache.h.

◆ put() [2/2]

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

Put a value using move semantics.

Definition at line 146 of file lru_cache.h.

◆ remove()

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

Remove a specific key from cache.

Returns
true if key was found and removed

Definition at line 195 of file lru_cache.h.

◆ resetStats()

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

Reset statistics counters.

Definition at line 276 of file lru_cache.h.

◆ size()

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

Get current number of entries.

Definition at line 221 of file lru_cache.h.


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