pub struct MemoryCache<T> { /* private fields */ }Expand description
Byte-bounded LRU of inflated entries plus any per-entry state the host
wants to keep alongside (T).
Implementations§
Source§impl<T> MemoryCache<T>
impl<T> MemoryCache<T>
Sourcepub fn new(cap_bytes: usize) -> Self
pub fn new(cap_bytes: usize) -> Self
An empty cache holding at most cap_bytes bytes of entries.
Sourcepub fn get(&mut self, key: &ApkKey, entry_name: &str) -> Option<Arc<T>>
pub fn get(&mut self, key: &ApkKey, entry_name: &str) -> Option<Arc<T>>
The entry cached under entry_name, marked most recently used.
Sourcepub fn insert(
&mut self,
key: &ApkKey,
entry_name: &str,
value: Arc<T>,
size: usize,
)
pub fn insert( &mut self, key: &ApkKey, entry_name: &str, value: Arc<T>, size: usize, )
Cache value under entry_name, charged at size bytes, replacing
any previous entry and evicting least recently used ones past the
capacity.
Sourcepub fn recharge(
&mut self,
key: &ApkKey,
entry_name: &str,
size_of: impl FnOnce(&T) -> usize,
) -> bool
pub fn recharge( &mut self, key: &ApkKey, entry_name: &str, size_of: impl FnOnce(&T) -> usize, ) -> bool
Re-charge the entry cached under entry_name at size_of its current
value, e.g. once per-entry state built after insertion has grown it,
and evict least recently used entries if that exceeds the capacity.
The value is measured under the cache’s lock, whatever replaced the
one a caller last saw, so the charge always matches what the slot
holds. Returns whether the charge changed.
Sourcepub fn replace(
&mut self,
key: &ApkKey,
entry_name: &str,
current: &Arc<T>,
new: Arc<T>,
) -> bool
pub fn replace( &mut self, key: &ApkKey, entry_name: &str, current: &Arc<T>, new: Arc<T>, ) -> bool
Swap the entry cached under entry_name for new when it is still
current, keeping its size and recency. Returns whether it swapped.
Sourcepub fn charged_bytes(&self, key: &ApkKey, entry_name: &str) -> Option<usize>
pub fn charged_bytes(&self, key: &ApkKey, entry_name: &str) -> Option<usize>
Size the entry cached under entry_name is charged at.
Sourcepub fn used_bytes(&self) -> usize
pub fn used_bytes(&self) -> usize
Bytes charged for the cached entries (the reserve not included).
Sourcepub fn capacity_bytes(&self) -> usize
pub fn capacity_bytes(&self) -> usize
Bytes the entries and the reserve may take together.
Sourcepub fn reserved_bytes(&self) -> usize
pub fn reserved_bytes(&self) -> usize
Bytes counted against the capacity besides the entries
(MemoryCache::set_reserved).
Sourcepub fn set_reserved(&mut self, bytes: usize)
pub fn set_reserved(&mut self, bytes: usize)
Count bytes the host holds elsewhere against the capacity, evicting
least recently used entries to make room.
Sourcepub fn set_capacity(&mut self, cap_bytes: usize)
pub fn set_capacity(&mut self, cap_bytes: usize)
Change the capacity, evicting least recently used entries if the cache now holds more.
Sourcepub fn peek(&self, key: &ApkKey, entry_name: &str) -> Option<Arc<T>>
pub fn peek(&self, key: &ApkKey, entry_name: &str) -> Option<Arc<T>>
The entry cached under entry_name, without touching its recency.
Sourcepub fn values(&self) -> impl Iterator<Item = &Arc<T>>
pub fn values(&self) -> impl Iterator<Item = &Arc<T>>
Every cached value, in no particular order (recency is not touched).
Sourcepub fn remove_apk(&mut self, key: &ApkKey) -> usize
pub fn remove_apk(&mut self, key: &ApkKey) -> usize
Drop every entry cached for key and return the bytes released.