SatCat5
satcat5::util::LruCache< T > Class Template Reference

Detailed Description

template<class T>
class satcat5::util::LruCache< T >

Template class implementing a least-recently-used (LRU) cache.

The "LruCache" template defines a searchable key-value store with a fixed maximum size. Querying a given key returns a pointer to the stored key if it exists, or a newly-created entry otherwise. If necessary, the oldest entry is evicted to make room.

The requirements for items using these this template are:

  • The object MUST declare itself as a friend of satcat5::util::LruCache. e.g., "friend satcat5::util::LruCache<MyClassName>;"
  • The object MUST be a "plain-old-data" class or struct with:
    • Member "m_next" as a pointer to the same type of object.
    • Member "m_key" as any type implementing operator= and operator==.
    • Note: Both "m_next" and "m_key" MUST NOT be declared as const.
  • The object MUST initialize the m_next pointer to zero.
  • The m_next pointer SHOULD generally be marked as "private". (The object SHOULD NOT access the pointer except through LruCache.)

Internally, the class uses a singly-linked list of key-value pairs. For simplicity, search is performed linearly by checking each entry. The list is maintained in most-recently-used order, overwriting the tail as needed when eviction is required.

Definition at line 33 of file lru_cache.h.

#include <lru_cache.h>

Inheritance diagram for satcat5::util::LruCache< T >:
[legend]

Public Member Functions

 LruCache (T *array, unsigned count)
 Given a backing array, initialize an empty cache.
 
void clear ()
 Reset this cache to the empty state.
 
bool is_empty () const
 Is this an empty list?
 
unsigned len () const
 Count the number of stored items.
 
T * find (const decltype(T::m_key)&key)
 Query the cache without modifying its contents. More...
 
T * query (const decltype(T::m_key)&key)
 Query the cache, updating the recently-used list. More...
 

Private Member Functions

T * update (T **prev, T *item)
 

Private Attributes

T * m_free
 
T * m_list
 

Member Function Documentation

◆ find()

template<class T >
T* satcat5::util::LruCache< T >::find ( const decltype(T::m_key)&  key)
inline

Query the cache without modifying its contents.

Returns null pointer if no match is found.

Definition at line 73 of file lru_cache.h.

◆ query()

template<class T >
T* satcat5::util::LruCache< T >::query ( const decltype(T::m_key)&  key)
inline

Query the cache, updating the recently-used list.

Returns a new or existing entry matching the given key. If the cache is full, evicts the oldest entry to make room.

Definition at line 84 of file lru_cache.h.


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