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