Cachemere
Modular Caching Library for C++
constraint_count.hpp
1 namespace cachemere::policy {
2 
3 template<class K, class KH, class V> ConstraintCount<K, KH, V>::ConstraintCount(size_t maximum_count)
4 {
5  update(maximum_count);
6 }
7 
8 template<class K, class KH, class V> void ConstraintCount<K, KH, V>::clear()
9 {
10  m_count = 0;
11 }
12 
13 template<class K, class KH, class V> bool ConstraintCount<K, KH, V>::can_add(const K& /* key */, const CacheItem& /* item */)
14 {
15  return m_count < m_maximum_count;
16 }
17 
18 template<class K, class KH, class V>
19 bool ConstraintCount<K, KH, V>::can_replace(const K& /* key */, const CacheItem& /* old_item */, const CacheItem& /* new_item */)
20 {
21  assert(m_count > 0);
22 
23  // Replacement doesn't change the count, so it's always allowed.
24  return true;
25 }
26 
27 template<class K, class KH, class V> bool ConstraintCount<K, KH, V>::is_satisfied()
28 {
29  return m_count <= m_maximum_count;
30 }
31 
32 template<class K, class KH, class V> void ConstraintCount<K, KH, V>::update(size_t maximum_count)
33 {
34  m_maximum_count = maximum_count;
35 }
36 
37 template<class K, class KH, class V> void ConstraintCount<K, KH, V>::on_insert(const K& /* key */, const CacheItem& /* item */)
38 {
39  ++m_count;
40 }
41 
42 template<class K, class KH, class V> void ConstraintCount<K, KH, V>::on_evict(const K& /* key */, const CacheItem& /* item */)
43 {
44  assert(m_count > 0);
45  --m_count;
46 }
47 
48 template<class K, class KH, class V> size_t ConstraintCount<K, KH, V>::count() const
49 {
50  return m_count;
51 }
52 
53 template<class K, class KH, class V> size_t ConstraintCount<K, KH, V>::maximum_count() const
54 {
55  return m_maximum_count;
56 }
57 
58 } // namespace cachemere::policy
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