Struct pingora_lru::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 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 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.

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

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

§

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

§

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