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

Count constraint. More...

#include <constraint_count.h>

Public Member Functions

 ConstraintCount (size_t maximum_count)
 
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 maximum_count)
 Update the cache constraint. More...
 
void on_insert (const Key &key, const CacheItem &item)
 Insertion event handler. More...
 
void on_evict (const Key &key, const CacheItem &item)
 Eviction event handler. More...
 
size_t count () const
 Get the number of items currently in the cache. More...
 
size_t maximum_count () const
 Get the maximum number of items allowed in cache. More...
 

Detailed Description

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

Count constraint.

Use this when the constraint of the cache should be the number of items in cache.

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.

Member Function Documentation

◆ can_add()

template<typename Key , typename KeyHash , typename Value >
bool cachemere::policy::ConstraintCount< 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::ConstraintCount< 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.

◆ count()

template<class K , class KH , class V >
size_t cachemere::policy::ConstraintCount< K, KH, V >::count

Get the number of items currently in the cache.

Returns
The number of items in cache.

◆ is_satisfied()

template<class K , class KH , class V >
bool cachemere::policy::ConstraintCount< 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_count()

template<class K , class KH , class V >
size_t cachemere::policy::ConstraintCount< K, KH, V >::maximum_count

Get the maximum number of items allowed in cache.

Returns
The maximum number of items allowed in cache.

◆ on_evict()

template<typename Key , typename KeyHash , typename Value >
void cachemere::policy::ConstraintCount< K, KH, V >::on_evict ( const Key &  key,
const CacheItem item 
)

Eviction event handler.

Removes one from the number of items in cache.

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

◆ on_insert()

template<typename Key , typename KeyHash , typename Value >
void cachemere::policy::ConstraintCount< K, KH, V >::on_insert ( const Key &  key,
const CacheItem item 
)

Insertion event handler.

Adds one to the number of items in cache.

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

◆ update()

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

Update the cache constraint.

Sets a new maximum count.

Parameters
maximum_countThe new number of items in cache.

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