ModErn Text Analysis
META Enumerates Textual Applications
Public Member Functions | Private Member Functions | Private Attributes | List of all members
meta::caching::dblru_cache< Key, Value, Map > Class Template Reference

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_cacheoperator= (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.
 

Detailed Description

template<class Key, class Value, template< class, class > class Map = locking_map>
class meta::caching::dblru_cache< Key, Value, 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).

See also
https://issues.apache.org/jira/browse/LUCENE-2075

Constructor & Destructor Documentation

template<class Key , class Value , template< class, class > class Map>
meta::caching::dblru_cache< Key, Value, Map >::dblru_cache ( uint64_t  max_size)

Constructs a dlbru_cache with a given fixed size.

Parameters
max_sizethe maximum allowed size for this cache

Member Function Documentation

template<class Key , class Value , template< class, class > class Map>
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.

Returns
the current dblru_cache
template<class Key , class Value , template< class, class > class Map>
void meta::caching::dblru_cache< Key, Value, Map >::swap ( dblru_cache< Key, Value, Map > &  other)

Swaps the current dlbru_cache with a given one.

Parameters
otherthe cache to swap with
template<class Key , class Value , template< class, class > class Map>
void meta::caching::dblru_cache< Key, Value, Map >::insert ( const Key &  key,
const Value &  value 
)

Insert a given (key, value) pair into the cache.

Parameters
key
value
template<class Key , class Value , template< class, class > class Map>
template<class... Args>
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.

Parameters
argsthe list of arguments to forward to constructing the (key, value) pair
template<class Key , class Value , template< class, class > class Map>
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.

Parameters
keythe key to find the corresponding value for
Returns
an optional that may contain the value, if found
template<class Key , class Value , template< class, class > class Map>
void meta::caching::dblru_cache< Key, Value, Map >::clear ( )

Empties the cache.


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