Struct odilia_cache::Cache
source · pub struct Cache {
pub by_id: Arc<DashMap<AccessiblePrimitive, Arc<RwLock<CacheItem>>, FxBuildHasher>>,
pub connection: Connection,
}Expand description
An internal cache used within Odilia.
This contains (mostly) all accessibles in the entire accessibility tree, and they are referenced by their IDs. If you are having issues with incorrect or invalid accessibles trying to be accessed, this is code is probably the issue.
Fields§
§by_id: Arc<DashMap<AccessiblePrimitive, Arc<RwLock<CacheItem>>, FxBuildHasher>>§connection: ConnectionImplementations§
source§impl Cache
impl Cache
sourcepub fn new(conn: Connection) -> Self
pub fn new(conn: Connection) -> Self
create a new, fresh cache
sourcepub fn add(&self, cache_item: CacheItem)
pub fn add(&self, cache_item: CacheItem)
add a single new item to the cache. Note that this will empty the bucket
before inserting the CacheItem into the cache (this is so there is
never two items with the same ID stored in the cache at the same time).
pub fn add_ref( &self, id: AccessiblePrimitive, cache_item: Arc<RwLock<CacheItem>> )
sourcepub fn remove(&self, id: &AccessiblePrimitive)
pub fn remove(&self, id: &AccessiblePrimitive)
Remove a single cache item
sourcepub fn get_ref(
&self,
id: &AccessiblePrimitive
) -> Option<Arc<RwLock<CacheItem>>>
pub fn get_ref( &self, id: &AccessiblePrimitive ) -> Option<Arc<RwLock<CacheItem>>>
Get a single item (mutable via lock) from the cache.
sourcepub fn get(&self, id: &AccessiblePrimitive) -> Option<CacheItem>
pub fn get(&self, id: &AccessiblePrimitive) -> Option<CacheItem>
Get a single item from the cache.
This will allow you to get the item without holding any locks to it, at the cost of (1) a clone and (2) no guarantees that the data is kept up-to-date.
sourcepub fn get_all(&self, ids: &[AccessiblePrimitive]) -> Vec<Option<CacheItem>>
pub fn get_all(&self, ids: &[AccessiblePrimitive]) -> Vec<Option<CacheItem>>
get a many items from the cache; this only creates one read handle (note that this will copy all data you would like to access)
sourcepub fn add_all(&self, cache_items: Vec<CacheItem>)
pub fn add_all(&self, cache_items: Vec<CacheItem>)
Bulk add many items to the cache; only one accessible should ever be associated with an id.
sourcepub fn remove_all(&self, ids: Vec<AccessiblePrimitive>)
pub fn remove_all(&self, ids: Vec<AccessiblePrimitive>)
Bulk remove all ids in the cache; this only refreshes the cache after removing all items.
sourcepub fn modify_item<F>(&self, id: &AccessiblePrimitive, modify: F) -> boolwhere
F: FnOnce(&mut CacheItem),
pub fn modify_item<F>(&self, id: &AccessiblePrimitive, modify: F) -> boolwhere F: FnOnce(&mut CacheItem),
Edit a mutable CacheItem. Returns true if the update was successful.
Note: an exclusive lock for the given cache item will be placed for the entire length of the passed function, so try to avoid any compute in it.
sourcepub async fn get_or_create(
&self,
accessible: &AccessibleProxy<'_>,
cache: Weak<Self>
) -> OdiliaResult<CacheItem>
pub async fn get_or_create( &self, accessible: &AccessibleProxy<'_>, cache: Weak<Self> ) -> OdiliaResult<CacheItem>
Get a single item from the cache (note that this copies some integers to a new struct). If the CacheItem is not found, create one, add it to the cache, and return it.