pub struct InMemoryStorage { /* private fields */ }Expand description
Bounded in-memory cache storage.
Defaults to a 256 MiB byte cap; override with
with_max_capacity_bytes,
unbounded,
with_time_to_idle, and
with_time_to_live. Each setter
discards any previously inserted entries; configure at
construction, before the storage is populated or shared.
Clone is cheap — clones share the same backing storage.
Implementations§
Source§impl InMemoryStorage
impl InMemoryStorage
Sourcepub fn new() -> Self
pub fn new() -> Self
Construct an in-memory storage with default settings: a 256 MiB byte cap, no idle eviction, no TTL.
Sourcepub fn with_max_capacity_bytes(self, bytes: u64) -> Self
pub fn with_max_capacity_bytes(self, bytes: u64) -> Self
Set the maximum total stored body size, in bytes. Entries are evicted when inserts would exceed this cap. Defaults to 256 MiB.
Sourcepub fn unbounded(self) -> Self
pub fn unbounded(self) -> Self
Remove the size cap. The cache grows without bound. Useful in tests and short-lived processes; production deployments should prefer the default capped configuration.
Sourcepub fn with_time_to_idle(self, duration: Duration) -> Self
pub fn with_time_to_idle(self, duration: Duration) -> Self
Evict entries that have not been read in this duration. Off by default.
Sourcepub fn with_time_to_live(self, duration: Duration) -> Self
pub fn with_time_to_live(self, duration: Duration) -> Self
Evict entries this duration after their last insert, regardless of access. Off by default.
Note: this is independent of RFC 9111 freshness — a stored
entry may be evicted by TTL while still within its
max-age/s-maxage window, or remain past it (the
CachePolicy handles freshness on read).
Sourcepub fn entry_count(&self) -> u64
pub fn entry_count(&self) -> u64
Approximate count of stored CacheKeys. Each key may hold
multiple Vary variants. Eventually consistent — call
run_pending_tasks first for a
settled value (useful in tests).
Sourcepub fn weighted_size(&self) -> u64
pub fn weighted_size(&self) -> u64
Approximate total weighted size (sum of stored body bytes
across all entries). Eventually consistent — call
run_pending_tasks first for a
settled value.
Sourcepub async fn run_pending_tasks(&self)
pub async fn run_pending_tasks(&self)
Flush pending eviction/insertion bookkeeping. Call before
reading entry_count or
weighted_size when an exact value
matters.
Trait Implementations§
Source§impl CacheStorage for InMemoryStorage
impl CacheStorage for InMemoryStorage
Source§type PutHandle = InMemoryPutHandle
type PutHandle = InMemoryPutHandle
put.Source§type StoredEntry = InMemoryEntry
type StoredEntry = InMemoryEntry
get.Source§async fn get(&self, key: &CacheKey) -> Vec<Self::StoredEntry>
async fn get(&self, key: &CacheKey) -> Vec<Self::StoredEntry>
key. Returns an empty vec when
the key has no entries.Source§async fn put(
&self,
key: CacheKey,
policy: CachePolicy,
) -> Result<Self::PutHandle>
async fn put( &self, key: CacheKey, policy: CachePolicy, ) -> Result<Self::PutHandle>
key with the supplied policy.
Returns a PutHandle that the caller writes body bytes into,
then closes with PutHandle::finalize. If an existing entry
has the same Vary signature, finalize replaces it; otherwise
the new entry is appended. Read moreSource§async fn invalidate(&self, key: &CacheKey)
async fn invalidate(&self, key: &CacheKey)
key.Source§impl Clone for InMemoryStorage
impl Clone for InMemoryStorage
Source§fn clone(&self) -> InMemoryStorage
fn clone(&self) -> InMemoryStorage
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more