Cachemere
Modular Caching Library for C++
|
Memory constraint. More...
#include <constraint_memory.h>
Public Member Functions | |
ConstraintMemory (size_t max_memory) | |
Constructor. More... | |
void | clear () |
Clears the policy. | |
bool | can_add (const Key &key, const CacheItem &item) |
Determines whether an insertion candidate can be added into the cache. More... | |
bool | can_replace (const Key &key, const CacheItem &old_item, const CacheItem &new_item) |
Determines whether an item already in cache can be updated. More... | |
bool | is_satisfied () |
Returns whether the constraint is satisfied. More... | |
void | update (size_t max_memory) |
Update the cache constraint. More... | |
size_t | memory () const |
Get the amount of memory currently used by the cache. More... | |
size_t | maximum_memory () const |
Get the maximum amount of memory that can be used by the cache. More... | |
void | on_insert (const Key &key, const CacheItem &item) |
Insertion event handler. More... | |
void | on_update (const Key &key, const CacheItem &old_item, const CacheItem &new_item) |
Update event handler. More... | |
void | on_evict (const Key &key, const CacheItem &item) |
Eviction event handler. More... | |
Memory constraint.
Use this when the constraint of the cache should be how many bytes of memory it uses.
Key | The type of the keys used to identify items in the cache. |
KeyHash | The type of the hasher used to hash item keys. |
Value | The type of the values stored in the cache. |
|
explicit |
Constructor.
max_memory | The maximum amount of memory to be used by the cache, in bytes. |
bool cachemere::policy::ConstraintMemory< K, KH, V >::can_add | ( | const Key & | key, |
const CacheItem & | item | ||
) |
Determines whether an insertion candidate can be added into the cache.
That is, whether the constraint would still be satisfied after inserting the candidate.
key | The key of the insertion candidate. |
item | The candidate item. |
bool cachemere::policy::ConstraintMemory< K, KH, V >::can_replace | ( | const Key & | key, |
const CacheItem & | old_item, | ||
const CacheItem & | new_item | ||
) |
Determines whether an item already in cache can be updated.
That is, whether the key can be updated to the new value while still satisfying the constraint.
key | The key to be updated. |
old_item | The current value of the key in cache. |
new_item | The value that would replace the current value. |
bool cachemere::policy::ConstraintMemory< K, KH, V >::is_satisfied |
Returns whether the constraint is satisfied.
Used by the cache after a constraint update to compute how many items should be evicted, if any.
size_t cachemere::policy::ConstraintMemory< K, KH, V >::maximum_memory |
Get the maximum amount of memory that can be used by the cache.
size_t cachemere::policy::ConstraintMemory< K, KH, V >::memory |
Get the amount of memory currently used by the cache.
void cachemere::policy::ConstraintMemory< K, KH, V >::on_evict | ( | const Key & | key, |
const CacheItem & | item | ||
) |
Eviction event handler.
Removes the item at the back of the list - ensuring it has the provided key.
key | The key that was evicted. |
item | The item that was evicted. |
void cachemere::policy::ConstraintMemory< K, KH, V >::on_insert | ( | const Key & | key, |
const CacheItem & | item | ||
) |
Insertion event handler.
Adds the size of this item to the amount of memory used.
key | The key of the inserted item. |
item | The item that has been inserted in cache. |
void cachemere::policy::ConstraintMemory< K, KH, V >::on_update | ( | const Key & | key, |
const CacheItem & | old_item, | ||
const CacheItem & | new_item | ||
) |
Update event handler.
Updates the amount of memory used to reflect the size difference between the old and the new value.
key | The key that has been updated in the cache. |
old_item | The old value for this key. |
new_item | The new value for this key |
void cachemere::policy::ConstraintMemory< K, KH, V >::update | ( | size_t | max_memory | ) |
Update the cache constraint.
Sets a new maximum amount of memory.
max_memory | The new maximum amount of memory to be used by the cache. |