pub struct CacheManifest {
pub entries: HashMap<String, CacheEntry>,
}Expand description
Manifest tracking all cached assets. Persisted as RON on disk.
Fields§
§entries: HashMap<String, CacheEntry>Implementations§
Source§impl CacheManifest
impl CacheManifest
Sourcepub fn load_from_disk(config: &CacheConfig) -> Result<Self, CacheError>
pub fn load_from_disk(config: &CacheConfig) -> Result<Self, CacheError>
Load the manifest from the filesystem. Returns Default if the file
does not exist.
Sourcepub fn save_to_disk(&self, config: &CacheConfig) -> Result<(), CacheError>
pub fn save_to_disk(&self, config: &CacheConfig) -> Result<(), CacheError>
Persist the manifest to disk as pretty-printed RON.
Sourcepub fn store<R: Read>(
&mut self,
config: &CacheConfig,
key: &str,
extension: &str,
reader: R,
max_age: Option<Duration>,
) -> Result<(), CacheError>
pub fn store<R: Read>( &mut self, config: &CacheConfig, key: &str, extension: &str, reader: R, max_age: Option<Duration>, ) -> Result<(), CacheError>
Store the contents of reader in the cache under key with the given
file extension (e.g. "png"). The file written to disk will be named
"{key}.{extension}". Overwrites any existing entry for the same key.
Accepting a Read instead of a byte slice means the data is streamed
directly to disk without requiring an intermediate in-memory buffer.
Use std::io::Cursor to wrap an existing slice or Vec<u8> when the
data is already in memory.
max_age optionally sets a per-entry lifetime. It only takes effect
when it exceeds CacheConfig::max_age; an entry cannot shorten
its lifetime below the global policy.
Sourcepub fn remove(
&mut self,
config: &CacheConfig,
key: &str,
) -> Result<(), CacheError>
pub fn remove( &mut self, config: &CacheConfig, key: &str, ) -> Result<(), CacheError>
Remove a cache entry and delete its file from disk.
Returns Ok(()) even if the entry did not exist.
Sourcepub fn get(&self, key: &str) -> Option<&CacheEntry>
pub fn get(&self, key: &str) -> Option<&CacheEntry>
Get the entry for a key, if it exists.
Sourcepub fn asset_path(&self, key: &str) -> Option<String>
pub fn asset_path(&self, key: &str) -> Option<String>
Return the Bevy asset path suitable for AssetServer::load.
Uses the cache:// asset source scheme.
Sourcepub fn is_cached(&self, config: &CacheConfig, key: &str) -> bool
pub fn is_cached(&self, config: &CacheConfig, key: &str) -> bool
Check whether a cached asset exists for key — both in the manifest
and on disk. Returns false if the manifest entry is stale (file
was deleted externally).
Sourcepub fn total_size_bytes(&self) -> u64
pub fn total_size_bytes(&self) -> u64
Returns the total size of all cached assets in bytes, as recorded in
the manifest. This is a sum of CacheEntry::size_bytes across all
entries
Sourcepub fn load_cached<A: Asset>(
&self,
config: &CacheConfig,
key: &str,
asset_server: &AssetServer,
) -> Result<Handle<A>, CacheError>
pub fn load_cached<A: Asset>( &self, config: &CacheConfig, key: &str, asset_server: &AssetServer, ) -> Result<Handle<A>, CacheError>
If a cached file exists for key, load it through the AssetServer
and return the typed handle. Returns CacheError::NotFound when
there is no manifest entry or the file is missing from disk.
This lets callers skip regenerating a dynamic asset when a valid cached copy is already available:
fn setup(
manifest: Res<CacheManifest>,
config: Res<CacheConfig>,
asset_server: Res<AssetServer>,
) {
let handle: Handle<Image> = manifest
.load_cached(&config, "scene_thumb", &asset_server)
.unwrap_or_else(|_| generate_and_cache_thumbnail(/* … */));
}Sourcepub fn remove_expired(&mut self, config: &CacheConfig) -> usize
pub fn remove_expired(&mut self, config: &CacheConfig) -> usize
Remove expired entries and delete their files. An entry is expired
when its age exceeds the effective max-age, which is the greater of
CacheConfig::max_age and the entry’s own
CacheEntry::max_age_secs (if set).
Returns the number of entries removed.
Sourcepub fn enforce_max_entries(&mut self, config: &CacheConfig) -> usize
pub fn enforce_max_entries(&mut self, config: &CacheConfig) -> usize
If the manifest exceeds max_entries, evict the oldest entries
(by modified_at) until the limit is satisfied. Returns the number
of entries removed.
Trait Implementations§
Source§impl Clone for CacheManifest
impl Clone for CacheManifest
Source§fn clone(&self) -> CacheManifest
fn clone(&self) -> CacheManifest
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for CacheManifest
impl Debug for CacheManifest
Source§impl Default for CacheManifest
impl Default for CacheManifest
Source§fn default() -> CacheManifest
fn default() -> CacheManifest
Source§impl<'de> Deserialize<'de> for CacheManifest
impl<'de> Deserialize<'de> for CacheManifest
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for CacheManifest
impl PartialEq for CacheManifest
Source§impl Serialize for CacheManifest
impl Serialize for CacheManifest
impl Resource for CacheManifest
impl StructuralPartialEq for CacheManifest
Auto Trait Implementations§
impl Freeze for CacheManifest
impl RefUnwindSafe for CacheManifest
impl Send for CacheManifest
impl Sync for CacheManifest
impl Unpin for CacheManifest
impl UnsafeUnpin for CacheManifest
impl UnwindSafe for CacheManifest
Blanket Implementations§
Source§impl<T, U> AsBindGroupShaderType<U> for T
impl<T, U> AsBindGroupShaderType<U> for T
Source§fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
T ShaderType for self. When used in AsBindGroup
derives, it is safe to assume that all images in self exist.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
Source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Creates Self using default().
Source§impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
Source§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more