pub struct InMemoryStorage { /* private fields */ }Expand description
Bounded in-memory cache storage.
Suitable for production reverse-proxy and client-side caching: byte-aware size cap, scan-resistant admission, and concurrent reads and writes on distinct keys without contention.
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.
§Granularity
Eviction is coarse: the unit is one CacheKey (method + URL), and all Vary variants
stored under that key live and die together during eviction. In typical traffic patterns
variants of the same URL are hot or cold together (a single Accept-Encoding is usually
dominant, etc.), so the cost is bounded — at worst we keep a few cold variants resident
alongside one hot variant. This is correct per RFC 9111; the only consequence is slightly
less efficient use of memory than per-variant eviction would give.
§Sizing
The byte cap is enforced over stored body bytes only (the dominant cost); headers and
other metadata are not counted. The per-response cap on Cache::with_max_cacheable_size
interacts independently — that one bounds how large any single response may be; the storage
cap bounds total resident size across the cache.
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 moreSource§impl Debug for InMemoryStorage
impl Debug for InMemoryStorage
Auto Trait Implementations§
impl !RefUnwindSafe for InMemoryStorage
impl !UnwindSafe for InMemoryStorage
impl Freeze for InMemoryStorage
impl Send for InMemoryStorage
impl Sync for InMemoryStorage
impl Unpin for InMemoryStorage
impl UnsafeUnpin for InMemoryStorage
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.