proxygen
folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual > Class Template Reference

#include <EvictingCacheMap.h>

Classes

class  iterator_base
 
struct  KeyHasher
 
struct  KeyValueEqual
 
struct  Node
 

Public Types

typedef std::function< void(TKey, TValue &&)> PruneHookCall
 
typedef iterator_base< TPair, typename NodeList::iterator > iterator
 
typedef iterator_base< const TPair, typename NodeList::const_iterator > const_iterator
 
typedef iterator_base< TPair, typename NodeList::reverse_iterator > reverse_iterator
 
typedef iterator_base< const TPair, typename NodeList::const_reverse_iterator > const_reverse_iterator
 
using key_type = TKey
 
using mapped_type = TValue
 
using hasher = THash
 

Public Member Functions

 EvictingCacheMap (std::size_t maxSize, std::size_t clearSize=1, const THash &keyHash=THash(), const TKeyEqual &keyEqual=TKeyEqual())
 
 EvictingCacheMap (const EvictingCacheMap &)=delete
 
EvictingCacheMapoperator= (const EvictingCacheMap &)=delete
 
 EvictingCacheMap (EvictingCacheMap &&)=default
 
EvictingCacheMapoperator= (EvictingCacheMap &&)=default
 
 ~EvictingCacheMap ()
 
void setMaxSize (size_t maxSize, PruneHookCall pruneHook=nullptr)
 
size_t getMaxSize () const
 
void setClearSize (size_t clearSize)
 
bool exists (const TKey &key) const
 
TValue & get (const TKey &key)
 
iterator find (const TKey &key)
 
const TValue & getWithoutPromotion (const TKey &key) const
 
TValue & getWithoutPromotion (const TKey &key)
 
const_iterator findWithoutPromotion (const TKey &key) const
 
iterator findWithoutPromotion (const TKey &key)
 
bool erase (const TKey &key)
 
void set (const TKey &key, TValue value, bool promote=true, PruneHookCall pruneHook=nullptr)
 
std::size_t size () const
 
bool empty () const
 
void clear (PruneHookCall pruneHook=nullptr)
 
void setPruneHook (PruneHookCall pruneHook)
 
void prune (std::size_t pruneSize, PruneHookCall pruneHook=nullptr)
 
iterator begin ()
 
iterator end ()
 
const_iterator begin () const
 
const_iterator end () const
 
const_iterator cbegin () const
 
const_iterator cend () const
 
reverse_iterator rbegin ()
 
reverse_iterator rend ()
 
const_reverse_iterator rbegin () const
 
const_reverse_iterator rend () const
 
const_reverse_iterator crbegin () const
 
const_reverse_iterator crend () const
 

Private Types

typedef boost::intrusive::link_mode< boost::intrusive::safe_link > link_mode
 
typedef boost::intrusive::unordered_set< Node, boost::intrusive::hash< KeyHasher >, boost::intrusive::equal< KeyValueEqual > > NodeMap
 
typedef boost::intrusive::list< NodeNodeList
 
typedef std::pair< const TKey, TValue > TPair
 

Private Member Functions

NodeMap::iterator findInIndex (const TKey &key)
 
NodeMap::const_iterator findInIndex (const TKey &key) const
 
void pruneWithFailSafeOption (std::size_t pruneSize, PruneHookCall pruneHook, bool failSafe)
 

Private Attributes

PruneHookCall pruneHook_
 
std::size_t nIndexBuckets_
 
std::unique_ptr< typename NodeMap::bucket_type[]> indexBuckets_
 
NodeMap::bucket_traits indexTraits_
 
THash keyHash_
 
TKeyEqual keyEqual_
 
NodeMap index_
 
NodeList lru_
 
std::size_t maxSize_
 
std::size_t clearSize_
 

Static Private Attributes

static const std::size_t kMinNumIndexBuckets = 100
 

Detailed Description

template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
class folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >

A general purpose LRU evicting cache. Designed to support constant time set/get operations. It maintains a doubly linked list of items that are threaded through an index (a hash map). The access ordered is maintained on the list by moving an element to the front of list on a get. New elements are added to the front of the list. The index size is set to half the capacity (setting capacity to 0 is a special case. see notes at the end of this section). So assuming uniform distribution of keys, set/get are both constant time operations.

On reaching capacity limit, clearSize_ LRU items are evicted at a time. If a callback is specified with setPruneHook, it is invoked for each eviction.

