Struct sierra::ResourceCache
source · pub struct ResourceCache<T> { /* private fields */ }
Expand description
General purpose cache for sierra resources. This cache evicts resources based on their last used epoch. Each time a resource is used, its last used epoch is updated to the current. The cache evicts all resources that have not been used for a certain amount of epochs.
This strategy works best for resources that may become obsolete and require substantial amount of memory while only few resources are in use so cache size is always low.
Implementations§
source§impl<T> ResourceCache<T>
impl<T> ResourceCache<T>
sourcepub fn with_capacity(cap: usize) -> Self
pub fn with_capacity(cap: usize) -> Self
Creates a new cache with preallocated resource capacity.
sourcepub fn fetch<F>(&mut self, eq: F) -> Option<&T>where
F: Fn(&T) -> bool,
pub fn fetch<F>(&mut self, eq: F) -> Option<&T>where
F: Fn(&T) -> bool,
Fetches resource from cache.
Resource last used epoch is updated to the current epoch.
Returns None
if resource is not in cache.
Returns Some
if resource is in cache.
sourcepub fn get<Q>(&mut self, key: &Q) -> Option<&T>where
T: Borrow<Q>,
Q: ?Sized + Hash + Eq,
pub fn get<Q>(&mut self, key: &Q) -> Option<&T>where
T: Borrow<Q>,
Q: ?Sized + Hash + Eq,
Fetches resource from cache.
Resource last used epoch is updated to the current epoch.
Returns None
if resource is not in cache.
Returns Some
if resource is in cache.
sourcepub fn fetch_no_update<F>(&self, eq: F) -> Option<&T>where
F: Fn(&T) -> bool,
pub fn fetch_no_update<F>(&self, eq: F) -> Option<&T>where
F: Fn(&T) -> bool,
Fetches resource from cache.
Resource last used epoch is not updated.
This is useful when only shared reference to cache is available.
Returns None
if resource is not in cache.
Returns Some
if resource is in cache.
sourcepub fn get_no_update<Q>(&self, key: &Q) -> Option<&T>where
T: Borrow<Q>,
Q: ?Sized + Hash + Eq,
pub fn get_no_update<Q>(&self, key: &Q) -> Option<&T>where
T: Borrow<Q>,
Q: ?Sized + Hash + Eq,
Fetches resource from cache.
Resource last used epoch is not updated.
This is useful when only shared reference to cache is available.
Returns None
if resource is not in cache.
Returns Some
if resource is in cache.
sourcepub fn try_fetch_or_create<K, F, E>(&mut self, eq: K, create: F) -> Result<&T, E>where
K: Fn(&T) -> bool,
F: FnOnce() -> Result<T, E>,
pub fn try_fetch_or_create<K, F, E>(&mut self, eq: K, create: F) -> Result<&T, E>where
K: Fn(&T) -> bool,
F: FnOnce() -> Result<T, E>,
Fetches resource from cache. Resource last used epoch is updated to the current epoch. If resource is not in cache, it is created and added to the cache. Returns a reference to the resource. Returns error if resource is not in cache and create function fails.
sourcepub fn fetch_or_create<E, F>(&mut self, eq: E, create: F) -> &Twhere
E: Fn(&T) -> bool,
F: FnOnce() -> T,
pub fn fetch_or_create<E, F>(&mut self, eq: E, create: F) -> &Twhere
E: Fn(&T) -> bool,
F: FnOnce() -> T,
Fetches resource from cache. Resource last used epoch is updated to the current epoch. If resource is not in cache, it is created and added to the cache. Returns a reference to the resource.
sourcepub fn try_get_or_create<Q, F, E>(&mut self, key: &Q, create: F) -> Result<&T, E>where
T: Borrow<Q>,
Q: ?Sized + Hash + Eq,
F: FnOnce() -> Result<T, E>,
pub fn try_get_or_create<Q, F, E>(&mut self, key: &Q, create: F) -> Result<&T, E>where
T: Borrow<Q>,
Q: ?Sized + Hash + Eq,
F: FnOnce() -> Result<T, E>,
Fetches resource from cache. Resource last used epoch is updated to the current epoch. If resource is not in cache, it is created and added to the cache. Returns a reference to the resource.
sourcepub fn get_or_create<Q, F>(&mut self, key: &Q, create: F) -> &Twhere
T: Borrow<Q>,
Q: ?Sized + Hash + Eq,
F: FnOnce() -> T,
pub fn get_or_create<Q, F>(&mut self, key: &Q, create: F) -> &Twhere
T: Borrow<Q>,
Q: ?Sized + Hash + Eq,
F: FnOnce() -> T,
Fetches resource from cache. Resource last used epoch is updated to the current epoch. If resource is not in cache, it is created and added to the cache. Returns a reference to the resource.
sourcepub fn next_epoch(&mut self)
pub fn next_epoch(&mut self)
Moves to the next epoch.