Skip to main content

CacheManager

Struct CacheManager 

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

Manages the physical cache directory layout and file I/O.

Implementations§

Source§

impl CacheManager

Source

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

Create a new manager rooted at root. The directory is created if it does not already exist.

Source

pub fn root(&self) -> &Path

Absolute path to the cache root.

Source

pub fn path_for( &self, context: &AssetContext, asset_id: &str, filename: &str, ) -> PathBuf

Compute the on-disk path for an asset given its context and filename.

Layout:

{root}/{context_dir}/{context_id}/{asset_id}-{safe_filename}

Both asset_id and filename are sanitized before becoming a single path component — any directory separators or .. sequences in the inputs are replaced with _, so calling path_for with hostile input can never escape the per-context directory.

An 8-character SHA-256 prefix of the raw asset_id is embedded in the filename to avoid collisions between IDs that differ only in characters collapsed by sanitization. 8 hex chars = 32 bits gives ~4 billion buckets — collision probability via birthday paradox is negligible for a rotated local cache (< 0.0001% with 100 files per context). We intentionally keep the hash short to stay well within the 255-char filename limit on ext4 / NTFS / APFS.

Source

pub fn dir_for(&self, context: &AssetContext) -> PathBuf

Directory for a given context (relative to the cache root, joined).

Source

pub fn store( &self, context: &AssetContext, asset_id: &str, filename: &str, data: &[u8], ) -> Result<StoredFile>

Store bytes for an asset and return the absolute path where they were written along with the SHA-256 checksum.

Parent directories are created as needed. Writes go through a temp file + rename so partial writes are never observable.

Source

pub fn load(&self, absolute: &Path) -> Result<Vec<u8>>

Read a file from the cache by absolute path. Returns NotFound if the file is missing (via AssetError::Io).

Source

pub fn delete(&self, absolute: &Path) -> Result<()>

Delete a file from the cache. Missing files are treated as success so that retries / idempotent deletes work as expected.

Source

pub fn exists(&self, absolute: &Path) -> bool

Check whether a file exists in the cache.

Trait Implementations§

Source§

impl Clone for CacheManager

Source§

fn clone(&self) -> CacheManager

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 CacheManager

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

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more