|
Gin
|
A simple LRU (Least Recently Used) cache with a fixed maximum size. More...
#include <gin_lrucache.h>
Public Member Functions | |
| LRUCache ()=default | |
| Creates an empty LRU cache. | |
| Value * | get (const Key &key) |
| Returns a pointer to the cached value for the given key, or nullptr if not found. | |
| const Value * | get (const Key &key) const |
| Returns a const pointer to the cached value for the given key, or nullptr if not found. | |
| void | put (const Key &key, Value value) |
| Inserts or updates a value in the cache. | |
| template<typename Factory > | |
| Value & | getOrCreate (const Key &key, Factory &&factory) |
| Gets an existing value or creates a new one using the provided factory function. | |
| bool | remove (const Key &key) |
| Removes an item from the cache. | |
| bool | contains (const Key &key) const |
| Checks if a key exists in the cache. | |
| size_t | size () const noexcept |
| Returns the number of items currently in the cache. | |
| bool | empty () const noexcept |
| Checks if the cache is empty. | |
| void | clear () |
| Removes all items from the cache. | |
Static Public Member Functions | |
| static constexpr size_t | maxSize () noexcept |
| Returns the maximum number of items the cache can hold. | |
A simple LRU (Least Recently Used) cache with a fixed maximum size.
This template class provides a cache that automatically evicts the least recently accessed items when it reaches capacity. It uses a hash map for O(1) average-case lookups and a linked list to track access order.
The cache is not thread-safe. If you need thread safety, you should synchronize access externally.
Example usage:
| Key | The type used as the cache key. Must be hashable via std::hash. |
| Value | The type of values stored in the cache. |
| MaxSize | The maximum number of items the cache can hold. |
|
default |
Creates an empty LRU cache.
Returns a pointer to the cached value for the given key, or nullptr if not found.
Accessing an item moves it to the front of the cache (most recently used).
| key | The key to look up |
Referenced by LRUCache< Key, Value, MaxSize >::get(), and LRUCache< Key, Value, MaxSize >::getOrCreate().
Returns a const pointer to the cached value for the given key, or nullptr if not found.
Accessing an item moves it to the front of the cache (most recently used).
| key | The key to look up |
References LRUCache< Key, Value, MaxSize >::get().
Inserts or updates a value in the cache.
If the key already exists, its value is updated and it's moved to the front. If the cache is at capacity, the least recently used item is evicted.
| key | The key to insert or update |
| value | The value to store |
Referenced by LRUCache< Key, Value, MaxSize >::getOrCreate().
Gets an existing value or creates a new one using the provided factory function.
If the key exists, returns a reference to its value (moving it to front). If the key doesn't exist, calls the factory function to create a value, inserts it, and returns a reference to it.
| key | The key to look up or create |
| factory | A callable that takes no arguments and returns a Value |
References LRUCache< Key, Value, MaxSize >::get(), and LRUCache< Key, Value, MaxSize >::put().
Removes an item from the cache.
| key | The key to remove |
Checks if a key exists in the cache.
Note: This does NOT update the access order.
| key | The key to check |
Returns the number of items currently in the cache.
Checks if the cache is empty.
Returns the maximum number of items the cache can hold.