A variant of EvictingCacheMap that assigns weights to entries and evicts entries in LRU order to ensure the total weight of all entries stays below some set maximum. ImplicitlyWeighted means this variant derives the weights from the key‐values using a chosen function. TWeightFn must be a type implementing size_t operator()(const TKey&, const Tvalue&)
Synopsis
Declared in <folly/container/WeightedEvictingCacheMap.h>
template<
class TKey,
class TValue,
class TWeightFn,
class THash = HeterogeneousAccessHash<TKey>,
class TKeyEqual = HeterogeneousAccessEqualTo<TKey>>
class ImplicitlyWeightedEvictingCacheMap;
Description
Example usage: if TKey and TValue are std::string, the weight could be the sum of the string sizes (already stored in the key and value) so that the total weight approximates the total memory usage.
Also consider WeightedEvictingCacheMap below, which tracks weights explicitly, along with keys and values.
TValue must be either movable or copyable. TKey must be copyable.
IMPORTANT NOTES: * Returned references, pointers, or iterators are potentially invalid after any pruning operation (set, insert, etc. that might increase total weight), or any set/insert on the same key (which are allowed to create a new entry or modify an existing entry becoming obsolete). * Operations are more restrictive than EvictingCacheMap to reduce the risk of modifying a value in a way that changes its weight without proper tracking. TValue can be a const type if appropriate. * This is NOT a thread‐safe structure. * For simplicity, functions taking a key implicitly inherit type constraints from EvictingCacheMap. (Must either match TKey or EligibleForHeterogeneousFind/Insert.)
This implementation has not been highly optimized and is a wrapper around EvictingCacheMap.
Type Aliases
Name |
Description |
Callback type invoked on eviction with the key and value. |
|
Const iterator over key‐value entries. |
|
Const reverse iterator over key‐value entries. |
Member Functions
Name |
Description |
|
Constructors |
|
Destroys the map, verifying tracked total weight in debug builds. |
Assignment operators |
|
Returns a const iterator to the most recently used entry. |
|
Clear the cache to an empty state. |
|
Typical empty function |
|
Returns a const iterator past the least recently used entry. |
|
Erases any entry with given key or iterator. |
|
Check for existence of a specific key in the map. This operation has no effect on LRU order. |
|
Get the iterator associated with a specific key. This function always promotes a found value to the head of the LRU. See IMPORTANT NOTES above for why this only returns a const_iterator. See also replace(). |
|
Same but without LRU promotion. |
|
Get the value associated with a specific key. This function always promotes a found value to the head of the LRU. The returned reference is const and might be inavlidated by many subsequent operations. See IMPORTANT NOTES above. See also replace(). |
|
Returns total weight of all entries currently in the cache. |
|
Returns the maximum allowed total weight of all entries in the cache. |
|
Same but without LRU promotion. |
|
Returns a const reverse iterator to the least recently used entry. |
|
Returns a const reverse iterator past the most recently used entry. |
|
|
|
|
|
Sets the maximum allowed total weight of all entries in the cache, evicting entries as needed for the new limit. |
|
Set the prune hook, which is the function invoked on the key and value on each eviction. An operation will throw if the pruneHook throws. Note that this prune hook is not automatically called on entries explicitly erase()ed nor on remaining entries at destruction time. |
|
Get the number of elements in the dictionary |
Static Data Members
Name |
Description |
Approximate memory used by each cache entry. |
Friends
Name |
Description |
A variant of EvictingCacheMap that tracks weights for entries and evicts entries in LRU order to ensure the total weight of all entries stays below some set maximum. Weights are stored as a size_t with each entry. |
Created with MrDocs