mod_servlet
C++Servlets
 All Classes Files Functions Variables Typedefs Macros Pages
servlet::lru_map< _Key, _Tp, _MT > Class Template Reference

Implementation of LRU (least recently used) timed cache. More...

#include <lru_map.h>

Inheritance diagram for servlet::lru_map< _Key, _Tp, _MT >:
servlet::linked_map< _Key, _Tp, _MT >

Public Types

typedef linked_map< _Key, _Tp,
_MT > 
base_type
 Type of base linked_map
 
typedef base_type::key_type key_type
 Container's key type.
 
typedef base_type::mapped_type mapped_type
 Container's mapped type.
 
typedef base_type::value_type value_type
 Container's value type: std::pair<const key_type&, mapped_type>
 
typedef base_type::allocator_type allocator_type
 Container's allocator type.
 
typedef base_type::reference reference
 value_type&
 
typedef base_type::const_reference const_reference
 const value_type&
 
typedef base_type::pointer pointer
 Pointer to value_type type.
 
typedef base_type::const_pointer const_pointer
 Constant pointer to value_type type.
 
typedef base_type::size_type size_type
 An unsigned integral type to represent the size of this container.
 
typedef base_type::difference_type difference_type
 A signed integral type to represent distance between iterators.
 
typedef base_type::iterator iterator
 Bidirectional iterator type.
 
typedef base_type::const_iterator const_iterator
 Bidirectional constant iterator type.
 
typedef base_type::reverse_iterator reverse_iterator
 Reverse iterator type.
 
typedef
base_type::const_reverse_iterator 
const_reverse_iterator
 Constant reverse iterator type.
 
- Public Types inherited from servlet::linked_map< _Key, _Tp, _MT >
typedef _Key key_type
 Container's key type.
 
typedef _Tp mapped_type
 Container's mapped type.
 
typedef std::pair< const _Key
&, _Tp > 
value_type
 Container's value type: std::pair<const key_type&, mapped_type>
 
typedef linked_map< _Key, _Tp,
_MT > 
self_type
 The type of this container. More...
 
typedef std::list< value_typelist_type
 List type to maintain the order of elements.
 
typedef _MT map_type
 Underlying map type.
 
typedef map_type::allocator_type allocator_type
 Container's allocator type.
 
typedef value_typereference
 value_type&
 
typedef const value_typeconst_reference
 const value_type&
 
typedef value_typepointer
 Pointer to value_type type.
 
typedef const value_typeconst_pointer
 Constant pointer to value_type type.
 
typedef map_type::size_type size_type
 An unsigned integral type to represent the size of this container.
 
typedef map_type::difference_type difference_type
 A signed integral type to represent distance between iterators.
 
typedef list_type::iterator iterator
 Bidirectional iterator type.
 
typedef list_type::const_iterator const_iterator
 Bidirectional constant iterator type.
 
typedef list_type::reverse_iterator reverse_iterator
 Reverse iterator type.
 
typedef
list_type::const_reverse_iterator 
const_reverse_iterator
 Constant reverse iterator type.
 

Public Member Functions

 lru_map (std::size_t timeout_sec)
 Constructs an empty container, with no elements. More...
 
 lru_map (const lru_map &other)
 Copy constructor. More...
 
 lru_map (lru_map &&other)
 Move constructor. More...
 
lru_mapoperator= (const lru_map &other)
 The copy assignment. More...
 
lru_mapoperator= (lru_map &&other)
 The move assignment. More...
 
void set_timeout (std::size_t timeout_sec)
 Sets the timeout after which inactive elements will be purged from the cache. More...
 
template<typename KeyType >
bool contains_key (const KeyType &key) const
 Tests whether value with a given key exists in this container. More...
 
void clear ()
 Clear content. More...
 
template<typename KeyType >
optional_ref< const mapped_typeget (const KeyType &key) const
 Returns optional_ref object to a value with a specified type, if that value exists and can be casted to the requested type. More...
 