This is NOT a thread-safe implementation.

Configurability: capacity of the cache, number of items to evict, eviction callback and the hasher to hash the keys can all be supplied by the caller.

If at a given state, N1 - N6 are the nodes in MRU to LRU order and hashing to index keys as {(N1,N5)->H1, (N4,N5,N5)->H2, N3->Hi}, the datastructure layout is as below. N1 .. N6 is a list threaded through the hash. Assuming, each the number of nodes hashed to each index key is bounded, the following operations run in constant time. i) get computes the index key, walks the list of elements hashed to the key and moves it to the front of the list, if found. ii) set inserts a new node into the list and places the same node on to the list of elements hashing to the corresponding index key. ii) prune deletes nodes from the end of the list as well from the index.

+-—+ +-—+ +-—+ | H1 | <-> | N1 | <-> | N5 | +-—+ +-—+ +-—+ ^ ^ ^ | ___/ \ | / \ |_ /________ ___ / | \ / | \ v v v +-—+ +-—+ +-—+ +-—+ | H2 | <-> | N4 | <-> | N2 | <-> | N6 | +-—+ +-—+ +-—+ +-—+ . ^ ^ . | | . | | . | _____| . | / v v +-—+ +-—+ | Hi | <-> | N3 | +-—+ +-—+

N.B 1 : Changing the capacity with setMaxSize does not change the index size and it could end up in too many elements indexed to the same slot in index. The set/get performance will get worse in this case. So it is best to avoid resizing.

N.B 2 : Setting capacity to 0, using setMaxSize or initialization, turns off evictions based on sizeof the cache making it an INFINITE size cache unless evictions of LRU items are triggered by calling prune() by clients (using their own eviction criteria).

Definition at line 98 of file EvictingCacheMap.h.

Member Typedef Documentation

template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
typedef iterator_base<const TPair, typename NodeList::const_iterator> folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::const_iterator

Definition at line 135 of file EvictingCacheMap.h.

template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
typedef iterator_base<const TPair, typename NodeList::const_reverse_iterator> folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::const_reverse_iterator

Definition at line 139 of file EvictingCacheMap.h.

template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
using folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::hasher = THash

Definition at line 144 of file EvictingCacheMap.h.

template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
typedef iterator_base<TPair, typename NodeList::iterator> folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::iterator

Definition at line 133 of file EvictingCacheMap.h.

template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
using folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::key_type = TKey

Definition at line 142 of file EvictingCacheMap.h.

template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
typedef boost::intrusive::link_mode<boost::intrusive::safe_link> folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::link_mode
private

Definition at line 103 of file EvictingCacheMap.h.

template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
using folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::mapped_type = TValue

Definition at line 143 of file EvictingCacheMap.h.

template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
typedef boost::intrusive::list<Node> folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::NodeList
private

Definition at line 110 of file EvictingCacheMap.h.

template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
typedef boost::intrusive::unordered_set< Node, boost::intrusive::hash<KeyHasher>, boost::intrusive::equal<KeyValueEqual> > folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::NodeMap
private

Definition at line 109 of file EvictingCacheMap.h.

template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
typedef std::function<void(TKey, TValue&&)> folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::PruneHookCall

Definition at line 114 of file EvictingCacheMap.h.

template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
typedef iterator_base<TPair, typename NodeList::reverse_iterator> folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::reverse_iterator

Definition at line 137 of file EvictingCacheMap.h.

template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
typedef std::pair<const TKey, TValue> folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::TPair
private

Definition at line 111 of file EvictingCacheMap.h.

Constructor & Destructor Documentation

template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::EvictingCacheMap ( std::size_t  maxSize,
std::size_t  clearSize = 1,
const THash &  keyHash = THash(),
const TKeyEqual &  keyEqual = TKeyEqual() 
)
inlineexplicit

Construct a EvictingCacheMap

Parameters
maxSizemaximum size of the cache map. Once the map size exceeds maxSize, the map will begin to evict.
clearSizethe number of elements to clear at a time when the eviction size is reached.

Definition at line 153 of file EvictingCacheMap.h.

Referenced by folly::EvictingCacheMap< std::string, SSL_SESSION * >::EvictingCacheMap().

158  : nIndexBuckets_(std::max(maxSize / 2, std::size_t(kMinNumIndexBuckets))),
159  indexBuckets_(new typename NodeMap::bucket_type[nIndexBuckets_]),
161  keyHash_(keyHash),
162  keyEqual_(keyEqual),
164  maxSize_(maxSize),
165  clearSize_(clearSize) {}
LogLevel max
Definition: LogLevel.cpp:31
NodeMap::bucket_traits indexTraits_
std::unique_ptr< typename NodeMap::bucket_type[]> indexBuckets_
static const std::size_t kMinNumIndexBuckets
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::EvictingCacheMap ( const EvictingCacheMap< TKey, TValue, THash, TKeyEqual > &  )
delete
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::EvictingCacheMap ( EvictingCacheMap< TKey, TValue, THash, TKeyEqual > &&  )
default
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::~EvictingCacheMap ( )
inline

Definition at line 172 of file EvictingCacheMap.h.

172  {
173  setPruneHook(nullptr);
174  // ignore any potential exceptions from pruneHook_
175  pruneWithFailSafeOption(size(), nullptr, true);
176  }
std::size_t size() const
void pruneWithFailSafeOption(std::size_t pruneSize, PruneHookCall pruneHook, bool failSafe)
void setPruneHook(PruneHookCall pruneHook)

Member Function Documentation

template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
iterator folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::begin ( )
inline

Definition at line 385 of file EvictingCacheMap.h.

Referenced by TEST().

385  {
386  return iterator(lru_.begin());
387  }
iterator_base< TPair, typename NodeList::iterator > iterator
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
const_iterator folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::begin ( ) const
inline

Definition at line 391 of file EvictingCacheMap.h.

391  {
392  return const_iterator(lru_.begin());
393  }
iterator_base< const TPair, typename NodeList::const_iterator > const_iterator
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
const_iterator folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::cbegin ( ) const
inline

Definition at line 398 of file EvictingCacheMap.h.

398  {
399  return const_iterator(lru_.cbegin());
400  }
iterator_base< const TPair, typename NodeList::const_iterator > const_iterator
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
const_iterator folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::cend ( ) const
inline

Definition at line 401 of file EvictingCacheMap.h.

401  {
402  return const_iterator(lru_.cend());
403  }
iterator_base< const TPair, typename NodeList::const_iterator > const_iterator
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
void folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::clear ( PruneHookCall  pruneHook = nullptr)
inline

Definition at line 354 of file EvictingCacheMap.h.

Referenced by wangle::LocalSSLSessionCache::~LocalSSLSessionCache().

354  {
355  prune(size(), pruneHook);
356  }
std::size_t size() const
void prune(std::size_t pruneSize, PruneHookCall pruneHook=nullptr)
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
const_reverse_iterator folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::crbegin ( ) const
inline

Definition at line 419 of file EvictingCacheMap.h.

419  {
420  return const_reverse_iterator(lru_.crbegin());
421  }
iterator_base< const TPair, typename NodeList::const_reverse_iterator > const_reverse_iterator
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
const_reverse_iterator folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::crend ( ) const
inline

Definition at line 422 of file EvictingCacheMap.h.

422  {
423  return const_reverse_iterator(lru_.crend());
424  }
iterator_base< const TPair, typename NodeList::const_reverse_iterator > const_reverse_iterator
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
bool folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::empty ( ) const
inline

Typical empty function

Returns
true if empty, false otherwise

Definition at line 350 of file EvictingCacheMap.h.

Referenced by TEST().

350  {
351  return index_.empty();
352  }
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
const_iterator folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::end ( ) const
inline

Definition at line 394 of file EvictingCacheMap.h.

394  {
395  return const_iterator(lru_.end());
396  }
iterator_base< const TPair, typename NodeList::const_iterator > const_iterator
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
bool folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::erase ( const TKey &  key)
inline

Erase the key-value pair associated with key if it exists.

Parameters
keykey associated with the value
Returns
true if the key existed and was erased, else false

Definition at line 293 of file EvictingCacheMap.h.

Referenced by TEST().

293  {
294  auto it = findInIndex(key);
295  if (it == index_.end()) {
296  return false;
297  }
298  auto node = &(*it);
299  std::unique_ptr<Node> nptr(node);
300  lru_.erase(lru_.iterator_to(*node));
301  index_.erase(it);
302  return true;
303  }
NodeMap::iterator findInIndex(const TKey &key)
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
bool folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::exists ( const TKey &  key) const
inline

Check for existence of a specific key in the map. This operation has no effect on LRU order.

Parameters
keykey to search for
Returns
true if exists, false otherwise

Definition at line 215 of file EvictingCacheMap.h.

Referenced by TEST().

215  {
216  return findInIndex(key) != index_.end();
217  }
NodeMap::iterator findInIndex(const TKey &key)
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
iterator folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::find ( const TKey &  key)
inline

Get the iterator associated with a specific key. This function always promotes a found value to the head of the LRU.

Parameters
keykey to associate with value
Returns
the iterator of the object (a std::pair of const TKey, TValue) or end() if it does not exist

Definition at line 241 of file EvictingCacheMap.h.

Referenced by folly::EvictingCacheMap< std::string, SSL_SESSION * >::get(), and TEST().

241  {
242  auto it = findInIndex(key);
243  if (it == index_.end()) {
244  return end();
245  }
246  lru_.erase(lru_.iterator_to(*it));
247  lru_.push_front(*it);
248  return iterator(lru_.iterator_to(*it));
249  }
iterator_base< TPair, typename NodeList::iterator > iterator
NodeMap::iterator findInIndex(const TKey &key)
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
NodeMap::iterator folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::findInIndex ( const TKey &  key)
inlineprivate

Get the iterator in in the index associated with a specific key. This is merely a search in the index and does not promote the object.

Parameters
keykey to associate with value
Returns
the NodeMap::iterator to the Node containing the object (a std::pair of const TKey, TValue) or index_.end() if it does not exist

Definition at line 466 of file EvictingCacheMap.h.

Referenced by folly::EvictingCacheMap< std::string, SSL_SESSION * >::erase(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::exists(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::find(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::findWithoutPromotion(), and folly::EvictingCacheMap< std::string, SSL_SESSION * >::set().

466  {
467  return index_.find(key, KeyHasher(keyHash_), KeyValueEqual(keyEqual_));
468  }
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
NodeMap::const_iterator folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::findInIndex ( const TKey &  key) const
inlineprivate

Definition at line 470 of file EvictingCacheMap.h.

470  {
471  return index_.find(key, KeyHasher(keyHash_), KeyValueEqual(keyEqual_));
472  }
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
const_iterator folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::findWithoutPromotion ( const TKey &  key) const
inline

Get the iterator associated with a specific key. This function never promotes a found value to the head of the LRU.

Parameters
keykey to associate with value
Returns
the iterator of the object (a std::pair of const TKey, TValue) or end() if it does not exist

Definition at line 278 of file EvictingCacheMap.h.

Referenced by folly::EvictingCacheMap< std::string, SSL_SESSION * >::getWithoutPromotion(), and TEST().

278  {
279  auto it = findInIndex(key);
280  return (it == index_.end()) ? end() : const_iterator(lru_.iterator_to(*it));
281  }
iterator_base< const TPair, typename NodeList::const_iterator > const_iterator
NodeMap::iterator findInIndex(const TKey &key)
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
iterator folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::findWithoutPromotion ( const TKey &  key)
inline

Definition at line 283 of file EvictingCacheMap.h.

283  {
284  auto it = findInIndex(key);
285  return (it == index_.end()) ? end() : iterator(lru_.iterator_to(*it));
286  }
iterator_base< TPair, typename NodeList::iterator > iterator
NodeMap::iterator findInIndex(const TKey &key)
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
TValue& folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::get ( const TKey &  key)
inline

Get the value associated with a specific key. This function always promotes a found value to the head of the LRU.

Parameters
keykey associated with the value
Returns
the value if it exists
Exceptions
std::out_of_rangeexception of the key does not exist

Definition at line 226 of file EvictingCacheMap.h.

Referenced by TEST().

226  {
227  auto it = find(key);
228  if (it == end()) {
229  throw_exception<std::out_of_range>("Key does not exist");
230  }
231  return it->second;
232  }
iterator find(const TKey &key)
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
size_t folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::getMaxSize ( ) const
inline

Definition at line 201 of file EvictingCacheMap.h.

201  {
202  return maxSize_;
203  }
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
const TValue& folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::getWithoutPromotion ( const TKey &  key) const
inline

Get the value associated with a specific key. This function never promotes a found value to the head of the LRU.

Parameters
keykey associated with the value
Returns
the value if it exists
Exceptions
std::out_of_rangeexception of the key does not exist

Definition at line 258 of file EvictingCacheMap.h.

Referenced by TEST().

258  {
259  auto it = findWithoutPromotion(key);
260  if (it == end()) {
261  throw_exception<std::out_of_range>("Key does not exist");
262  }
263  return it->second;
264  }
const_iterator findWithoutPromotion(const TKey &key) const
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
TValue& folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::getWithoutPromotion ( const TKey &  key)
inline

Definition at line 266 of file EvictingCacheMap.h.

266  {
267  auto const& cThis = *this;
268  return const_cast<TValue&>(cThis.getWithoutPromotion(key));
269  }
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
EvictingCacheMap& folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::operator= ( const EvictingCacheMap< TKey, TValue, THash, TKeyEqual > &  )
delete
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
EvictingCacheMap& folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::operator= ( EvictingCacheMap< TKey, TValue, THash, TKeyEqual > &&  )
default
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
void folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::prune ( std::size_t  pruneSize,
PruneHookCall  pruneHook = nullptr 
)
inline

Prune the minimum of pruneSize and size() from the back of the LRU. Will throw if pruneHook throws.

Parameters
pruneSizeminimum number of elements to prune
pruneHooka custom pruneHook function

Definition at line 379 of file EvictingCacheMap.h.

Referenced by folly::EvictingCacheMap< std::string, SSL_SESSION * >::clear(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::set(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::setMaxSize(), and TEST().

379  {
380  // do not swallow exceptions for prunes not triggered from destructor
381  pruneWithFailSafeOption(pruneSize, pruneHook, false);
382  }
void pruneWithFailSafeOption(std::size_t pruneSize, PruneHookCall pruneHook, bool failSafe)
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
void folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::pruneWithFailSafeOption ( std::size_t  pruneSize,
PruneHookCall  pruneHook,
bool  failSafe 
)
inlineprivate

Prune the minimum of pruneSize and size() from the back of the LRU.

Parameters
pruneSizeminimum number of elements to prune
pruneHooka custom pruneHook function
failSafetrue if exceptions are to ignored, false by default

Definition at line 480 of file EvictingCacheMap.h.

Referenced by folly::EvictingCacheMap< std::string, SSL_SESSION * >::prune(), and folly::EvictingCacheMap< std::string, SSL_SESSION * >::~EvictingCacheMap().

483  {
484  auto& ph = (nullptr == pruneHook) ? pruneHook_ : pruneHook;
485 
486  for (std::size_t i = 0; i < pruneSize && !lru_.empty(); i++) {
487  auto* node = &(*lru_.rbegin());
488  std::unique_ptr<Node> nptr(node);
489 
490  lru_.erase(lru_.iterator_to(*node));
491  index_.erase(index_.iterator_to(*node));
492  if (ph) {
493  try {
494  ph(node->pr.first, std::move(node->pr.second));
495  } catch (...) {
496  if (!failSafe) {
497  throw;
498  }
499  }
500  }
501  }
502  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
reverse_iterator folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::rbegin ( )
inline

Definition at line 405 of file EvictingCacheMap.h.

Referenced by TEST().

405  {
406  return reverse_iterator(lru_.rbegin());
407  }
iterator_base< TPair, typename NodeList::reverse_iterator > reverse_iterator
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
const_reverse_iterator folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::rbegin ( ) const
inline

Definition at line 412 of file EvictingCacheMap.h.

412  {
413  return const_reverse_iterator(lru_.rbegin());
414  }
iterator_base< const TPair, typename NodeList::const_reverse_iterator > const_reverse_iterator
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
reverse_iterator folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::rend ( )
inline

Definition at line 408 of file EvictingCacheMap.h.

Referenced by TEST().

408  {
409  return reverse_iterator(lru_.rend());
410  }
iterator_base< TPair, typename NodeList::reverse_iterator > reverse_iterator
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
const_reverse_iterator folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::rend ( ) const
inline

Definition at line 415 of file EvictingCacheMap.h.

415  {
416  return const_reverse_iterator(lru_.rend());
417  }
iterator_base< const TPair, typename NodeList::const_reverse_iterator > const_reverse_iterator
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
void folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::set ( const TKey &  key,
TValue  value,
bool  promote = true,
PruneHookCall  pruneHook = nullptr 
)
inline

Set a key-value pair in the dictionary

Parameters
keykey to associate with value
valuevalue to associate with the key
promoteboolean flag indicating whether or not to move something to the front of an LRU. This only really matters if you're setting a value that already exists.
pruneHookcallback to use on eviction (if it occurs).

Definition at line 314 of file EvictingCacheMap.h.

Referenced by TEST().

318  {
319  auto it = findInIndex(key);
320  if (it != index_.end()) {
321  it->pr.second = std::move(value);
322  if (promote) {
323  lru_.erase(lru_.iterator_to(*it));
324  lru_.push_front(*it);
325  }
326  } else {
327  auto node = new Node(key, std::move(value));
328  index_.insert(*node);
329  lru_.push_front(*node);
330 
331  // no evictions if maxSize_ is 0 i.e. unlimited capacity
332  if (maxSize_ > 0 && size() > maxSize_) {
333  prune(clearSize_, pruneHook);
334  }
335  }
336  }
std::size_t size() const
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void prune(std::size_t pruneSize, PruneHookCall pruneHook=nullptr)
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
NodeMap::iterator findInIndex(const TKey &key)
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
void folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::setClearSize ( size_t  clearSize)
inline

Definition at line 205 of file EvictingCacheMap.h.

Referenced by TEST().

205  {
206  clearSize_ = clearSize;
207  }
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
void folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::setMaxSize ( size_t  maxSize,
PruneHookCall  pruneHook = nullptr 
)
inline

Adjust the max size of EvictingCacheMap. Note that this does not update nIndexBuckets_ accordingly. This API can cause performance to get very bad, e.g., the nIndexBuckets_ is still 100 after maxSize is updated to 1M.

Calling this function with an arugment of 0 removes the limit on the cache size and elements are not evicted unless clients explictly call prune.

If you intend to resize dynamically using this, then picking an index size that works well and initializing with corresponding maxSize is the only reasonable option.

Parameters
maxSizenew maximum size of the cache map.
pruneHookcallback to use on eviction.

Definition at line 193 of file EvictingCacheMap.h.

Referenced by TEST().

193  {
194  if (maxSize != 0 && maxSize < size()) {
195  // Prune the excess elements with our new constraints.
196  prune(std::max(size() - maxSize, clearSize_), pruneHook);
197  }
198  maxSize_ = maxSize;
199  }
std::size_t size() const
LogLevel max
Definition: LogLevel.cpp:31
void prune(std::size_t pruneSize, PruneHookCall pruneHook=nullptr)
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
void folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::setPruneHook ( PruneHookCall  pruneHook)
inline

Set the prune hook, which is the function invoked on the key and value on each eviction. Will throw If the pruneHook throws, unless the EvictingCacheMap object is being destroyed in which case it will be ignored.

Parameters
pruneHooknew callback to use on eviction.
promoteboolean flag indicating whether or not to move something to the front of an LRU.
Returns
the iterator of the object (a std::pair of const TKey, TValue) or end() if it does not exist

Definition at line 369 of file EvictingCacheMap.h.

Referenced by wangle::LocalSSLSessionCache::LocalSSLSessionCache(), TEST(), and folly::EvictingCacheMap< std::string, SSL_SESSION * >::~EvictingCacheMap().

369  {
370  pruneHook_ = pruneHook;
371  }
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
std::size_t folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::size ( ) const
inline

Member Data Documentation

template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
std::size_t folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::clearSize_
private
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
std::unique_ptr<typename NodeMap::bucket_type[]> folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::indexBuckets_
private

Definition at line 507 of file EvictingCacheMap.h.

template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
NodeMap::bucket_traits folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::indexTraits_
private

Definition at line 508 of file EvictingCacheMap.h.

template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
TKeyEqual folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::keyEqual_
private
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
THash folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::keyHash_
private
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
const std::size_t folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::kMinNumIndexBuckets = 100
staticprivate

Definition at line 504 of file EvictingCacheMap.h.

template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
std::size_t folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::maxSize_
private
template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
std::size_t folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::nIndexBuckets_
private

Definition at line 506 of file EvictingCacheMap.h.

template<class TKey, class TValue, class THash = std::hash<TKey>, class TKeyEqual = std::equal_to<TKey>>
PruneHookCall folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::pruneHook_
private

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