Skip to main content

Lru

Struct Lru 

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

Source

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

Source

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.

Source

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

Source

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.

Source

pub fn promote(&self, key: u64) -> bool

Promote the key to the head of the LRU

Return true if the key exists.

Source

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

Source

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

Source

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.

Source

pub fn remove(&self, key: u64) -> Option<(T, usize)>

Remove the given asset.

Source

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

Source

pub fn peek(&self, key: u64) -> bool

Check existence of a key without changing the order in LRU.

Source

pub fn peek_weight(&self, key: u64) -> Option<usize>

Check the weight of a key without changing the order in LRU.

Source

pub fn weight(&self) -> usize

Return the current total weight.

Source

pub fn evicted_weight(&self) -> usize

Return the total weight of items evicted from this Lru.

Source

pub fn evicted_len(&self) -> usize

Return the total count of items evicted from this Lru.

Source

pub fn len(&self) -> usize

The number of items inside this Lru.

Source

pub fn iter_for_each<F>(&self, shard: usize, f: F)
where F: FnMut((&T, usize)),

Scan a shard with the given function F

Source

pub const fn shards(&self) -> usize

Get the total number of shards

Source

pub fn shard_len(&self, shard: usize) -> usize

Get the number of items inside a shard

Source

pub fn shard_weight(&self, shard: usize) -> usize

Get the weight (total size) inside a shard

Auto Trait Implementations§

§

impl<T, const N: usize> !Freeze for Lru<T, N>

§

impl<T, const N: usize> !RefUnwindSafe for Lru<T, N>

§

impl<T, const N: usize> Send for Lru<T, N>
where T: Send,

§

impl<T, const N: usize> Sync for Lru<T, N>
where T: Send + Sync,

§

impl<T, const N: usize> Unpin for Lru<T, N>

§

impl<T, const N: usize> UnsafeUnpin for Lru<T, N>

§

impl<T, const N: usize> UnwindSafe for Lru<T, N>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V