Cachemere
Modular Caching Library for C++
Classes | Public Types | Public Member Functions | List of all members
cachemere::policy::EvictionLRU< Key, KeyHash, Value > Class Template Reference

Least Recently Used (LRU) eviction policy. More...

#include <eviction_lru.h>

Classes

class  VictimIterator
 Iterator for iterating over cache items in the order they should be evicted. More...
 

Public Types

using CacheItem = cachemere::Item< Value >
 

Public Member Functions

void clear ()
 Clears the policy.
 
void on_insert (const Key &key, const CacheItem &item)
 Insertion event handler. More...
 
void on_update (const Key &key, const CacheItem &old_item, const CacheItem &new_item)
 Update event handler. More...
 
void on_cache_hit (const Key &key, const CacheItem &item)
 Cache hit event handler. More...
 
void on_evict (const Key &key, const CacheItem &item)
 Eviction event handler. More...
 
VictimIterator victim_begin () const
 Get an iterator to the first item that should be evicted. More...
 
VictimIterator victim_end () const
 Get an end iterator. More...
 

Detailed Description

template<typename Key, typename KeyHash, typename Value>
class cachemere::policy::EvictionLRU< Key, KeyHash, Value >

Least Recently Used (LRU) eviction policy.

Implemented internally using a linked list. The keys are ordered from most-recently used to least-recently used. Only stores references to keys kept alive by the cache.

Template Parameters
KeyThe type of the keys used to identify items in the cache.
KeyHashThe type of the hasher used to hash item keys.
ValueThe type of the values stored in the cache.

Member Function Documentation

◆ on_cache_hit()

template<class Key , class KeyHash , class Value >
void cachemere::policy::EvictionLRU< Key, KeyHash, Value >::on_cache_hit ( const Key &  key,
const CacheItem item 
)

Cache hit event handler.

Moves the provided item at the front of the list.

Parameters
keyThe key that has been hit.
itemThe item that has been hit.

◆ on_evict()

template<class Key , class KeyHash , class Value >
void cachemere::policy::EvictionLRU< Key, KeyHash, Value >::on_evict ( const Key &  key,
const CacheItem item 
)

Eviction event handler.

Removes the item at the back of the list - ensuring it has the provided key.

Parameters
keyThe key that was evicted.
itemThe item that was evicted.

◆ on_insert()

template<class Key , class KeyHash , class Value >
void cachemere::policy::EvictionLRU< Key, KeyHash, Value >::on_insert ( const Key &  key,
const CacheItem item 
)

Insertion event handler.

Inserts the provided item at the front of the list.

Parameters
keyThe key of the inserted item.
itemThe item that has been inserted in cache.

◆ on_update()

template<class Key , class KeyHash , class Value >
void cachemere::policy::EvictionLRU< Key, KeyHash, Value >::on_update ( const Key &  key,
const CacheItem old_item,
const CacheItem new_item 
)

Update event handler.

Moves the provided item to the front of the list.

Parameters
keyThe key that has been updated in the cache.
old_itemThe old value for this key.
new_itemThe new value for this key

◆ victim_begin()

template<class Key , class KeyHash , class Value >
auto cachemere::policy::EvictionLRU< Key, KeyHash, Value >::victim_begin

Get an iterator to the first item that should be evicted.

Considering that the keys are ordered internally from most-recently used to least-recently used, this iterator will effectively walk the internal structure backwards.

Returns
An item iterator.

◆ victim_end()

template<class Key , class KeyHash , class Value >
auto cachemere::policy::EvictionLRU< Key, KeyHash, Value >::victim_end

Get an end iterator.

Returns
The end iterator.

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