vkdb
A time series database engine in C++.
Loading...
Searching...
No Matches
vkdb::LRUCache< TKey, TValue > Class Template Reference

A thread-safe LRU cache. More...

#include <lru_cache.h>

Public Types

using key_type = TKey
 
using mapped_type = std::optional<TValue>
 
using value_type = std::pair<const key_type, mapped_type>
 
using size_type = uint64_t
 

Public Member Functions

 LRUCache () noexcept
 Construct a new LRUCache object.
 
 LRUCache (size_type capacity)
 Construct a new LRUCache object given a capacity.
 
 LRUCache (LRUCache &&) noexcept=default
 Move-construct a LRUCache object.
 
LRUCacheoperator= (LRUCache &&) noexcept=default
 Move-assign a LRUCache object.
 
 LRUCache (const LRUCache &)=delete
 Deleted copy constructor.
 
LRUCacheoperator= (const LRUCache &)=delete
 Deleted copy assignment operator.
 
 ~LRUCache () noexcept=default
 Destroy the LRUCache object.
 
template<SameNoCVRefQuals< key_type > K, std::convertible_to< mapped_type > V>
void put (K &&key, V &&value)
 Put a key-value pair into the cache.
 
template<SameNoCVRefQuals< key_type > K>
mapped_type get (K &&key)
 Get a value from the cache.
 
template<SameNoCVRefQuals< key_type > K>
bool contains (K &&key) const noexcept
 Check if the cache contains a key.
 
size_type capacity () const noexcept
 Remove a key from the cache.
 
size_type size () const noexcept
 Get the size of the cache.
 
void clear () noexcept
 Clear the cache.
 

Detailed Description

template<RegularNoCVRefQuals TKey, RegularNoCVRefQuals TValue>
class vkdb::LRUCache< TKey, TValue >

A thread-safe LRU cache.

Template Parameters
TKeyKey type.
TValueValue type.

Definition at line 20 of file lru_cache.h.

Member Typedef Documentation

◆ key_type

template<RegularNoCVRefQuals TKey, RegularNoCVRefQuals TValue>
using vkdb::LRUCache< TKey, TValue >::key_type = TKey

Definition at line 22 of file lru_cache.h.

◆ mapped_type

template<RegularNoCVRefQuals TKey, RegularNoCVRefQuals TValue>
using vkdb::LRUCache< TKey, TValue >::mapped_type = std::optional<TValue>

Definition at line 23 of file lru_cache.h.

◆ size_type

template<RegularNoCVRefQuals TKey, RegularNoCVRefQuals TValue>
using vkdb::LRUCache< TKey, TValue >::size_type = uint64_t

Definition at line 25 of file lru_cache.h.

◆ value_type

template<RegularNoCVRefQuals TKey, RegularNoCVRefQuals TValue>
using vkdb::LRUCache< TKey, TValue >::value_type = std::pair<const key_type, mapped_type>

Definition at line 24 of file lru_cache.h.

Constructor & Destructor Documentation

◆ LRUCache() [1/2]

template<RegularNoCVRefQuals TKey, RegularNoCVRefQuals TValue>
vkdb::LRUCache< TKey, TValue >::LRUCache ( )
inlinenoexcept

Construct a new LRUCache object.

Definition at line 31 of file lru_cache.h.

◆ LRUCache() [2/2]

template<RegularNoCVRefQuals TKey, RegularNoCVRefQuals TValue>
vkdb::LRUCache< TKey, TValue >::LRUCache ( size_type capacity)
inlineexplicit

Construct a new LRUCache object given a capacity.

Parameters
capacityThe capacity.
Exceptions
std::invalid_argumentIf the capacity is 0.

Definition at line 42 of file lru_cache.h.

Member Function Documentation

◆ capacity()

template<RegularNoCVRefQuals TKey, RegularNoCVRefQuals TValue>
size_type vkdb::LRUCache< TKey, TValue >::capacity ( ) const
inlinenodiscardnoexcept

Remove a key from the cache.

Template Parameters
KKey type.
Parameters
keyKey.

Definition at line 145 of file lru_cache.h.

◆ clear()

template<RegularNoCVRefQuals TKey, RegularNoCVRefQuals TValue>
void vkdb::LRUCache< TKey, TValue >::clear ( )
inlinenoexcept

Clear the cache.

Definition at line 164 of file lru_cache.h.

◆ contains()

template<RegularNoCVRefQuals TKey, RegularNoCVRefQuals TValue>
template<SameNoCVRefQuals< key_type > K>
bool vkdb::LRUCache< TKey, TValue >::contains ( K && key) const
inlinenodiscardnoexcept

Check if the cache contains a key.

Template Parameters
KKey type.
Parameters
keyKey.
Returns
true if the cache contains the key.
false if the cache does not contain the key.

Definition at line 134 of file lru_cache.h.

◆ get()

template<RegularNoCVRefQuals TKey, RegularNoCVRefQuals TValue>
template<SameNoCVRefQuals< key_type > K>
mapped_type vkdb::LRUCache< TKey, TValue >::get ( K && key)
inlinenodiscard

Get a value from the cache.

Template Parameters
KKey type.
Parameters
keyKey.
Returns
mapped_type The value if it exists, std::nullopt otherwise.

Definition at line 115 of file lru_cache.h.

◆ put()

template<RegularNoCVRefQuals TKey, RegularNoCVRefQuals TValue>
template<SameNoCVRefQuals< key_type > K, std::convertible_to< mapped_type > V>
void vkdb::LRUCache< TKey, TValue >::put ( K && key,
V && value )
inline

Put a key-value pair into the cache.

Template Parameters
KKey type.
VValue-convertible type.
Parameters
keyKey.
valueValue.
Exceptions
std::exceptionIf inserting the key-value pair fails.

Definition at line 93 of file lru_cache.h.

◆ size()

template<RegularNoCVRefQuals TKey, RegularNoCVRefQuals TValue>
size_type vkdb::LRUCache< TKey, TValue >::size ( ) const
inlinenodiscardnoexcept

Get the size of the cache.

Returns
size_type The size of the cache.

Definition at line 155 of file lru_cache.h.


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