Cachemere
Modular Caching Library for C++
constraint_memory.hpp
1 namespace cachemere::policy {
2 
3 template<class K, class KH, class V> ConstraintMemory<K, KH, V>::ConstraintMemory(size_t max_memory)
4 {
5  update(max_memory);
6 }
7 
8 template<class K, class KH, class V> void ConstraintMemory<K, KH, V>::clear()
9 {
10  m_memory = 0;
11 }
12 
13 template<class K, class KH, class V> bool ConstraintMemory<K, KH, V>::can_add(const K& /* key */, const CacheItem& item)
14 {
15  return (m_memory + item.m_total_size) <= m_maximum_memory;
16 }
17 
18 template<class K, class KH, class V> bool ConstraintMemory<K, KH, V>::can_replace(const K& /* key */, const CacheItem& old_item, const CacheItem& new_item)
19 {
20  assert(old_item.m_key_size == new_item.m_key_size); // Key size *really* shouldn't have changed since the key is supposed to be const.
21  return ((m_memory - old_item.m_value_size) + new_item.m_value_size) <= m_maximum_memory;
22 }
23 
24 template<class K, class KH, class V> bool ConstraintMemory<K, KH, V>::is_satisfied()
25 {
26  return m_memory <= m_maximum_memory;
27 }
28 
29 template<class K, class KH, class V> void ConstraintMemory<K, KH, V>::update(size_t max_memory)
30 {
31  m_maximum_memory = max_memory;
32 }
33 
34 template<class K, class KH, class V> size_t ConstraintMemory<K, KH, V>::memory() const
35 {
36  return m_memory;
37 }
38 
39 template<class K, class KH, class V> size_t ConstraintMemory<K, KH, V>::maximum_memory() const
40 {
41  return m_maximum_memory;
42 }
43 
44 template<class K, class KH, class V> void ConstraintMemory<K, KH, V>::on_insert(const K& /* key */, const CacheItem& item)
45 {
46  m_memory += item.m_total_size;
47  assert(m_memory <= m_maximum_memory);
48 }
49 
50 template<class K, class KH, class V> void ConstraintMemory<K, KH, V>::on_update(const K& /* key */, const CacheItem& old_item, const CacheItem& new_item)
51 {
52  m_memory -= old_item.m_value_size;
53  m_memory += new_item.m_value_size;
54  assert(m_memory <= m_maximum_memory);
55 }
56 
57 template<class K, class KH, class V> void ConstraintMemory<K, KH, V>::on_evict(const K& /* key */, const CacheItem& item)
58 {
59  assert(item.m_total_size <= m_memory);
60  m_memory -= item.m_total_size;
61 }
62 
63 } // namespace cachemere::policy
cachemere::Item::m_value_size
size_t m_value_size
The size of the item.
Definition: item.h:26
cachemere::policy::ConstraintMemory::clear
void clear()
Clears the policy.
Definition: constraint_memory.hpp:8
cachemere::policy::ConstraintMemory::ConstraintMemory
ConstraintMemory(size_t max_memory)
Constructor.
Definition: constraint_memory.hpp:3
cachemere::policy::ConstraintMemory::update
void update(size_t max_memory)
Update the cache constraint.
Definition: constraint_memory.hpp:29
cachemere::policy::ConstraintMemory::on_update
void on_update(const Key &key, const CacheItem &old_item, const CacheItem &new_item)
Update event handler.
Definition: constraint_memory.hpp:50
cachemere::policy::ConstraintMemory::is_satisfied
bool is_satisfied()
Returns whether the constraint is satisfied.
Definition: constraint_memory.hpp:24
cachemere::Item
A wrapper for items stored in the cache.
Definition: item.h:10
cachemere::policy::ConstraintMemory::memory
size_t memory() const
Get the amount of memory currently used by the cache.
Definition: constraint_memory.hpp:34
cachemere::Item::m_key_size
size_t m_key_size
The size of the key.
Definition: item.h:23
cachemere::policy::ConstraintMemory::can_add
bool can_add(const Key &key, const CacheItem &item)
Determines whether an insertion candidate can be added into the cache.
Definition: constraint_memory.hpp:13
cachemere::policy::ConstraintMemory::maximum_memory
size_t maximum_memory() const
Get the maximum amount of memory that can be used by the cache.
Definition: constraint_memory.hpp:39
cachemere::Item::m_total_size
size_t m_total_size
The total size of the item (m_key_size + m_value_size)
Definition: item.h:28
cachemere::policy::ConstraintMemory::on_insert
void on_insert(const Key &key, const CacheItem &item)
Insertion event handler.
Definition: constraint_memory.hpp:44
cachemere::policy::ConstraintMemory::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_memory.hpp:18
cachemere::policy::ConstraintMemory::on_evict
void on_evict(const Key &key, const CacheItem &item)
Eviction event handler.
Definition: constraint_memory.hpp:57