pub struct CacheManager { /* private fields */ }Expand description
Manages the physical cache directory layout and file I/O.
Implementations§
Source§impl CacheManager
impl CacheManager
Sourcepub fn new(root: PathBuf) -> Result<Self>
pub fn new(root: PathBuf) -> Result<Self>
Create a new manager rooted at root. The directory is created if
it does not already exist.
Sourcepub fn path_for(
&self,
context: &AssetContext,
asset_id: &str,
filename: &str,
) -> PathBuf
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.
Sourcepub fn dir_for(&self, context: &AssetContext) -> PathBuf
pub fn dir_for(&self, context: &AssetContext) -> PathBuf
Directory for a given context (relative to the cache root, joined).
Sourcepub fn store(
&self,
context: &AssetContext,
asset_id: &str,
filename: &str,
data: &[u8],
) -> Result<StoredFile>
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.
Sourcepub fn load(&self, absolute: &Path) -> Result<Vec<u8>>
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).
Trait Implementations§
Source§impl Clone for CacheManager
impl Clone for CacheManager
Source§fn clone(&self) -> CacheManager
fn clone(&self) -> CacheManager
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more