template<typename KeyType >
optional_ref< mapped_typeget (const KeyType &key)
 Returns optional_ref object to a value with a specified type, if that value exists and can be casted to the requested type. More...
 
template<class... Args>
bool put (key_type &&key, Args &&...args)
 Associates a value of specified type created with a given arguments with the specified key in this map. More...
 
template<class... Args>
bool put (const key_type &key, Args &&...args)
 Associates a value of specified type created with a given arguments with the specified key in this map. More...
 
template<class... Args>
bool try_put (key_type &&key, Args &&...args)
 Associates a value of specified type created with a given arguments with the specified key in this map. More...
 
template<class... Args>
bool try_put (const key_type &key, Args &&...args)
 Associates a value of specified type created with a given arguments with the specified key in this map. More...
 
template<typename KeyType >
bool erase (const KeyType &key)
 Erase element. More...
 
- Public Member Functions inherited from servlet::linked_map< _Key, _Tp, _MT >
 linked_map ()=default
 Constructs an empty container, with no elements.
 
 linked_map (const linked_map &m)=default
 Copy constructor. More...
 
 linked_map (linked_map &&m)=default
 Move constructor. More...
 
linked_mapoperator= (const linked_map &m)=default
 The copy assignment. More...
 
linked_mapoperator= (linked_map &&m)=default
 The move assignment. More...
 
bool empty () const noexcept
 Test whether container is empty. More...
 
size_type size () const noexcept
 Returns the number of elements in the container. More...
 
template<typename KeyType >
bool contains_key (const KeyType &key) const
 Tests whether value with a given key exists in this container. More...
 
void clear ()
 Clear content. More...
 
template<typename KeyType >
optional_ref< const mapped_typeget (const KeyType &key) const
 Returns optional_ref object to a value with a specified type, if that value exists and can be casted to the requested type. More...
 
template<typename KeyType >
optional_ref< mapped_typeget (const KeyType &key)
 Returns optional_ref object to a value with a specified type, if that value exists and can be casted to the requested type. More...
 
template<class... Args>
bool put (key_type &&key, Args &&...args)
 Associates a value of specified type created with a given arguments with the specified key in this map. More...
 
template<class... Args>
bool put (const key_type &key, Args &&...args)
 Associates a value of specified type created with a given arguments with the specified key in this map. More...
 
template<class... Args>
bool try_put (key_type &&key, Args &&...args)
 Associates a value of specified type created with a given arguments with the specified key in this map. More...
 
template<class... Args>
bool try_put (const key_type &key, Args &&...args)
 Associates a value of specified type created with a given arguments with the specified key in this map. More...
 
template<typename KeyType >
bool erase (const KeyType &key)
 Erase element. More...
 
iterator begin () noexcept
 Returns iterator to beginning of the container. More...
 
iterator end () noexcept
 Return iterator to end of the container. More...
 
const_iterator begin () const noexcept
 Returns constant iterator to beginning of the container. More...
 
const_iterator end () const noexcept
 Return constant iterator to end of the container. More...
 
const_iterator cbegin () const noexcept
 Returns constant iterator to beginning of the container. More...
 
const_iterator cend () const noexcept
 Return constant iterator to end of the container. More...
 
reverse_iterator rbegin () noexcept
 Return reverse iterator to reverse beginning. More...
 
reverse_iterator rend () noexcept
 Return reverse iterator to reverse end. More...
 
const_reverse_iterator rbegin () const noexcept
 Return constant reverse iterator to reverse beginning. More...
 
const_reverse_iterator rend () const noexcept
 Return constant reverse iterator to reverse end. More...
 
const_reverse_iterator crbegin () const noexcept
 Return constant reverse iterator to reverse beginning. More...
 
const_reverse_iterator crend () const noexcept
 Return constant reverse iterator to reverse end. More...
 

