Cachemere
Modular Caching Library for C++
eviction_gdsf.h
1 #ifndef CACHEMERE_EVICTION_GDSF
2 #define CACHEMERE_EVICTION_GDSF
3 
4 #include <algorithm>
5 
6 #include <absl/container/btree_map.h>
7 
8 #include "cachemere/item.h"
9 #include "cachemere/policy/detail/counting_bloom_filter.h"
10 
11 namespace cachemere::policy {
12 
22 // The cost must not exceed 2^64 and should ideally be quite a bit below this limit since the cost of the item is added to the
23 // policy's internal 64-bit clock on every insertion.
24 template<typename Key, typename KeyHash, typename Value, typename Cost> class EvictionGDSF
25 {
26 private:
27  using KeyRef = std::reference_wrapper<const Key>;
28 
29  struct PriorityEntry {
30  PriorityEntry(KeyRef key, double coefficient);
31 
32  bool operator<(const PriorityEntry& other) const;
33 
34  KeyRef m_key;
35  double m_h_coefficient;
36  };
37 
38  using PrioritySet = std::multiset<PriorityEntry>;
39  using PrioritySetIt = typename PrioritySet::const_iterator;
40 
41 public:
42  using CacheItem = Item<Value>;
43 
47  {
48  public:
49  VictimIterator(PrioritySetIt iterator);
50 
51  const Key& operator*() const;
52  VictimIterator& operator++();
53  VictimIterator operator++(int);
54  bool operator==(const VictimIterator& other) const;
55  bool operator!=(const VictimIterator& other) const;
56 
57  private:
58  PrioritySetIt m_iterator;
59  };
60 
62  void clear();
63 
70  void set_cardinality(uint32_t cardinality);
71 
76  void on_insert(const Key& key, const CacheItem& item);
77 
83  void on_update(const Key& key, const CacheItem& old_item, const CacheItem& new_item);
84 
89  void on_cache_hit(const Key& key, const CacheItem& item);
90 
95  void on_evict(const Key& key, const CacheItem& item);
96 
99  [[nodiscard]] VictimIterator victim_begin() const;
100 
103  [[nodiscard]] VictimIterator victim_end() const;
104 
105 private:
106  using IteratorMap = absl::btree_map<KeyRef, PrioritySetIt, std::less<const Key>>;
107 
108  const static uint32_t DEFAULT_CACHE_CARDINALITY = 2000; // The expected cache cardinality, for the counting bloom filter.
109  mutable Cost m_measure_cost; // The functor to measure the cost metric of cached items.
110  detail::CountingBloomFilter<KeyHash> m_frequency_sketch{DEFAULT_CACHE_CARDINALITY}; // TODO: Replace with a count-min sketch to get rid of cardinality
111 
112  PrioritySet m_priority_set; // A multiset of keys sorted by H-coefficients in ascending order.
113 
114  IteratorMap m_iterator_map; // A map of keys pointing to the corresponding iterator in the priority set. This is necessary because since PrioritySet uses
115  // coefficients to compare items to one another, we can't test for key membership directly.
116 
117  uint64_t m_clock{0};
118 
119  [[nodiscard]] double get_h_coefficient(const Key& key, const CacheItem& item) const noexcept;
120 };
121 
122 } // namespace cachemere::policy
123 
124 #include "eviction_gdsf.hpp"
125 
126 #endif
cachemere::policy::EvictionGDSF::on_cache_hit
void on_cache_hit(const Key &key, const CacheItem &item)
Cache hit event handler.
Definition: eviction_gdsf.hpp:74
cachemere::policy::EvictionGDSF::on_evict
void on_evict(const Key &key, const CacheItem &item)
Eviction event handler.
Definition: eviction_gdsf.hpp:86
cachemere::policy::EvictionGDSF::set_cardinality
void set_cardinality(uint32_t cardinality)
Set the cardinality of the policy.
Definition: eviction_gdsf.hpp:55
cachemere::policy::detail::CountingBloomFilter< KeyHash >
cachemere::policy::EvictionGDSF::on_insert
void on_insert(const Key &key, const CacheItem &item)
Insertion event handler.
Definition: eviction_gdsf.hpp:60
cachemere::Item
A wrapper for items stored in the cache.
Definition: item.h:10
cachemere::policy::EvictionGDSF::victim_end
VictimIterator victim_end() const
Get an end iterator.
Definition: eviction_gdsf.hpp:105
cachemere::policy::EvictionGDSF
Greedy-Dual-Size-Frequency (GDSF) eviction policy.
Definition: eviction_gdsf.h:24
cachemere::policy::EvictionGDSF::clear
void clear()
Clears the policy.
Definition: eviction_gdsf.hpp:48
cachemere::policy::EvictionGDSF::victim_begin
VictimIterator victim_begin() const
Get an iterator to the first item that should be evicted.
Definition: eviction_gdsf.hpp:100
cachemere::policy::EvictionGDSF::VictimIterator
Iterator for iterating over cache items in the order they should be evicted.
Definition: eviction_gdsf.h:46
cachemere::policy::EvictionGDSF::on_update
void on_update(const Key &key, const CacheItem &old_item, const CacheItem &new_item)
Update event handler.
Definition: eviction_gdsf.hpp:69