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

Segmented Least Recently Used (S-LRU) Eviction Policy. More...

#include <eviction_segmented_lru.h>

Classes

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

Public Types

using CacheItem = cachemere::Item< Value >
 

Public Member Functions

void clear ()
 Clears the policy.
 
void set_protected_segment_size (size_t size)
 Set the maximum number of items in the protected LRU segment. More...
 
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::EvictionSegmentedLRU< Key, KeyHash, Value >

Segmented Least Recently Used (S-LRU) Eviction Policy.

Segmented LRU is really similar to LRU. The main difference is that a segmented LRU policy has two separate LRU segments. Items are initially inserted in a probation segment. When items that are on probation are accessed, they are promoted from the protected segment. If the protected segment is full, the item that was least-recently accessed is downgraded to the probation segment. This architecture has the effect of reducing cache churn and keeping "good" items in cache a bit longer.

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::EvictionSegmentedLRU< Key, KeyHash, Value >::on_cache_hit ( const Key &  key,
const CacheItem item 
)

Cache hit event handler.

If the item is in the probation segment, it is moved to the protected segment. If the item is in the protected segment, it is moved to the front of the protected segment.

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::EvictionSegmentedLRU< Key, KeyHash, Value >::on_evict ( const Key &  key,
const CacheItem item 
)

Eviction event handler.

Removes the item from the segment it belongs to.

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

◆ on_insert()

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

Insertion event handler.

Inserts the provided item at the front of the probation segment.

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

◆ on_update()

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

Update event handler.

If the item is in the probation segment, it is moved to the protected segment. If the item is in the protected segment, it is moved to the front of the protected segment.

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

◆ set_protected_segment_size()

template<class Key , class KeyHash , class Value >
void cachemere::policy::EvictionSegmentedLRU< Key, KeyHash, Value >::set_protected_segment_size ( size_t  size)

Set the maximum number of items in the protected LRU segment.

Parameters
sizeThe maximum number of items in the protected segment.

◆ victim_begin()

template<class Key , class KeyHash , class Value >
auto cachemere::policy::EvictionSegmentedLRU< 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::EvictionSegmentedLRU< Key, KeyHash, Value >::victim_end

Get an end iterator.

Returns
The end iterator.

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