Skip to main content

FileSystemStorage

Struct FileSystemStorage 

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

Filesystem-backed cache storage rooted at a directory.

Persists cached responses under a root directory so they survive process restarts. Each response is two files: a <hash>.meta sidecar holding the CachePolicy and any trailers as an rkyv-encoded binary blob, and a <hash>.body holding the raw body bytes and nothing else. Bodies stream in and out — put writes to a temporary file the caller feeds incrementally, and open streams the stored body back without loading it into memory. The metadata is not human-readable; it is optimized for compact, fast loading rather than inspection.

Defaults to a 1 GiB byte cap; override with with_max_capacity_bytes or remove it with unbounded.

Clone is cheap — clones share the same root and capacity index, and see each other’s writes.

§Layout

Entries live at <root>/<key-hash>/<variant-hash>.{meta,body}. The key hash is a SHA-256 of the request method and URL; the variant hash is a SHA-256 of the Vary signature, so the multiple variants of one URL are sibling files in the same directory and get enumerates them by reading that directory. Writing a variant that already exists replaces it.

§Durability

Writes commit by renaming a fully-written temporary file into place, and the .meta is written last — a reader treats it as the commit marker, so a half-written or abandoned entry (a PutHandle dropped without finalize) is never visible to get.

§Capacity

A byte cap (1 GiB by default) bounds the total stored body size. When a write would push the total past the cap, least-recently-used variants are evicted — their .meta and .body files deleted — until the cache fits. The cap counts body bytes only, per variant, matching the granularity of the on-disk layout. Reads count as use, so a frequently-served variant outlives idle ones. Override with with_max_capacity_bytes or remove the cap with unbounded.

The cap is tracked in an in-memory index built by scanning the root at construction, so it survives restarts (recency resets to whatever order the scan encounters). A directory that grew past the current cap under an older, unbounded configuration is trimmed to fit on the next construction.

§Runtime

Filesystem access goes through the runtime selected by the smol, tokio, or async-std feature. Enabling fs without one of those compiles but panics on use.

Implementations§

Source§

impl FileSystemStorage

Source

pub fn new(root: impl Into<PathBuf>) -> Self

Construct a storage rooted at root with a 1 GiB byte cap. The directory is created on demand as entries are written; it need not exist yet. If it exists, it is scanned to seed the capacity index, so previously stored entries count against the cap.

Source

pub fn with_max_capacity_bytes(self, bytes: u64) -> Self

Set the maximum total stored body size, in bytes. Least-recently-used variants are evicted — their files deleted — when a write would exceed this cap. Defaults to 1 GiB. Re-scans the root, so a directory already over the new cap is trimmed to fit.

Source

pub fn unbounded(self) -> Self

Remove the size cap. Stored bytes grow without bound. Useful in tests and short-lived processes; a cache living on shared disk should prefer the default capped configuration.

Source

pub fn weighted_size(&self) -> u64

Approximate total stored body size, in bytes, currently counted against the cap. Eventually consistent — call run_pending_tasks first for a settled value.

Source

pub fn entry_count(&self) -> u64

Approximate count of stored variants. Eventually consistent — call run_pending_tasks first for a settled value.

Source

pub async fn run_pending_tasks(&self)

Flush pending eviction bookkeeping, including deletion of files for evicted variants. Call before reading weighted_size or entry_count when an exact value matters.

Trait Implementations§

Source§

impl CacheStorage for FileSystemStorage

Source§

type PutHandle = FsPutHandle

Streaming writer returned by put.
Source§

type StoredEntry = FsStoredEntry

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 FileSystemStorage

Source§

fn clone(&self) -> FileSystemStorage

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 FileSystemStorage

Source§

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

Formats the value using the given formatter. 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.