Cachemere
Modular Caching Library for C++
insertion_tinylfu.h
1 #ifndef CACHEMERE_INSERTION_TINYLFU_H
2 #define CACHEMERE_INSERTION_TINYLFU_H
3 
4 #include <cstdint>
5 
6 #include "cachemere/item.h"
7 
8 #include "detail/bloom_filter.h"
9 #include "detail/counting_bloom_filter.h"
10 
11 namespace cachemere::policy {
12 
21 template<typename Key, typename KeyHash, typename Value> class InsertionTinyLFU
22 {
23 public:
25 
27  void clear();
28 
33  void on_cache_hit(const Key& key, const CacheItem& item);
34 
38  template<typename KeyType> void on_cache_miss(const KeyType& key);
39 
46  void set_cardinality(uint32_t cardinality);
47 
51  bool should_add(const Key& key);
52 
59  bool should_replace(const Key& victim, const Key& candidate);
60 
61 private:
62  const static uint32_t DEFAULT_CACHE_CARDINALITY = 2000;
63  detail::BloomFilter<KeyHash> m_gatekeeper{DEFAULT_CACHE_CARDINALITY}; // TODO: Investigate using cuckoo filter here instead.
64  detail::CountingBloomFilter<KeyHash> m_frequency_sketch{DEFAULT_CACHE_CARDINALITY}; // TODO: Replace with count-min sketch to get rid of cardinality param.
65 
66  uint32_t estimate_count_for_key(const Key& key) const;
67  void reset();
68 
69  template<typename KeyType> void touch_item(const KeyType& key);
70 };
71 
72 } // namespace cachemere::policy
73 
74 #include "insertion_tinylfu.hpp"
75 
76 #endif
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