|
| | 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.
|
| |
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
-
| K | Key type (must be hashable for std::unordered_map) |
| V | Value 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 at line 29 of file lru.hpp.
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
-
- Returns
- A reference to the value if found, std::nullopt otherwise.
Definition at line 41 of file lru.hpp.
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
-
| key | The key to insert or update. |
| value | The value to associate with the key. |
Definition at line 59 of file lru.hpp.
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
-
Definition at line 88 of file lru.hpp.