Skip to main content

ByteLruCache

Struct ByteLruCache 

Source
pub struct ByteLruCache<K, V> { /* private fields */ }
Expand description

A thread-safe LRU cache governed by a caller-supplied byte budget.

Values are stored behind Arc so callers can hold a value without copying it. Each entry carries a strictly positive weight. The cache evicts least-recently-used entries until the sum of weights is at most max_bytes.

The weight is an accounting value supplied by the caller. It should be a conservative estimate of the memory retained by the value; the cache cannot measure an arbitrary V’s heap allocation itself.

Implementations§

Source§

impl<K, V> ByteLruCache<K, V>
where K: Eq + Hash + Send + Sync, V: Send + Sync,

Source

pub fn new(max_bytes: u64, label: impl Into<String>) -> Self

Creates an empty cache with the supplied byte ceiling and label.

A zero ceiling is valid and causes every inserted entry to be immediately evicted. This is useful when a caller disables a cache without changing its construction path.

Source

pub fn label(&self) -> &str

Returns the caller-supplied label for this cache.

Source

pub fn get(&self, key: &K) -> Option<Arc<V>>

Looks up a key and promotes a hit to the most-recently-used position.

§Panics

Panics if another thread poisoned the cache lock.

Source

pub fn insert(&self, key: K, value: Arc<V>, weight_bytes: NonZeroU64)

Inserts or replaces an entry and evicts least-recently-used entries until the byte ceiling is satisfied.

The weight_bytes argument is non-zero by construction, so the cache’s entry count cannot grow without consuming the configured byte budget.

§Panics

Panics if another thread poisoned the cache lock.

Source

pub fn evict(&self, key: &K)

Removes a key if it is present.

§Panics

Panics if another thread poisoned the cache lock.

Source

pub fn current_bytes(&self) -> u64

Returns the sum of the weights currently retained by the cache.

§Panics

Panics if another thread poisoned the cache lock.

Source

pub fn max_bytes(&self) -> u64

Returns the configured byte ceiling.

§Panics

Panics if another thread poisoned the cache lock.

Source

pub fn set_max_bytes(&self, max_bytes: u64)

Changes the byte ceiling and immediately evicts entries if necessary.

§Panics

Panics if another thread poisoned the cache lock.

Source

pub fn stats(&self) -> CacheStats

Returns a point-in-time copy of the cache counters.

Source

pub fn len(&self) -> usize

Returns the number of retained entries.

§Panics

Panics if another thread poisoned the cache lock.

Source

pub fn is_empty(&self) -> bool

Returns whether the cache has no retained entries.

Auto Trait Implementations§

§

impl<K, V> !Freeze for ByteLruCache<K, V>

§

impl<K, V> Forget for ByteLruCache<K, V>

§

impl<K, V> Move for ByteLruCache<K, V>

§

impl<K, V> RefUnwindSafe for ByteLruCache<K, V>
where Mutex<Inner<K, V>>: RefUnwindSafe,

§

impl<K, V> Send for ByteLruCache<K, V>
where Mutex<Inner<K, V>>: Send,

§

impl<K, V> Sync for ByteLruCache<K, V>
where Mutex<Inner<K, V>>: Sync,

§

impl<K, V> Unpin for ByteLruCache<K, V>
where Mutex<Inner<K, V>>: Unpin,

§

impl<K, V> UnsafeUnpin for ByteLruCache<K, V>
where Mutex<Inner<K, V>>: UnsafeUnpin,

§

impl<K, V> UnwindSafe for ByteLruCache<K, V>
where Mutex<Inner<K, V>>: 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.