Cachemere
Modular Caching Library for C++
eviction_lru.hpp
1 namespace cachemere::policy {
2 
3 template<class Key, class KeyHash, class Value>
4 EvictionLRU<Key, KeyHash, Value>::VictimIterator::VictimIterator(const KeyRefReverseIt& iterator) : m_iterator(iterator)
5 {
6 }
7 
8 template<class Key, class KeyHash, class Value> const Key& EvictionLRU<Key, KeyHash, Value>::VictimIterator::operator*() const
9 {
10  return *m_iterator;
11 }
12 
13 template<class Key, class KeyHash, class Value> auto EvictionLRU<Key, KeyHash, Value>::VictimIterator::operator++() -> VictimIterator&
14 {
15  ++m_iterator;
16  return *this;
17 }
18 
19 template<class Key, class KeyHash, class Value> auto EvictionLRU<Key, KeyHash, Value>::VictimIterator::operator++(int) -> VictimIterator
20 {
21  return (*this)++;
22 }
23 
24 template<class Key, class KeyHash, class Value> bool EvictionLRU<Key, KeyHash, Value>::VictimIterator::operator==(const VictimIterator& other) const
25 {
26  return m_iterator == other.m_iterator;
27 }
28 
29 template<class Key, class KeyHash, class Value> bool EvictionLRU<Key, KeyHash, Value>::VictimIterator::operator!=(const VictimIterator& other) const
30 {
31  return m_iterator != other.m_iterator;
32 }
33 
34 template<class Key, class KeyHash, class Value> void EvictionLRU<Key, KeyHash, Value>::clear()
35 {
36  m_keys.clear();
37  m_nodes.clear();
38 }
39 
40 template<class Key, class KeyHash, class Value> void EvictionLRU<Key, KeyHash, Value>::on_insert(const Key& key, const CacheItem& /* item */)
41 {
42  assert(m_nodes.find(std::ref(key)) == m_nodes.end()); // Validate the item is not already in policy.
43 
44  m_keys.emplace_front(std::ref(key));
45  m_nodes.emplace(std::ref(key), m_keys.begin());
46 }
47 
48 template<class Key, class KeyHash, class Value>
49 void EvictionLRU<Key, KeyHash, Value>::on_update(const Key& key, const CacheItem& /* old_item */, const CacheItem& new_item)
50 {
51  on_cache_hit(key, new_item);
52 }
53 
54 template<class Key, class KeyHash, class Value> void EvictionLRU<Key, KeyHash, Value>::on_cache_hit(const Key& key, const CacheItem& /* item */)
55 {
56  auto node_it = m_nodes.find(key);
57  if (node_it != m_nodes.end()) {
58  // No need to shuffle stuff around if item is already the hottest item in cache.
59  if (node_it->second != m_keys.begin()) {
60  m_keys.splice(m_keys.begin(), m_keys, node_it->second);
61  }
62  } else {
63  // If this is tripped, there is a disconnect between the contents of the policy and the contents of the cache.
64  assert(false);
65  }
66 }
67 
68 template<class Key, class KeyHash, class Value> void EvictionLRU<Key, KeyHash, Value>::on_evict(const Key& key, const CacheItem& /* item */)
69 {
70  assert(!m_nodes.empty());
71  assert(!m_keys.empty());
72 
73  if (m_keys.back().get() == key) {
74  m_nodes.erase(key);
75  m_keys.pop_back();
76  } else {
77  auto it = m_nodes.find(key);
78  assert(it != m_nodes.end());
79  m_nodes.erase(it);
80  }
81 }
82 
83 template<class Key, class KeyHash, class Value> auto EvictionLRU<Key, KeyHash, Value>::victim_begin() const -> VictimIterator
84 {
85  return VictimIterator{m_keys.rbegin()};
86 }
87 
88 template<class Key, class KeyHash, class Value> auto EvictionLRU<Key, KeyHash, Value>::victim_end() const -> VictimIterator
89 {
90  return VictimIterator{m_keys.rend()};
91 }
92 
93 } // namespace cachemere::policy
cachemere::policy::EvictionLRU::on_insert
void on_insert(const Key &key, const CacheItem &item)
Insertion event handler.
Definition: eviction_lru.hpp:40
cachemere::policy::EvictionLRU::on_cache_hit
void on_cache_hit(const Key &key, const CacheItem &item)
Cache hit event handler.
Definition: eviction_lru.hpp:54
cachemere::policy::EvictionLRU::VictimIterator
Iterator for iterating over cache items in the order they should be evicted.
Definition: eviction_lru.h:34
cachemere::policy::EvictionLRU::clear
void clear()
Clears the policy.
Definition: eviction_lru.hpp:34
cachemere::Item
A wrapper for items stored in the cache.
Definition: item.h:10
cachemere::policy::EvictionLRU::on_update
void on_update(const Key &key, const CacheItem &old_item, const CacheItem &new_item)
Update event handler.
Definition: eviction_lru.hpp:49
cachemere::policy::EvictionLRU::victim_begin
VictimIterator victim_begin() const
Get an iterator to the first item that should be evicted.
Definition: eviction_lru.hpp:83
cachemere::policy::EvictionLRU::victim_end
VictimIterator victim_end() const
Get an end iterator.
Definition: eviction_lru.hpp:88
cachemere::policy::EvictionLRU::on_evict
void on_evict(const Key &key, const CacheItem &item)
Eviction event handler.
Definition: eviction_lru.hpp:68