Cachemere
Modular Caching Library for C++
insertion_tinylfu.hpp
1 namespace cachemere::policy {
2 
3 template<class Key, class KeyHash, class Value> void InsertionTinyLFU<Key, KeyHash, Value>::clear()
4 {
5  m_gatekeeper.clear();
6  m_frequency_sketch.clear();
7 }
8 
9 template<class Key, class KeyHash, class Value> void InsertionTinyLFU<Key, KeyHash, Value>::on_cache_hit(const Key& key, const CacheItem& /* item */)
10 {
11  touch_item(key);
12 }
13 
14 template<class Key, class KeyHash, class Value> template<class KeyType> void InsertionTinyLFU<Key, KeyHash, Value>::on_cache_miss(const KeyType& key)
15 {
16  touch_item(key);
17 }
18 
19 template<class Key, class KeyHash, class Value> void InsertionTinyLFU<Key, KeyHash, Value>::set_cardinality(uint32_t cardinality)
20 {
21  m_gatekeeper = detail::BloomFilter<KeyHash>(cardinality);
22  m_frequency_sketch = detail::CountingBloomFilter<KeyHash>(cardinality);
23 }
24 
25 template<class Key, class KeyHash, class Value> bool InsertionTinyLFU<Key, KeyHash, Value>::should_add(const Key& key)
26 {
27  return m_gatekeeper.maybe_contains(key);
28 }
29 
30 template<class Key, class KeyHash, class Value> bool InsertionTinyLFU<Key, KeyHash, Value>::should_replace(const Key& victim, const Key& candidate)
31 {
32  return estimate_count_for_key(candidate) > estimate_count_for_key(victim);
33 }
34 
35 template<class Key, class KeyHash, class Value> uint32_t InsertionTinyLFU<Key, KeyHash, Value>::estimate_count_for_key(const Key& key) const
36 {
37  uint32_t sketch_estimation = m_frequency_sketch.estimate(key);
38  if (m_gatekeeper.maybe_contains(key)) {
39  ++sketch_estimation;
40  }
41 
42  return sketch_estimation;
43 }
44 
45 template<class Key, class KeyHash, class Value> void InsertionTinyLFU<Key, KeyHash, Value>::reset()
46 {
47  m_gatekeeper.clear();
48  m_frequency_sketch.decay();
49 }
50 
51 template<class Key, class KeyHash, class Value> template<class KeyType> void InsertionTinyLFU<Key, KeyHash, Value>::touch_item(const KeyType& key)
52 {
53  if (m_gatekeeper.maybe_contains(key)) {
54  m_frequency_sketch.add(key);
55  if (m_frequency_sketch.estimate(key) > m_frequency_sketch.cardinality()) {
56  reset();
57  }
58  } else {
59  m_gatekeeper.add(key);
60  }
61 }
62 
63 } // namespace cachemere::policy
cachemere::policy::InsertionTinyLFU::on_cache_hit
void on_cache_hit(const Key &key, const CacheItem &item)
Cache hit event handler.
Definition: insertion_tinylfu.hpp:9
cachemere::policy::detail::BloomFilter< KeyHash >
cachemere::policy::InsertionTinyLFU::clear
void clear()
Clears the policy.
Definition: insertion_tinylfu.hpp:3
cachemere::policy::detail::CountingBloomFilter< KeyHash >
cachemere::Item
A wrapper for items stored in the cache.
Definition: item.h:10
cachemere::policy::InsertionTinyLFU
Tiny Least Frequently Used (TinyLFU) insertion policy.
Definition: insertion_tinylfu.h:21
cachemere::policy::InsertionTinyLFU::should_add
bool should_add(const Key &key)
Determines whether a given key should be inserted into the cache.
Definition: insertion_tinylfu.hpp:25
cachemere::policy::InsertionTinyLFU::on_cache_miss
void on_cache_miss(const KeyType &key)
Cache miss event handler.
Definition: insertion_tinylfu.hpp:14
cachemere::policy::InsertionTinyLFU::should_replace
bool should_replace(const Key &victim, const Key &candidate)
Determines whether a given victim should be replaced by a given candidate.
Definition: insertion_tinylfu.hpp:30
cachemere::policy::InsertionTinyLFU::set_cardinality
void set_cardinality(uint32_t cardinality)
Set the cardinality of the policy.
Definition: insertion_tinylfu.hpp:19