Cachemere
Modular Caching Library for C++
Public Member Functions | List of all members
cachemere::policy::ConstraintMemory< Key, KeyHash, Value > Class Template Reference

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...
 

Detailed Description

template<typename Key, typename KeyHash, typename Value>
class cachemere::policy::ConstraintMemory< Key, KeyHash, Value >

Memory constraint.

Use this when the constraint of the cache should be how many bytes of memory it uses.

Template Parameters
KeyThe type of the keys used to identify items in the cache.
KeyHashThe type of the hasher used to hash item keys.
ValueThe type of the values stored in the cache.

Constructor & Destructor Documentation

◆ ConstraintMemory()

template<class K , class KH , class V >
cachemere::policy::ConstraintMemory< K, KH, V >::ConstraintMemory ( size_t  max_memory)
explicit

Constructor.

Parameters
max_memoryThe maximum amount of memory to be used by the cache, in bytes.

Member Function Documentation

◆ can_add()

template<typename Key , typename KeyHash , typename Value >
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.

Parameters
keyThe key of the insertion candidate.
itemThe candidate item.
Returns
Whether the item can be added in cache.

◆ can_replace()

template<typename Key , typename KeyHash , typename Value >
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.

Parameters
keyThe key to be updated.
old_itemThe current value of the key in cache.
new_itemThe value that would replace the current value.
Returns
Whether the item can be replaced.

◆ is_satisfied()

template<class K , class KH , class V >
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.

Returns
Whether the cache constraint is satisfied.

◆ maximum_memory()

template<class K , class KH , class V >
size_t cachemere::policy::ConstraintMemory< K, KH, V >::maximum_memory

Get the maximum amount of memory that can be used by the cache.

Returns
The maximum amount of memory, in bytes.

◆ memory()

template<class K , class KH , class V >
size_t cachemere::policy::ConstraintMemory< K, KH, V >::memory

Get the amount of memory currently used by the cache.

Returns
The current amount of memory used, in bytes.

◆ on_evict()

template<typename Key , typename KeyHash , typename Value >
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.

Parameters
keyThe key that was evicted.
itemThe item that was evicted.

◆ on_insert()

template<typename Key , typename KeyHash , typename Value >
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.

Parameters
keyThe key of the inserted item.
itemThe item that has been inserted in cache.

◆ on_update()

template<typename Key , typename KeyHash , typename Value >
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.

Parameters
keyThe key that has been updated in the cache.
old_itemThe old value for this key.
new_itemThe new value for this key

◆ update()

template<class K , class KH , class V >
void cachemere::policy::ConstraintMemory< K, KH, V >::update ( size_t  max_memory)

Update the cache constraint.

Sets a new maximum amount of memory.

Parameters
max_memoryThe new maximum amount of memory to be used by the cache.

The documentation for this class was generated from the following files: