proxygen
|
#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 | |
EvictingCacheMap & | operator= (const EvictingCacheMap &)=delete |
EvictingCacheMap (EvictingCacheMap &&)=default | |
EvictingCacheMap & | operator= (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< Node > | NodeList |
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 |
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.
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.
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.
using folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::hasher = THash |
Definition at line 144 of file EvictingCacheMap.h.
typedef iterator_base<TPair, typename NodeList::iterator> folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::iterator |
Definition at line 133 of file EvictingCacheMap.h.
using folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::key_type = TKey |
Definition at line 142 of file EvictingCacheMap.h.
|
private |
Definition at line 103 of file EvictingCacheMap.h.
using folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::mapped_type = TValue |
Definition at line 143 of file EvictingCacheMap.h.
|
private |
Definition at line 110 of file EvictingCacheMap.h.
|
private |
Definition at line 109 of file EvictingCacheMap.h.
typedef std::function<void(TKey, TValue&&)> folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::PruneHookCall |
Definition at line 114 of file EvictingCacheMap.h.
typedef iterator_base<TPair, typename NodeList::reverse_iterator> folly::EvictingCacheMap< TKey, TValue, THash, TKeyEqual >::reverse_iterator |
Definition at line 137 of file EvictingCacheMap.h.
|
private |
Definition at line 111 of file EvictingCacheMap.h.
|
inlineexplicit |
Construct a EvictingCacheMap
maxSize | maximum size of the cache map. Once the map size exceeds maxSize, the map will begin to evict. |
clearSize | the 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().
|
delete |
|
default |
|
inline |
Definition at line 172 of file EvictingCacheMap.h.
|
inline |
Definition at line 385 of file EvictingCacheMap.h.
Referenced by TEST().
|
inline |
Definition at line 391 of file EvictingCacheMap.h.
|
inline |
Definition at line 398 of file EvictingCacheMap.h.
|
inline |
Definition at line 401 of file EvictingCacheMap.h.
|
inline |
Definition at line 354 of file EvictingCacheMap.h.
Referenced by wangle::LocalSSLSessionCache::~LocalSSLSessionCache().
|
inline |
Definition at line 419 of file EvictingCacheMap.h.
|
inline |
Definition at line 422 of file EvictingCacheMap.h.
|
inline |
Typical empty function
Definition at line 350 of file EvictingCacheMap.h.
Referenced by TEST().
|
inline |
Definition at line 388 of file EvictingCacheMap.h.
Referenced by folly::EvictingCacheMap< std::string, SSL_SESSION * >::find(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::findWithoutPromotion(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::get(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::getWithoutPromotion(), wangle::ShardedLocalSSLSessionCache::lookupSession(), wangle::ShardedLocalSSLSessionCache::storeSession(), and TEST().
|
inline |
Definition at line 394 of file EvictingCacheMap.h.
|
inline |
Erase the key-value pair associated with key if it exists.
key | key associated with the value |
Definition at line 293 of file EvictingCacheMap.h.
Referenced by TEST().
|
inline |
Check for existence of a specific key in the map. This operation has no effect on LRU order.
key | key to search for |
Definition at line 215 of file EvictingCacheMap.h.
Referenced by TEST().
|
inline |
Get the iterator associated with a specific key. This function always promotes a found value to the head of the LRU.
key | key to associate with value |
Definition at line 241 of file EvictingCacheMap.h.
Referenced by folly::EvictingCacheMap< std::string, SSL_SESSION * >::get(), and TEST().
|
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.
key | key to associate with value |
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().
|
inlineprivate |
Definition at line 470 of file EvictingCacheMap.h.
|
inline |
Get the iterator associated with a specific key. This function never promotes a found value to the head of the LRU.
key | key to associate with value |
Definition at line 278 of file EvictingCacheMap.h.
Referenced by folly::EvictingCacheMap< std::string, SSL_SESSION * >::getWithoutPromotion(), and TEST().
|
inline |
Definition at line 283 of file EvictingCacheMap.h.
|
inline |
Get the value associated with a specific key. This function always promotes a found value to the head of the LRU.
key | key associated with the value |
std::out_of_range | exception of the key does not exist |
Definition at line 226 of file EvictingCacheMap.h.
Referenced by TEST().
|
inline |
Definition at line 201 of file EvictingCacheMap.h.
|
inline |
Get the value associated with a specific key. This function never promotes a found value to the head of the LRU.
key | key associated with the value |
std::out_of_range | exception of the key does not exist |
Definition at line 258 of file EvictingCacheMap.h.
Referenced by TEST().
|
inline |
Definition at line 266 of file EvictingCacheMap.h.
|
delete |
|
default |
|
inline |
Prune the minimum of pruneSize and size() from the back of the LRU. Will throw if pruneHook throws.
pruneSize | minimum number of elements to prune |
pruneHook | a 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().
|
inlineprivate |
Prune the minimum of pruneSize and size() from the back of the LRU.
pruneSize | minimum number of elements to prune |
pruneHook | a custom pruneHook function |
failSafe | true 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().
|
inline |
Definition at line 405 of file EvictingCacheMap.h.
Referenced by TEST().
|
inline |
Definition at line 412 of file EvictingCacheMap.h.
|
inline |
Definition at line 408 of file EvictingCacheMap.h.
Referenced by TEST().
|
inline |
Definition at line 415 of file EvictingCacheMap.h.
|
inline |
Set a key-value pair in the dictionary
key | key to associate with value |
value | value to associate with the key |
promote | boolean 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. |
pruneHook | callback to use on eviction (if it occurs). |
Definition at line 314 of file EvictingCacheMap.h.
Referenced by TEST().
|
inline |
|
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.
maxSize | new maximum size of the cache map. |
pruneHook | callback to use on eviction. |
Definition at line 193 of file EvictingCacheMap.h.
Referenced by TEST().
|
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.
pruneHook | new callback to use on eviction. |
promote | boolean flag indicating whether or not to move something to the front of an LRU. |
Definition at line 369 of file EvictingCacheMap.h.
Referenced by wangle::LocalSSLSessionCache::LocalSSLSessionCache(), TEST(), and folly::EvictingCacheMap< std::string, SSL_SESSION * >::~EvictingCacheMap().
|
inline |
Get the number of elements in the dictionary
Definition at line 342 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(), TEST(), and folly::EvictingCacheMap< std::string, SSL_SESSION * >::~EvictingCacheMap().
|
private |
|
private |
Definition at line 511 of file EvictingCacheMap.h.
Referenced by folly::EvictingCacheMap< std::string, SSL_SESSION * >::empty(), 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 * >::findInIndex(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::findWithoutPromotion(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::pruneWithFailSafeOption(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::set(), and folly::EvictingCacheMap< std::string, SSL_SESSION * >::size().
|
private |
Definition at line 507 of file EvictingCacheMap.h.
|
private |
Definition at line 508 of file EvictingCacheMap.h.
|
private |
Definition at line 510 of file EvictingCacheMap.h.
Referenced by folly::EvictingCacheMap< std::string, SSL_SESSION * >::findInIndex().
|
private |
Definition at line 509 of file EvictingCacheMap.h.
Referenced by folly::EvictingCacheMap< std::string, SSL_SESSION * >::findInIndex().
|
staticprivate |
Definition at line 504 of file EvictingCacheMap.h.
|
private |
Definition at line 512 of file EvictingCacheMap.h.
Referenced by folly::EvictingCacheMap< std::string, SSL_SESSION * >::begin(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::cbegin(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::cend(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::crbegin(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::crend(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::end(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::erase(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::find(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::findWithoutPromotion(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::pruneWithFailSafeOption(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::rbegin(), folly::EvictingCacheMap< std::string, SSL_SESSION * >::rend(), and folly::EvictingCacheMap< std::string, SSL_SESSION * >::set().
|
private |
|
private |
Definition at line 506 of file EvictingCacheMap.h.
|
private |
Definition at line 505 of file EvictingCacheMap.h.
Referenced by folly::EvictingCacheMap< std::string, SSL_SESSION * >::pruneWithFailSafeOption(), and folly::EvictingCacheMap< std::string, SSL_SESSION * >::setPruneHook().