loon
High-performance header-only C++ library for low-latency applications
Loading...
Searching...
No Matches
loon::LRU< K, V > Class Template Reference

A Least Recently Used (LRU) cache with O(1) access and eviction. More...

#include <lru.hpp>

Public Member Functions

 LRU (size_t size)
 Constructs an LRU cache with the specified capacity.
 
std::optional< std::reference_wrapper< V > > get (const K &key)
 Retrieves a value from the cache.
 
void put (const K &key, const V &value)
 Inserts or updates a key-value pair in the cache.
 
bool exists (const K &key) const
 Checks if a key exists in the cache.
 
void remove (const K &key)
 Removes a key-value pair from the cache.
 
size_t size () const
 Returns the current number of entries in the cache.
 

Detailed Description

template<typename K, typename V>
class loon::LRU< K, V >

A Least Recently Used (LRU) cache with O(1) access and eviction.

This cache maintains a fixed capacity and automatically evicts the least recently used entries when the capacity is exceeded. Both get() and put() operations update the recency of the accessed key.

Template Parameters
KKey type (must be hashable for std::unordered_map)
VValue type
cache.put("key", 42);
if (auto val = cache.get("key")) {
std::cout << val->get() << std::endl;
}
A Least Recently Used (LRU) cache with O(1) access and eviction.
Definition lru.hpp:29

Definition at line 29 of file lru.hpp.

Constructor & Destructor Documentation

◆ LRU()

template<typename K , typename V >
loon::LRU< K, V >::LRU ( size_t  size)
inlineexplicit

Constructs an LRU cache with the specified capacity.

Parameters
sizeMaximum number of entries the cache can hold.

Definition at line 33 of file lru.hpp.

Member Function Documentation

◆ exists()

template<typename K , typename V >
bool loon::LRU< K, V >::exists ( const K &  key) const
inline

Checks if a key exists in the cache.

This operation does not affect the recency of the key.

Parameters
keyThe key to check.
Returns
true if the key exists, false otherwise.

Definition at line 81 of file lru.hpp.

◆ get()

template<typename K , typename V >
std::optional< std::reference_wrapper< V > > loon::LRU< K, V >::get ( const K &  key)
inline

Retrieves a value from the cache.

If the key exists, it is marked as most recently used.

Parameters
keyThe key to look up.
Returns
A reference to the value if found, std::nullopt otherwise.

Definition at line 41 of file lru.hpp.

◆ put()

template<typename K , typename V >
void loon::LRU< K, V >::put ( const K &  key,
const V &  value 
)
inline

Inserts or updates a key-value pair in the cache.

If the key already exists, its value is updated and it becomes the most recently used. If the cache is at capacity, the least recently used entry is evicted before inserting the new entry.

Parameters
keyThe key to insert or update.
valueThe value to associate with the key.

Definition at line 59 of file lru.hpp.

◆ remove()

template<typename K , typename V >
void loon::LRU< K, V >::remove ( const K &  key)
inline

Removes a key-value pair from the cache.

If the key does not exist, this operation has no effect.

Parameters
keyThe key to remove.

Definition at line 88 of file lru.hpp.

◆ size()

template<typename K , typename V >
size_t loon::LRU< K, V >::size ( ) const
inline

Returns the current number of entries in the cache.

Returns
The number of cached entries.

Definition at line 98 of file lru.hpp.


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