Cachemere
Modular Caching Library for C++
constraint_memory.h
1 #ifndef CACHEMERE_CONSTRAINT_MEMORY_H
2 #define CACHEMERE_CONSTRAINT_MEMORY_H
3 
4 #include "cachemere/item.h"
5 
6 namespace cachemere::policy {
7 
13 template<typename Key, typename KeyHash, typename Value> class ConstraintMemory
14 {
15  using CacheItem = Item<Value>;
16 
17 public:
20  explicit ConstraintMemory(size_t max_memory);
21 
23  void clear();
24 
30  [[nodiscard]] bool can_add(const Key& key, const CacheItem& item);
31 
38  [[nodiscard]] bool can_replace(const Key& key, const CacheItem& old_item, const CacheItem& new_item);
39 
43  [[nodiscard]] bool is_satisfied();
44 
48  void update(size_t max_memory);
49 
52  [[nodiscard]] size_t memory() const;
53 
56  [[nodiscard]] size_t maximum_memory() const;
57 
62  void on_insert(const Key& key, const CacheItem& item);
63 
69  void on_update(const Key& key, const CacheItem& old_item, const CacheItem& new_item);
70 
75  void on_evict(const Key& key, const CacheItem& item);
76 
77 private:
78  size_t m_maximum_memory;
79  size_t m_memory = 0;
80 };
81 
82 } // namespace cachemere::policy
83 
84 #include "constraint_memory.hpp"
85 
86 #endif
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::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
Memory constraint.
Definition: constraint_memory.h: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::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