ModErn Text Analysis
META Enumerates Textual Applications
|
A double-barrel approach at a LRU cache. More...
#include <dblru_cache.h>
Public Member Functions | |
dblru_cache (uint64_t max_size) | |
Constructs a dlbru_cache with a given fixed size. More... | |
dblru_cache (dblru_cache &&) | |
dblru_cache may be move constructed. | |
dblru_cache & | operator= (dblru_cache) |
dlbru_cache may be be assigned. More... | |
~dblru_cache ()=default | |
Default destructor. | |
void | swap (dblru_cache &other) |
Swaps the current dlbru_cache with a given one. More... | |
void | insert (const Key &key, const Value &value) |
Insert a given (key, value) pair into the cache. More... | |
template<class... Args> | |
void | emplace (Args &&...args) |
Inserts a key value pair into the cache using in-place construction if possible. More... | |
util::optional< Value > | find (const Key &key) |
Finds a value in the cache. More... | |
void | clear () |
Empties the cache. More... | |
Private Member Functions | |
void | handle_insert () |
Helper function to ensure that the primary and secondary map swapping occurs at the correct moment. | |
std::shared_ptr< Map< Key, Value > > | get_primary_map () const |
Gets the primary map. | |
std::shared_ptr< Map< Key, Value > > | get_secondary_map () const |
Gets the secondary map. | |
Private Attributes | |
uint64_t | max_size_ |
The maximum allowed size for the cache. | |
uint64_t | current_size_ |
The current size of the primary map. | |
std::unique_ptr< std::mutex > | mutables_ {make_unique<std::mutex>()} |
std::shared_ptr< Map< Key, Value > > | primary_ |
The primary map. | |
std::shared_ptr< Map< Key, Value > > | secondary_ |
The secondary map. | |
A double-barrel approach at a LRU cache.
Uses two Maps, primary and secondary, for implementation. A find will search in the primary and, if found, return the found value. Otherwise, it will search the secondary—if found, that value is promoted to the primary map and the value returned. Otherwise, the value does not exist.
After a fixed number of inserts into the primary, the secondary is emptied and swapped with the primary. This ensures that things that have been less recently used are dropped.
It is assumed that the Maps are internally synchronized (i.e., they contain a mutex or have some other way of guaranteeing concurrency safety).
meta::caching::dblru_cache< Key, Value, Map >::dblru_cache | ( | uint64_t | max_size | ) |
Constructs a dlbru_cache with a given fixed size.
max_size | the maximum allowed size for this cache |
dblru_cache< Key, Value, Map > & meta::caching::dblru_cache< Key, Value, Map >::operator= | ( | dblru_cache< Key, Value, Map > | rhs | ) |
dlbru_cache may be be assigned.
void meta::caching::dblru_cache< Key, Value, Map >::swap | ( | dblru_cache< Key, Value, Map > & | other | ) |
Swaps the current dlbru_cache with a given one.
other | the cache to swap with |
void meta::caching::dblru_cache< Key, Value, Map >::insert | ( | const Key & | key, |
const Value & | value | ||
) |
Insert a given (key, value) pair into the cache.
key | |
value |
void meta::caching::dblru_cache< Key, Value, Map >::emplace | ( | Args &&... | args | ) |
Inserts a key value pair into the cache using in-place construction if possible.
args | the list of arguments to forward to constructing the (key, value) pair |
util::optional< Value > meta::caching::dblru_cache< Key, Value, Map >::find | ( | const Key & | key | ) |
Finds a value in the cache.
If it exists, the optional will be engaged, otherwise, it will be disengaged.
key | the key to find the corresponding value for |
void meta::caching::dblru_cache< Key, Value, Map >::clear | ( | ) |
Empties the cache.