Skip to content

caches::fixed_sized_cache

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

#include <cache.hpp>

Public Types

Name
using typename std::unordered_map< Key, Value >::iterator iterator
using typename std::unordered_map< Key, Value >::const_iterator const_iterator
using typename std::lock_guard< std::mutex > operation_guard
using typename std::function< void(const Key &key, const Value &value)> on_erase_cb

Public Functions

Name
fixed_sized_cache(size_t max_size, const Policy< Key > policy =Policy< Key >{}, on_erase_cb OnErase = {})
Fixed sized cache constructor.
~fixed_sized_cache()
void Put(const Key & key, const Value & value)
Put element into the cache.
std::pair< const_iterator, bool > TryGet(const Key & key) const
Try to get an element by the given key from the cache.
const Value & Get(const Key & key) const
Get element from the cache if present.
bool Cached(const Key & key) const
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 Functions

Name
void Clear()
const_iterator begin() const
const_iterator end() const
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

Detailed Description

template <typename Key ,
typename Value ,
template< typename > class Policy =NoCachePolicy>
class caches::fixed_sized_cache;

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

Template Parameters:

  • Key Type of a key (should be hashable)
  • Value Type of a value stored in the cache
  • Policy Type of a policy to be used with the cache

Public Types Documentation

using iterator

using caches::fixed_sized_cache< Key, Value, Policy >::iterator =  typename std::unordered_map<Key, Value>::iterator;

using const_iterator

using caches::fixed_sized_cache< Key, Value, Policy >::const_iterator =  typename std::unordered_map<Key, Value>::const_iterator;

using operation_guard

using caches::fixed_sized_cache< Key, Value, Policy >::operation_guard =  typename std::lock_guard<std::mutex>;

using on_erase_cb

using caches::fixed_sized_cache< Key, Value, Policy >::on_erase_cb =  typename std::function<void(const Key &key, const Value &value)>;

Public Functions Documentation

function fixed_sized_cache

inline explicit fixed_sized_cache(
    size_t max_size,
    const Policy< Key > policy =Policy< Key >{},
    on_erase_cb OnErase =[](const Key &, const Value &) {}
)

Fixed sized cache constructor.

Parameters:

  • max_size Maximum size of the cache
  • policy Cache policy to use
  • OnErase on_erase_cb function to be called when cache's element get erased

Exceptions:

  • std::invalid_argument

function ~fixed_sized_cache

inline ~fixed_sized_cache()

function Put

inline void Put(
    const Key & key,
    const Value & value
)

Put element into the cache.

Parameters:

  • key Key value to use
  • value Value to assign to the given key

function TryGet

inline std::pair< const_iterator, bool > TryGet(
    const Key & key
) const

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

Parameters:

  • key Get element by key

Return: 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

function Get

inline const Value & Get(
    const Key & key
) const

Get element from the cache if present.

Parameters:

  • key Get element by key

Exceptions:

  • std::range_error

Return: Reference to the value stored by the specified key in the cache

Warning: This method will change in the future with an optional class capabilities to avoid throwing exceptions

function Cached

inline bool Cached(
    const Key & key
) const

Check whether the given key is presented in the cache.

Parameters:

  • key Element key to check

Returns:

  • true Element is presented in the case
  • false Element is not presented in the case

function Size

inline std::size_t Size() const

Get number of elements in cache.

Return: Number of elements currently stored in the cache

function Remove

inline bool Remove(
    const Key & key
)

Parameters:

  • key Key parameter

Returns:

  • true if an element specified by key was found and deleted
  • false if an element is not present in a cache

Remove an element specified by key

Protected Functions Documentation

function Clear

inline void Clear()

function begin

inline const_iterator begin() const

function end

inline const_iterator end() const

function Insert

inline void Insert(
    const Key & key,
    const Value & value
)

function Erase

inline void Erase(
    const_iterator elem
)

function Erase

inline void Erase(
    const Key & key
)

function Update

inline void Update(
    const Key & key,
    const Value & value
)

function FindElem

inline const_iterator FindElem(
    const Key & key
) const

function GetInternal

inline std::pair< const_iterator, bool > GetInternal(
    const Key & key
) const