Cachemere
Modular Caching Library for C++
eviction_segmented_lru.h
1 #ifndef CACHEMERE_EVICTION_SEGMENTED_LRU_H
2 #define CACHEMERE_EVICTION_SEGMENTED_LRU_H
3 
4 #include <list>
5 
6 #include <absl/container/flat_hash_map.h>
7 
8 #include "cachemere/item.h"
9 
10 namespace cachemere::policy {
11 
22 template<typename Key, typename KeyHash, typename Value> class EvictionSegmentedLRU
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 
33  // evicted.
35  {
36  public:
37  using KeyRefReverseIt = typename std::list<KeyRef>::const_reverse_iterator;
38 
39  VictimIterator(const KeyRefReverseIt& probation_iterator, const KeyRefReverseIt& probation_end_iterator, const KeyRefReverseIt& protected_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_probation_iterator;
49  KeyRefReverseIt m_probation_end_iterator;
50  KeyRefReverseIt m_protected_iterator;
51  bool m_done_with_probation;
52  };
53 
55  void clear();
56 
59  void set_protected_segment_size(size_t size);
60 
65  void on_insert(const Key& key, const CacheItem& item);
66 
74  void on_update(const Key& key, const CacheItem& old_item, const CacheItem& new_item);
75 
82  void on_cache_hit(const Key& key, const CacheItem& item);
83 
88  void on_evict(const Key& key, const CacheItem& item);
89 
95  [[nodiscard]] VictimIterator victim_begin() const;
96 
99  [[nodiscard]] VictimIterator victim_end() const;
100 
101 private:
102  size_t m_protected_segment_size;
103 
104  std::list<KeyRef> m_probation_list;
105  KeyRefMap m_probation_nodes;
106 
107  std::list<KeyRef> m_protected_list;
108  KeyRefMap m_protected_nodes;
109 
110  bool move_to_protected(const Key& key);
111  bool pop_to_probation();
112 };
113 
114 } // namespace cachemere::policy
115 
116 #include "eviction_segmented_lru.hpp"
117 
118 #endif
cachemere::policy::EvictionSegmentedLRU::on_insert
void on_insert(const Key &key, const CacheItem &item)
Insertion event handler.
Definition: eviction_segmented_lru.hpp:63
cachemere::Item
A wrapper for items stored in the cache.
Definition: item.h:10
cachemere::policy::EvictionSegmentedLRU::on_update
void on_update(const Key &key, const CacheItem &old_item, const CacheItem &new_item)
Update event handler.
Definition: eviction_segmented_lru.hpp:72
cachemere::policy::EvictionSegmentedLRU::clear
void clear()
Clears the policy.
Definition: eviction_segmented_lru.hpp:49
cachemere::policy::EvictionSegmentedLRU::set_protected_segment_size
void set_protected_segment_size(size_t size)
Set the maximum number of items in the protected LRU segment.
Definition: eviction_segmented_lru.hpp:58
cachemere::policy::EvictionSegmentedLRU::on_cache_hit
void on_cache_hit(const Key &key, const CacheItem &item)
Cache hit event handler.
Definition: eviction_segmented_lru.hpp:77
cachemere::policy::EvictionSegmentedLRU::victim_end
VictimIterator victim_end() const
Get an end iterator.
Definition: eviction_segmented_lru.hpp:125
cachemere::policy::EvictionSegmentedLRU::VictimIterator
Iterator for iterating over cache items in the order they should be.
Definition: eviction_segmented_lru.h:34
cachemere::policy::EvictionSegmentedLRU::victim_begin
VictimIterator victim_begin() const
Get an iterator to the first item that should be evicted.
Definition: eviction_segmented_lru.hpp:120
cachemere::policy::EvictionSegmentedLRU
Segmented Least Recently Used (S-LRU) Eviction Policy.
Definition: eviction_segmented_lru.h:22
cachemere::policy::EvictionSegmentedLRU::on_evict
void on_evict(const Key &key, const CacheItem &item)
Eviction event handler.
Definition: eviction_segmented_lru.hpp:104