Protected Member Functions

void update (value_type &val) const override
 Updates access timestamp of the element. More...
 
void purge () override
 Removes all the elements from the cache which has not been accessed for longer than this cache timeout.
 

Detailed Description

template<typename _Key, typename _Tp, typename _MT>
class servlet::lru_map< _Key, _Tp, _MT >

Implementation of LRU (least recently used) timed cache.

This class keeps track of time when elements were accessed and removes them if the have not been accessed for longer than timeout

This is a synchronized container.

Template Parameters
_Keytype of the key
_Tptype of the mapped value
_MTtype of the base map for this class to inherit from. Currently it can be either std::map or std::unordered_map
See Also
linked_map

Constructor & Destructor Documentation

template<typename _Key , typename _Tp , typename _MT >
servlet::lru_map< _Key, _Tp, _MT >::lru_map ( std::size_t  timeout_sec)
inline

Constructs an empty container, with no elements.

The timeout argument is specified in seconds. After this number of seconds if element is not accessed it will be removed from this container.

Parameters
timeout_secExpiration time for elements in this container.
template<typename _Key , typename _Tp , typename _MT >
servlet::lru_map< _Key, _Tp, _MT >::lru_map ( const lru_map< _Key, _Tp, _MT > &  other)
inline

Copy constructor.

Constructs a container with a copy of each of the elements in other.

Parameters
otherlinked map object to copy from.
template<typename _Key , typename _Tp , typename _MT >
servlet::lru_map< _Key, _Tp, _MT >::lru_map ( lru_map< _Key, _Tp, _MT > &&  other)
inline

Move constructor.

Constructs a container that acquires the elements of other by moving them.

Parameters
otherlinked map object to move from.

Member Function Documentation

template<typename _Key , typename _Tp , typename _MT >
void servlet::lru_map< _Key, _Tp, _MT >::clear ( )
inline

Clear content.

Removes all elements from the container (which are destroyed), leaving the container with a size of 0

template<typename _Key , typename _Tp , typename _MT >
template<typename KeyType >
bool servlet::lru_map< _Key, _Tp, _MT >::contains_key ( const KeyType &  key) const
inline

Tests whether value with a given key exists in this container.

Template Parameters
KeyTypea type comparable to type of this map's key (via std::less<>
Parameters
keyKey to test.
Returns
true if a value with a given key exists in this container, false otherwise.
template<typename _Key , typename _Tp , typename _MT >
template<typename KeyType >
bool servlet::lru_map< _Key, _Tp, _MT >::erase ( const KeyType &  key)
inline

Erase element.

Removes from the container a single element identified by a given key. Does nothing if the element with a given key is not found.

Template Parameters
KeyTypea type comparable to type of this map's key (via std::less<>
Parameters
keyKey of the element to remove.
Returns
true if the element was actually removed, false otherwise.
template<typename _Key , typename _Tp , typename _MT >
template<typename KeyType >
optional_ref<const mapped_type> servlet::lru_map< _Key, _Tp, _MT >::get ( const KeyType &  key) const
inline

Returns optional_ref object to a value with a specified type, if that value exists and can be casted to the requested type.

If the value with a given key doesn't exists empty optional_ref will be returned.

Template Parameters
KeyTypea type comparable to type of this map's key (via std::less<>
Parameters
keyKey to be searched for.
Returns
optional_ref to the found value, or empty reference if a value with a given key doesn't exists in this container.
Exceptions
std::bad_any_castif the value is found, but couldn't be casted to the requested type
template<typename _Key , typename _Tp , typename _MT >
template<typename KeyType >
optional_ref<mapped_type> servlet::lru_map< _Key, _Tp, _MT >::get ( const KeyType &  key)
inline

Returns optional_ref object to a value with a specified type, if that value exists and can be casted to the requested type.

If the value with a given key doesn't exists empty optional_ref will be returned.

