|
SatCat5
|
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:
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>
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 |
|
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.
|
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.