Skip to main content

InMemoryCache

Struct InMemoryCache 

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

In-memory cache for L1 tier.

The cache is bounded: it holds at most max_entries live entries. Expired entries are evicted lazily on read and eagerly when making room for a new key; when the map is full of live entries the one nearest to expiry is evicted to admit a new write.

§Eviction cost

Eviction order is maintained incrementally rather than recomputed: entries carrying a TTL are tracked in a min-heap keyed by expiry, and entries without one in an insertion-ordered queue. Making room is therefore O(log n) (amortised) instead of the full O(n) map scan a naive min_by_key would need on every admission once the map is full — which, because every L2 -> L1 promotion in TieredCache::get is such an admission, otherwise put a full scan of the (10,000-entry by default) map on the hot read path and made filling the cache O(n^2).

Implementations§

Source§

impl InMemoryCache

Source

pub fn new() -> Self

Create a new in-memory cache bounded to DEFAULT_MAX_ENTRIES entries.

Source

pub fn with_capacity(max_entries: usize) -> Self

Create a new in-memory cache bounded to max_entries live entries.

Pass 0 for an explicitly unbounded cache (growth is then the caller’s responsibility).

Source

pub async fn len(&self) -> usize

Number of entries currently held (including any not-yet-evicted expired ones). Primarily useful for tests and capacity assertions.

Source

pub async fn is_empty(&self) -> bool

Whether the cache currently holds no entries.

Source

pub async fn cleanup_expired(&self)

Eagerly remove every expired entry in one pass.

Expired entries are also reclaimed lazily (on read) and opportunistically (when making room for a new write); this method exposes an explicit full sweep for callers that want to reclaim memory proactively.

Trait Implementations§

Source§

impl CacheStore for InMemoryCache

Source§

fn get_json<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = CacheResult<Option<String>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Get a JSON value from the cache. Read more
Source§

fn set_json<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, value: String, ttl: Option<Duration>, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Set a JSON value in the cache. Read more
Source§

fn delete<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Delete a key from the cache. Read more
Source§

fn exists<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = CacheResult<bool>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Check if a key exists in the cache. Read more
Source§

fn clear<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Clear all keys from the cache. Read more
Source§

fn ttl<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = CacheResult<Option<Duration>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Get the TTL (time-to-live) of a key. Read more
Source§

fn expire<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ttl: Duration, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Set or update the expiration time for a key. Read more
Source§

fn increment<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, delta: i64, ) -> Pin<Box<dyn Future<Output = CacheResult<i64>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Increment a numeric value. Read more
Source§

fn decrement<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, delta: i64, ) -> Pin<Box<dyn Future<Output = CacheResult<i64>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Decrement a numeric value. Read more
Source§

fn set_json_forever<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, value: String, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Set a JSON value that never expires, explicitly bypassing any configured CacheConfig::default_ttl. Read more
Source§

fn mget<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, keys: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = CacheResult<Vec<Option<String>>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Get multiple keys in a single batch operation. Read more
Source§

fn mset<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, items: &'life1 [(&'life2 str, String)], ttl: Option<Duration>, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Set multiple key/value pairs in a single batch operation. Read more
Source§

fn mdel<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, keys: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Delete multiple keys in a single batch operation. Read more
Source§

fn get_many<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, keys: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = CacheResult<Vec<Option<String>>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Get multiple keys in parallel. Read more
Source§

fn set_many<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, items: &'life1 [(&'life2 str, String)], ttl: Option<Duration>, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Set multiple key-value pairs in parallel. Read more
Source§

fn delete_many<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, keys: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Delete multiple keys in parallel. Read more
Source§

fn exists_many<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, keys: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = CacheResult<Vec<bool>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Check existence of multiple keys in parallel. Read more
Source§

fn ttl_many<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, keys: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = CacheResult<Vec<Option<Duration>>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Get TTL for multiple keys in parallel. Read more
Source§

fn supports_atomic_sets(&self) -> bool

Whether this backend’s Self::set_add/Self::set_remove/ Self::set_members are backed by a native, atomic set type rather than the trait’s default non-atomic read-modify-write. Read more
Source§

fn set_add<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, set_key: &'life1 str, member: &'life2 str, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Add member to the persistent string set stored at set_key. Read more
Source§

fn set_remove<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, set_key: &'life1 str, member: &'life2 str, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Remove member from the persistent string set stored at set_key. Read more
Source§

fn set_add_many<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, set_key: &'life1 str, members: &'life2 [&'life3 str], ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

Add every member of members to the set stored at set_key. Read more
Source§

fn set_remove_many<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, set_key: &'life1 str, members: &'life2 [&'life3 str], ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

Remove every member of members from the set stored at set_key. Read more
Source§

fn set_members<'life0, 'life1, 'async_trait>( &'life0 self, set_key: &'life1 str, ) -> Pin<Box<dyn Future<Output = CacheResult<Vec<String>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Read every member of the persistent string set stored at set_key. Read more
Source§

impl Default for InMemoryCache

Source§

fn default() -> Self

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.