Skip to main content

InMemoryStorage

Struct InMemoryStorage 

Source
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

Source

pub fn new() -> Self

Construct an in-memory storage with default settings: a 256 MiB byte cap, no idle eviction, no TTL.

Source

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.

Source

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.

Source

pub fn with_time_to_idle(self, duration: Duration) -> Self

Evict entries that have not been read in this duration. Off by default.

Source

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).

Source

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).

Source

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.

Source

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

Source§

type PutHandle = InMemoryPutHandle

Streaming writer returned by put.
Source§

type StoredEntry = InMemoryEntry

Concrete entry type returned by get.
Source§

async fn get(&self, key: &CacheKey) -> Vec<Self::StoredEntry>

Fetch all entries stored under key. Returns an empty vec when the key has no entries.
Source§

async fn put( &self, key: CacheKey, policy: CachePolicy, ) -> Result<Self::PutHandle>

Open a streaming insert for 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 more
Source§

async fn invalidate(&self, key: &CacheKey)

Remove all entries stored under key.
Source§

impl Clone for InMemoryStorage

Source§

fn clone(&self) -> InMemoryStorage

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for InMemoryStorage

Source§

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

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

impl Default for InMemoryStorage

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> ArchivePointee for T

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.