Gin
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | List of all members
LRUCache< Key, Value, MaxSize > Class Template Reference

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.
 

Detailed Description

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

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:

// Insert items
cache.put ("one", 1);
cache.put ("two", 2);
// Retrieve items
if (auto* value = cache.get ("one"))
process (*value);
// Use getOrCreate for lazy initialization
auto& value = cache.getOrCreate ("three", [] { return expensiveComputation(); });
A lightweight 2D point class for projects that don't use juce_graphics.
Definition gin_point.h:25
Template Parameters
KeyThe type used as the cache key. Must be hashable via std::hash.
ValueThe type of values stored in the cache.
MaxSizeThe maximum number of items the cache can hold.

Constructor & Destructor Documentation

◆ LRUCache()

template<typename Key , typename Value , size_t MaxSize>
LRUCache< Key, Value, MaxSize >::LRUCache ( )
default

Creates an empty LRU cache.

Member Function Documentation

◆ get() [1/2]

template<typename Key , typename Value , size_t MaxSize>
Value * LRUCache< Key, Value, MaxSize >::get ( const Key key)

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).

Parameters
keyThe key to look up
Returns
A pointer to the value if found, nullptr otherwise

Referenced by LRUCache< Key, Value, MaxSize >::get(), and LRUCache< Key, Value, MaxSize >::getOrCreate().

◆ get() [2/2]

template<typename Key , typename Value , size_t MaxSize>
const Value * LRUCache< Key, Value, MaxSize >::get ( const Key key) const

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).

Parameters
keyThe key to look up
Returns
A const pointer to the value if found, nullptr otherwise

References LRUCache< Key, Value, MaxSize >::get().

◆ put()

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

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.

Parameters
keyThe key to insert or update
valueThe value to store

Referenced by LRUCache< Key, Value, MaxSize >::getOrCreate().

◆ getOrCreate()

template<typename Key , typename Value , size_t MaxSize>
template<typename Factory >
Value & LRUCache< Key, Value, MaxSize >::getOrCreate ( const Key key,
Factory &&  factory 
)

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.

Parameters
keyThe key to look up or create
factoryA callable that takes no arguments and returns a Value
Returns
A reference to the cached value

References LRUCache< Key, Value, MaxSize >::get(), and LRUCache< Key, Value, MaxSize >::put().

◆ remove()

template<typename Key , typename Value , size_t MaxSize>
bool LRUCache< Key, Value, MaxSize >::remove ( const Key key)

Removes an item from the cache.

Parameters
keyThe key to remove
Returns
true if the item was found and removed, false otherwise

◆ contains()

template<typename Key , typename Value , size_t MaxSize>
bool LRUCache< Key, Value, MaxSize >::contains ( const Key key) const

Checks if a key exists in the cache.

Note: This does NOT update the access order.

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

◆ size()

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

Returns the number of items currently in the cache.

Returns
The current size of the cache

◆ empty()

template<typename Key , typename Value , size_t MaxSize>
bool LRUCache< Key, Value, MaxSize >::empty ( ) const
noexcept

Checks if the cache is empty.

Returns
true if the cache contains no items

◆ clear()

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

Removes all items from the cache.

◆ maxSize()

template<typename Key , typename Value , size_t MaxSize>
static constexpr size_t LRUCache< Key, Value, MaxSize >::maxSize ( )
staticconstexprnoexcept

Returns the maximum number of items the cache can hold.

Returns
The maximum capacity

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