Cachemere
Modular Caching Library for C++
eviction_lru.h
1 #ifndef CACHEMERE_EVICTION_LRU
2 #define CACHEMERE_EVICTION_LRU
3 
4 #include <cassert>
5 #include <functional>
6 #include <list>
7 #include <map>
8 
9 #include <absl/container/flat_hash_map.h>
10 
11 #include "cachemere/item.h"
12 
13 namespace cachemere::policy {
14 
22 template<typename Key, typename KeyHash, typename Value> class EvictionLRU
23 {
24 private:
25  using KeyRef = std::reference_wrapper<const Key>;
26  using KeyRefIt = typename std::list<KeyRef>::iterator;
27  using KeyRefMap = absl::flat_hash_map<KeyRef, KeyRefIt, KeyHash, std::equal_to<Key>>;
28 
29 public:
31 
35  {
36  public:
37  using KeyRefReverseIt = typename std::list<KeyRef>::const_reverse_iterator;
38 
39  VictimIterator(const KeyRefReverseIt& p_Iterator);
40 
41  const Key& operator*() const;
42  VictimIterator& operator++();
43  VictimIterator operator++(int);
44  bool operator==(const VictimIterator& other) const;
45  bool operator!=(const VictimIterator& other) const;
46 
47  private:
48  KeyRefReverseIt m_iterator;
49  };
50 
52  void clear();
53 
58  void on_insert(const Key& key, const CacheItem& item);
59 
65  void on_update(const Key& key, const CacheItem& old_item, const CacheItem& new_item);
66 
71  void on_cache_hit(const Key& key, const CacheItem& item);
72 
77  void on_evict(const Key& key, const CacheItem& item);
78 
84  [[nodiscard]] VictimIterator victim_begin() const;
85 
88  [[nodiscard]] VictimIterator victim_end() const;
89 
90 private:
91  std::list<KeyRef> m_keys;
92  KeyRefMap m_nodes;
93 };
94 
95 } // namespace cachemere::policy
96 
97 #include "eviction_lru.hpp"
98 
99 #endif
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
Least Recently Used (LRU) eviction policy.
Definition: eviction_lru.h:22
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