pub struct AssetManager { /* private fields */ }Expand description
Top-level asset cache.
Implementations§
Source§impl AssetManager
impl AssetManager
Sourcepub fn from_config(config: AssetConfig) -> Result<Self>
pub fn from_config(config: AssetConfig) -> Result<Self>
Build a new manager from a raw AssetConfig.
Sourcepub fn from_resolved(config: ResolvedAssetConfig) -> Result<Self>
pub fn from_resolved(config: ResolvedAssetConfig) -> Result<Self>
Build a new manager from a validated ResolvedAssetConfig.
Startup flow:
- Load the on-disk index (recovering an empty one if the file is missing or corrupt).
- Integrity check — drop entries whose file is gone or whose stored path resolves outside the cache root.
- Rotate — enforce
max_file_ageandmax_cache_sizeon the loaded state so a cache that was closed over budget comes back up within limits before any new writes. - Persist the resulting index.
Sourcepub fn with_root(root: PathBuf) -> Result<Self>
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).
Sourcepub fn config(&self) -> &ResolvedAssetConfig
pub fn config(&self) -> &ResolvedAssetConfig
Full resolved configuration.
Sourcepub fn store(&self, request: StoreRequest<'_>) -> Result<CachedAsset>
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.
Sourcepub fn get(&self, asset_id: &str) -> Result<Option<ResolvedAsset>>
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.
Sourcepub fn delete(&self, asset_id: &str) -> Result<bool>
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.
Sourcepub fn list(&self) -> Result<Vec<CachedAsset>>
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.
Sourcepub fn total_size(&self) -> Result<u64>
pub fn total_size(&self) -> Result<u64>
Total tracked bytes in the cache.
Returns AssetError::Poisoned if the in-memory index mutex is
poisoned.
Sourcepub fn rotate_now(&self) -> Result<RotationStats>
pub fn rotate_now(&self) -> Result<RotationStats>
Force a rotation pass immediately.
Sourcepub fn integrity_check(&self) -> Result<usize>
pub fn integrity_check(&self) -> Result<usize>
Re-check index vs filesystem. Returns the number of dropped entries.
Sourcepub fn index_path(&self) -> PathBuf
pub fn index_path(&self) -> PathBuf
Path to the on-disk index file. Useful for diagnostics and tests.
Sourcepub fn content_id(data: &[u8]) -> String
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
impl Clone for AssetManager
Source§fn clone(&self) -> AssetManager
fn clone(&self) -> AssetManager
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more