Skip to main content

MemoryCacheStore

Struct MemoryCacheStore 

Source
pub struct MemoryCacheStore { /* private fields */ }
Expand description

In-memory CacheStore with optional TTL and LRU eviction.

Default constructor builds an unbounded store (no TTL, no LRU). Use Self::builder to opt in to limits — e.g. MemoryCacheStore::builder().max_entries(256).max_age(Duration::from_secs(60)).build().

Eviction is checked lazily on get (expired entries are removed) and on put (over-capacity entries are dropped, least-recently-used first). LRU is approximated with a monotonic counter — no doubly-linked list — which keeps the struct small at the cost of O(n) eviction. That’s fine for the caches we expect (≤ a few hundred entries).

Implementations§

Source§

impl MemoryCacheStore

Source

pub fn new() -> Self

Build an unbounded store (no TTL, no LRU).

Source

pub fn builder() -> MemoryCacheStoreBuilder

Start a builder for an LRU- / TTL-bounded store.

Source

pub fn len(&self) -> usize

Number of entries currently cached.

Note: this does not prune expired entries first. Use Self::is_empty or a fresh get to trigger pruning if needed.

§Panics

Panics if the internal mutex was poisoned by a prior panic.

Source

pub fn is_empty(&self) -> bool

true when the store has no entries.

§Panics

Panics if the internal mutex was poisoned by a prior panic.

Trait Implementations§

Source§

impl CacheStore for MemoryCacheStore

Source§

fn get(&self, key: &str) -> Option<CachedEntry>

Look up an entry by key. None is a miss.
Source§

fn put(&self, key: String, value: CachedEntry)

Store an entry. Overwrites any existing value for key.
Source§

impl Debug for MemoryCacheStore

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MemoryCacheStore

Source§

fn default() -> MemoryCacheStore

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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.