Skip to main content

AssetManager

Struct AssetManager 

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

Top-level asset cache.

Implementations§

Source§

impl AssetManager

Source

pub fn from_config(config: AssetConfig) -> Result<Self>

Build a new manager from a raw AssetConfig.

Source

pub fn from_resolved(config: ResolvedAssetConfig) -> Result<Self>

Build a new manager from a validated ResolvedAssetConfig.

Startup flow:

  1. Load the on-disk index (recovering an empty one if the file is missing or corrupt).
  2. Integrity check — drop entries whose file is gone or whose stored path resolves outside the cache root.
  3. Rotate — enforce max_file_age and max_cache_size on the loaded state so a cache that was closed over budget comes back up within limits before any new writes.
  4. Persist the resulting index.
Source

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

Convenience constructor for tests, standalone usage, and minimal environments (containers, CI) where dirs::cache_dir() may return None.

Unlike AssetManager::from_config, this bypasses OS cache-dir discovery entirely and uses sensible defaults for all other settings (1 GiB budget, 7-day TTL, LRU eviction).

Source

pub fn cache_dir(&self) -> &Path

Absolute path to the cache directory.

Source

pub fn config(&self) -> &ResolvedAssetConfig

Full resolved configuration.

Source

pub fn store(&self, request: StoreRequest<'_>) -> Result<CachedAsset>

Store a new asset, persisting the updated index and running a rotation pass. Returns the newly created CachedAsset.

If the payload is larger than the configured ResolvedAssetConfig::max_cache_size this returns an AssetError::Config error without touching the filesystem — otherwise rotation would immediately evict the just-written file and we’d hand back an asset id that no longer resolves.

A max_cache_size of 0 is treated as unlimited. Both this method and Rotator::rotate share that convention so the two cannot disagree about whether the budget is exhausted.

Source

pub fn get(&self, asset_id: &str) -> Result<Option<ResolvedAsset>>

Look up an asset by id and return the absolute path on disk if it is still cached. Also touches last_accessed and persists the index if the asset was found.

The returned ResolvedAsset::asset reflects the post-touch state, so asset.last_accessed_ms is the timestamp just written to the index rather than the stale pre-touch value.

Source

pub fn delete(&self, asset_id: &str) -> Result<bool>

Delete an asset from the cache (both the index entry and the file). Returns true if the asset was present.

Source

pub fn list(&self) -> Result<Vec<CachedAsset>>

List all assets currently tracked.

Returns AssetError::Poisoned if the in-memory index mutex is poisoned; callers that want a best-effort snapshot can .unwrap_or_default() on the result.

Source

pub fn total_size(&self) -> Result<u64>

Total tracked bytes in the cache.

Returns AssetError::Poisoned if the in-memory index mutex is poisoned.

Source

pub fn rotate_now(&self) -> Result<RotationStats>

Force a rotation pass immediately.

Source

pub fn integrity_check(&self) -> Result<usize>

Re-check index vs filesystem. Returns the number of dropped entries.

Source

pub fn index_path(&self) -> PathBuf

Path to the on-disk index file. Useful for diagnostics and tests.

Source

pub fn content_id(data: &[u8]) -> String

Compute a content-addressed asset ID from raw bytes.

The returned string has the form sha256:{16 hex chars} (64-bit prefix of the SHA-256 digest). This is the same ID that AssetManager::store generates when asset_id is None.

Use this to check whether a file is already cached before downloading it:

let id = AssetManager::content_id(data);
if manager.get(&id)?.is_some() { /* already cached */ }

Trait Implementations§

Source§

impl Clone for AssetManager

Source§

fn clone(&self) -> AssetManager

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 AssetManager

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