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

PruneHookCall

Callback type invoked on eviction with the key and value.

const_iterator

Const iterator over key‐value entries.

const_reverse_iterator

Const reverse iterator over key‐value entries.

Member Functions

Name

Description

ImplicitlyWeightedEvictingCacheMap [constructor]

Constructors

~ImplicitlyWeightedEvictingCacheMap [destructor]

Destroys the map, verifying tracked total weight in debug builds.

operator=

Assignment operators

begin

Returns a const iterator to the most recently used entry.

clear

Clear the cache to an empty state.

empty

Typical empty function

end

Returns a const iterator past the least recently used entry.

erase

Erases any entry with given key or iterator.

exists

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

find

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().

findWithoutPromotion

Same but without LRU promotion.

get

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().

getCurrentTotalWeight

Returns total weight of all entries currently in the cache.

getMaxTotalWeight

Returns the maximum allowed total weight of all entries in the cache.

getWithoutPromotion

Same but without LRU promotion.

rbegin

Returns a const reverse iterator to the least recently used entry.

rend

Returns a const reverse iterator past the most recently used entry.

replace

replace overloads

set

set overloads

setMaxTotalWeight

Sets the maximum allowed total weight of all entries in the cache, evicting entries as needed for the new limit.

setPruneHook

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.

size

Get the number of elements in the dictionary

Static Data Members

Name

Description

kApproximateEntryMemUsage

Approximate memory used by each cache entry.

Friends

Name

Description

folly::WeightedEvictingCacheMap

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