Cachemere
Modular Caching Library for C++
constraint_count.h
1 #ifndef CACHEMERE_CONSTRAINT_COUNT_H
2 #define CACHEMERE_CONSTRAINT_COUNT_H
3 
4 #include "cachemere/item.h"
5 
6 namespace cachemere::policy {
7 
13 template<typename Key, typename KeyHash, typename Value> class ConstraintCount
14 {
15  using CacheItem = Item<Value>;
16 
17 public:
18  explicit ConstraintCount(size_t maximum_count);
19 
21  void clear();
22 
28  [[nodiscard]] bool can_add(const Key& key, const CacheItem& item);
29 
36  [[nodiscard]] bool can_replace(const Key& key, const CacheItem& old_item, const CacheItem& new_item);
37 
41  [[nodiscard]] bool is_satisfied();
42 
46  void update(size_t maximum_count);
47 
52  void on_insert(const Key& key, const CacheItem& item);
53 
58  void on_evict(const Key& key, const CacheItem& item);
59 
62  [[nodiscard]] size_t count() const;
63 
66  [[nodiscard]] size_t maximum_count() const;
67 
68 private:
69  size_t m_maximum_count;
70  size_t m_count = 0;
71 };
72 
73 } // namespace cachemere::policy
74 
75 #include "constraint_count.hpp"
76 
77 #endif
cachemere::policy::ConstraintCount
Count constraint.
Definition: constraint_count.h:13
cachemere::policy::ConstraintCount::count
size_t count() const
Get the number of items currently in the cache.
Definition: constraint_count.hpp:48
cachemere::policy::ConstraintCount::clear
void clear()
Clears the policy.
Definition: constraint_count.hpp:8
cachemere::Item
A wrapper for items stored in the cache.
Definition: item.h:10
cachemere::policy::ConstraintCount::is_satisfied
bool is_satisfied()
Returns whether the constraint is satisfied.
Definition: constraint_count.hpp:27
cachemere::policy::ConstraintCount::can_replace
bool can_replace(const Key &key, const CacheItem &old_item, const CacheItem &new_item)
Determines whether an item already in cache can be updated.
Definition: constraint_count.hpp:19
cachemere::policy::ConstraintCount::on_evict
void on_evict(const Key &key, const CacheItem &item)
Eviction event handler.
Definition: constraint_count.hpp:42
cachemere::policy::ConstraintCount::maximum_count
size_t maximum_count() const
Get the maximum number of items allowed in cache.
Definition: constraint_count.hpp:53
cachemere::policy::ConstraintCount::on_insert
void on_insert(const Key &key, const CacheItem &item)
Insertion event handler.
Definition: constraint_count.hpp:37
cachemere::policy::ConstraintCount::update
void update(size_t maximum_count)
Update the cache constraint.
Definition: constraint_count.hpp:32
cachemere::policy::ConstraintCount::can_add
bool can_add(const Key &key, const CacheItem &item)
Determines whether an insertion candidate can be added into the cache.
Definition: constraint_count.hpp:13