Template Parameters
KeyTypea type comparable to type of this map's key (via std::less<>
Parameters
keyKey to be searched for.
Returns
optional_ref to the found value, or empty reference if a value with a given key doesn't exists in this container.
Exceptions
std::bad_any_castif the value is found, but couldn't be casted to the requested type
template<typename _Key , typename _Tp , typename _MT >
lru_map& servlet::lru_map< _Key, _Tp, _MT >::operator= ( const lru_map< _Key, _Tp, _MT > &  other)
inline

The copy assignment.

Copies all the elements from other into the container (with other preserving its contents)

Parameters
otherCache object to copy from.
Returns
reference to self
template<typename _Key , typename _Tp , typename _MT >
lru_map& servlet::lru_map< _Key, _Tp, _MT >::operator= ( lru_map< _Key, _Tp, _MT > &&  other)
inline

The move assignment.

Moves the elements of other into the container (other is left in an unspecified but valid state).

Parameters
otherCache object to move from.
Returns
reference to self
template<typename _Key , typename _Tp , typename _MT >
template<class... Args>
bool servlet::lru_map< _Key, _Tp, _MT >::put ( key_type &&  key,
Args &&...  args 
)
inline

Associates a value of specified type created with a given arguments with the specified key in this map.

If the map previously contained a mapping for the key, the old value is replaced.

Template Parameters
Argstypes of the arguments to be forwarded to new mapped value constructor.
Parameters
keykey with which the specified value is to be associated
argsarguments to create the mapped value
Returns
bool denoting whether the previous value was replaced.
See Also
try_put
template<typename _Key , typename _Tp , typename _MT >
template<class... Args>
bool servlet::lru_map< _Key, _Tp, _MT >::put ( const key_type key,
Args &&...  args 
)
inline

Associates a value of specified type created with a given arguments with the specified key in this map.

If the map previously contained a mapping for the key, the old value is replaced.

Template Parameters
Argstypes of the arguments to be forwarded to new mapped value constructor.
Parameters
keykey with which the specified value is to be associated
argsargument to create the mapped value
Returns
bool denoting whether the previous value was replaced.
See Also
try_put
template<typename _Key , typename _Tp , typename _MT >
void servlet::lru_map< _Key, _Tp, _MT >::set_timeout ( std::size_t  timeout_sec)
inline

Sets the timeout after which inactive elements will be purged from the cache.

Parameters
timeout_secNumber of seconds after which inactive element will be removed.
template<typename _Key , typename _Tp , typename _MT >
template<class... Args>
bool servlet::lru_map< _Key, _Tp, _MT >::try_put ( key_type &&  key,
Args &&...  args 
)
inline

Associates a value of specified type created with a given arguments with the specified key in this map.

If the map previously contained a mapping for the key, does nothing.

Template Parameters
Argstypes of the arguments to be forwarded to new mapped value constructor.
Parameters
keykey with which the specified value is to be associated
argsargument to create the mapped value
Returns
bool denoting whether insertion took place.
See Also
put
template<typename _Key , typename _Tp , typename _MT >
template<class... Args>
bool servlet::lru_map< _Key, _Tp, _MT >::try_put ( const key_type key,
Args &&...  args 
)
inline

Associates a value of specified type created with a given arguments with the specified key in this map.

If the map previously contained a mapping for the key, does nothing.

Template Parameters
Argstypes of the arguments to be forwarded to new mapped value constructor.
Parameters
keykey with which the specified value is to be associated
argsargument to create the mapped value
Returns
bool denoting whether insertion took place.
See Also
put
template<typename _Key , typename _Tp , typename _MT >
void servlet::lru_map< _Key, _Tp, _MT >::update ( value_type val) const
inlineoverrideprotectedvirtual

Updates access timestamp of the element.

Parameters
valValue for which to update the access timestamp.

Reimplemented from servlet::linked_map< _Key, _Tp, _MT >.


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