Caches 0.1.0
LRU/LFU/FIFO Caches library
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Member Functions | List of all members
caches::fixed_sized_cache< Key, Value, Policy, HashMap > Class Template Reference

Fixed sized cache that can be used with different policy types (e.g. LRU, FIFO, LFU) More...

#include <cache.hpp>

Public Types

using map_type = HashMap
 
using value_type = typename map_type::mapped_type
 
using iterator = typename map_type::iterator
 
using const_iterator = typename map_type::const_iterator
 
using operation_guard = typename std::lock_guard< std::mutex >
 
using on_erase_cb = typename std::function< void(const Key &key, const value_type &value)>
 

Public Member Functions

 fixed_sized_cache (size_t max_size, const Policy< Key > policy=Policy< Key >{}, on_erase_cb on_erase=[](const Key &, const value_type &) {})
 Fixed sized cache constructor.
 
void Put (const Key &key, const Value &value) noexcept
 Put element into the cache.
 
std::pair< value_type, bool > TryGet (const Key &key) const noexcept
 Try to get an element by the given key from the cache.
 
value_type Get (const Key &key) const
 Get element from the cache if present.
 
bool Cached (const Key &key) const noexcept
 Check whether the given key is presented in the cache.
 
std::size_t Size () const
 Get number of elements in cache.
 
bool Remove (const Key &key)
 

Protected Member Functions

void Clear ()
 
const_iterator begin () const noexcept
 
const_iterator end () const noexcept
 
void Insert (const Key &key, const Value &value)
 
void Erase (const_iterator elem)
 
void Erase (const Key &key)
 
void Update (const Key &key, const Value &value)
 
const_iterator FindElem (const Key &key) const
 
std::pair< const_iterator, bool > GetInternal (const Key &key) const noexcept
 

Detailed Description

template<typename Key, typename Value, template< typename > class Policy = NoCachePolicy, typename HashMap = std::unordered_map<Key, WrappedValue<Value>>>
class caches::fixed_sized_cache< Key, Value, Policy, HashMap >

Fixed sized cache that can be used with different policy types (e.g. LRU, FIFO, LFU)

Template Parameters
KeyType of a key (should be hashable)
ValueType of a value stored in the cache
PolicyType of a policy to be used with the cache
HashMapType of a hashmap to use for cache operations. Should have std::unordered_map compatible interface

Constructor & Destructor Documentation

◆ fixed_sized_cache()

template<typename Key , typename Value , template< typename > class Policy = NoCachePolicy, typename HashMap = std::unordered_map<Key, WrappedValue<Value>>>
caches::fixed_sized_cache< Key, Value, Policy, HashMap >::fixed_sized_cache ( size_t  max_size,
const Policy< Key >  policy = Policy<Key>{},
on_erase_cb  on_erase = [](const Key &, const value_type &) {} 
)
inlineexplicit

Fixed sized cache constructor.

Exceptions
std::invalid_argument
Parameters
[in]max_sizeMaximum size of the cache
[in]policyCache policy to use
[in]on_eraseon_erase_cb function to be called when cache's element get erased

Member Function Documentation

◆ Cached()

template<typename Key , typename Value , template< typename > class Policy = NoCachePolicy, typename HashMap = std::unordered_map<Key, WrappedValue<Value>>>
bool caches::fixed_sized_cache< Key, Value, Policy, HashMap >::Cached ( const Key &  key) const
inlinenoexcept

Check whether the given key is presented in the cache.

Parameters
[in]keyElement key to check
Return values
trueElement is presented in the case
falseElement is not presented in the case

◆ Get()

template<typename Key , typename Value , template< typename > class Policy = NoCachePolicy, typename HashMap = std::unordered_map<Key, WrappedValue<Value>>>
value_type caches::fixed_sized_cache< Key, Value, Policy, HashMap >::Get ( const Key &  key) const
inline

Get element from the cache if present.

Warning
This method will change in the future with an optional class capabilities to avoid throwing exceptions
Exceptions
std::range_error
Parameters
[in]keyGet element by key
Returns
Reference to the value stored by the specified key in the cache

◆ Put()

template<typename Key , typename Value , template< typename > class Policy = NoCachePolicy, typename HashMap = std::unordered_map<Key, WrappedValue<Value>>>
void caches::fixed_sized_cache< Key, Value, Policy, HashMap >::Put ( const Key &  key,
const Value &  value 
)
inlinenoexcept

Put element into the cache.

Parameters
[in]keyKey value to use
[in]valueValue to assign to the given key

◆ Remove()

template<typename Key , typename Value , template< typename > class Policy = NoCachePolicy, typename HashMap = std::unordered_map<Key, WrappedValue<Value>>>
bool caches::fixed_sized_cache< Key, Value, Policy, HashMap >::Remove ( const Key &  key)
inline

Remove an element specified by key

Parameters
[in]keyKey parameter
Return values
trueif an element specified by key was found and deleted
falseif an element is not present in a cache

◆ Size()

template<typename Key , typename Value , template< typename > class Policy = NoCachePolicy, typename HashMap = std::unordered_map<Key, WrappedValue<Value>>>
std::size_t caches::fixed_sized_cache< Key, Value, Policy, HashMap >::Size ( ) const
inline

Get number of elements in cache.

Returns
Number of elements currently stored in the cache

◆ TryGet()

template<typename Key , typename Value , template< typename > class Policy = NoCachePolicy, typename HashMap = std::unordered_map<Key, WrappedValue<Value>>>
std::pair< value_type, bool > caches::fixed_sized_cache< Key, Value, Policy, HashMap >::TryGet ( const Key &  key) const
inlinenoexcept

Try to get an element by the given key from the cache.

Parameters
[in]keyGet element by key
Returns
Pair of iterator that points to the element and boolean value that shows whether get operation has been successful or not. If pair's boolean value is false, the element is not presented in the cache. If pair's boolean value is true, returned iterator can be used to get access to the element

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