Skip to main content

BoundedCache

Struct BoundedCache 

Source
pub struct BoundedCache<C> { /* private fields */ }
Expand description

A cache wrapper that caps total memory consumption by byte count.

Memory is tracked as key.len() + value.len() for every live entry. When inserting a new entry would exceed max_bytes, the oldest entries (tracked in insertion order) are evicted until there is enough budget.

§Type parameters

  • C: an inner Cache<Vec<u8>, Vec<u8>> implementation.

Implementations§

Source§

impl<C> BoundedCache<C>
where C: Cache<Vec<u8>, Vec<u8>>,

Source

pub fn new(inner: C, max_bytes: usize) -> Self

Create a new BoundedCache wrapping inner with a hard byte budget.

max_bytes is the maximum combined byte size of all keys and values.

Source

pub fn current_bytes(&self) -> usize

Return the current byte usage.

Source

pub fn max_bytes(&self) -> usize

Return the maximum byte budget.

Trait Implementations§

Source§

impl<C> Cache<Vec<u8>, Vec<u8>> for BoundedCache<C>
where C: Cache<Vec<u8>, Vec<u8>>,

Source§

fn get(&mut self, key: &Vec<u8>) -> Option<&Vec<u8>>

Look up key, returning a reference to the value if present. Read more
Source§

fn put(&mut self, key: Vec<u8>, value: Vec<u8>) -> Option<Vec<u8>>

Insert or update key -> value without a TTL. Read more
Source§

fn put_with_ttl( &mut self, key: Vec<u8>, value: Vec<u8>, ttl: Duration, ) -> Option<Vec<u8>>

Insert or update key -> value with a time-to-live. Read more
Source§

fn len(&self) -> usize

Return the number of live entries currently in the cache.
Source§

fn cap(&self) -> usize

Return the maximum number of entries the cache can hold.
Source§

fn remove(&mut self, key: &Vec<u8>) -> Option<Vec<u8>>

Remove the entry associated with key, returning its value if present. Read more
Source§

fn clear(&mut self)

Remove all entries from the cache.
Source§

fn peek(&self, key: &Vec<u8>) -> Option<&Vec<u8>>

Look up key without updating access metadata (no promotion). Read more
Source§

fn contains_key(&self, key: &Vec<u8>) -> bool

Return true if key is present in the cache (without promotion). Read more
Source§

fn resize(&mut self, new_cap: usize)

Dynamically resize the cache capacity. Read more
Source§

fn is_empty(&self) -> bool

Return true if the cache holds no entries.
Source§

fn get_or_insert(&mut self, key: K, default: impl FnOnce() -> V) -> &V
where K: Clone,

Return &V for key, inserting default() if the key is absent. Read more
Source§

fn values(&self) -> Vec<&V>

Return all live (non-expired) values currently stored in the cache. Read more
Source§

fn warm(&mut self, iter: impl IntoIterator<Item = (K, V)>)

Pre-populate the cache with (key, value) pairs from an iterator. Read more

Auto Trait Implementations§

§

impl<C> Freeze for BoundedCache<C>
where C: Freeze,

§

impl<C> RefUnwindSafe for BoundedCache<C>
where C: RefUnwindSafe,

§

impl<C> Send for BoundedCache<C>
where C: Send,

§

impl<C> Sync for BoundedCache<C>
where C: Sync,

§

impl<C> Unpin for BoundedCache<C>
where C: Unpin,

§

impl<C> UnsafeUnpin for BoundedCache<C>
where C: UnsafeUnpin,

§

impl<C> UnwindSafe for BoundedCache<C>
where C: 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.