Cachemere
Modular Caching Library for C++
|
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... | |
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.
Key | The type of the keys used to identify items in the cache. |
KeyHash | The type of the hasher used to hash item keys. |
Value | The type of the values stored in the cache. |
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.
key | The key that has been hit. |
item | The item that has been hit. |
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.
key | The key that was evicted. |
item | The item that was evicted. |
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.
key | The key of the inserted item. |
item | The item that has been inserted in the cache. |
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.
key | The key that has been updated in the cache. |
old_item | The old value for this key. |
new_item | The new value for this key |
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.
size | The maximum number of items in the protected segment. |
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.
auto cachemere::policy::EvictionSegmentedLRU< Key, KeyHash, Value >::victim_end |
Get an end iterator.