pub struct Lru<T, const N: usize> { /* private fields */ }Expand description
The LRU with N shards
Implementations§
Source§impl<T, const N: usize> Lru<T, N>
impl<T, const N: usize> Lru<T, N>
Sourcepub fn with_capacity(weight_limit: usize, capacity: usize) -> Self
pub fn with_capacity(weight_limit: usize, capacity: usize) -> Self
Create an Lru with the given weight limit and predicted capacity.
The capacity is per shard (for simplicity). So the total capacity = capacity * N
Sourcepub fn with_capacity_and_watermark(
weight_limit: usize,
capacity: usize,
len_watermark: Option<usize>,
) -> Self
pub fn with_capacity_and_watermark( weight_limit: usize, capacity: usize, len_watermark: Option<usize>, ) -> Self
Create an Lru with the given weight limit, predicted capacity and optional watermark
The capacity is per shard (for simplicity). So the total capacity = capacity * N
The watermark indicates at what count we should begin evicting and acts as a limit on the total number of allowed items.
Sourcepub fn admit(&self, key: u64, data: T, weight: usize) -> usize
pub fn admit(&self, key: u64, data: T, weight: usize) -> usize
Admit the key value to the Lru
Return the shard index which the asset is added to
Sourcepub fn increment_weight(
&self,
key: u64,
delta: usize,
max_weight: Option<usize>,
) -> usize
pub fn increment_weight( &self, key: u64, delta: usize, max_weight: Option<usize>, ) -> usize
Increment the weight associated with a given key, up to an optional max weight.
If a max_weight is provided, the weight cannot exceed this max weight. If the current
weight is higher than the max, it will be capped to the max.
Return the total new weight. 0 indicates the key did not exist.
Sourcepub fn promote(&self, key: u64) -> bool
pub fn promote(&self, key: u64) -> bool
Promote the key to the head of the LRU
Return true if the key exists.
Sourcepub fn promote_top_n(&self, key: u64, top: usize) -> bool
pub fn promote_top_n(&self, key: u64, top: usize) -> bool
Promote to the top n of the LRU
This function is a bit more efficient in terms of reducing lock contention because it will acquire a write lock only if the key is outside top n but only acquires a read lock when the key is already in the top n.
Return false if the item doesn’t exist
Sourcepub fn evict_shard(&self, shard: u64) -> Option<(T, usize)>
pub fn evict_shard(&self, shard: u64) -> Option<(T, usize)>
Evict at most one item from the given shard
Return the evicted asset and its size if there is anything to evict
Sourcepub fn evict_to_limit(&self) -> Vec<(T, usize)>
pub fn evict_to_limit(&self) -> Vec<(T, usize)>
Evict the Lru until the overall weight is below the limit (or the configured watermark).
Return a list of evicted items.
The evicted items are randomly selected from all the shards.
Sourcepub fn insert_tail(&self, key: u64, data: T, weight: usize) -> bool
pub fn insert_tail(&self, key: u64, data: T, weight: usize) -> bool
Insert the item to the tail of this LRU.
Useful to recreate an LRU in most-to-least order
Sourcepub fn peek(&self, key: u64) -> bool
pub fn peek(&self, key: u64) -> bool
Check existence of a key without changing the order in LRU.
Sourcepub fn peek_weight(&self, key: u64) -> Option<usize>
pub fn peek_weight(&self, key: u64) -> Option<usize>
Check the weight of a key without changing the order in LRU.
Sourcepub fn evicted_weight(&self) -> usize
pub fn evicted_weight(&self) -> usize
Return the total weight of items evicted from this Lru.
Sourcepub fn evicted_len(&self) -> usize
pub fn evicted_len(&self) -> usize
Return the total count of items evicted from this Lru.
Sourcepub fn iter_for_each<F>(&self, shard: usize, f: F)
pub fn iter_for_each<F>(&self, shard: usize, f: F)
Scan a shard with the given function F
Sourcepub fn shard_weight(&self, shard: usize) -> usize
pub fn shard_weight(&self, shard: usize) -> usize
Get the weight (total size) inside a shard