Skip to main content

MemoryCache

Struct MemoryCache 

Source
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>

Source

pub fn new(cap_bytes: usize) -> Self

An empty cache holding at most cap_bytes bytes of entries.

Source

pub fn get(&mut self, key: &ApkKey, entry_name: &str) -> Option<Arc<T>>

The entry cached under entry_name, marked most recently used.

Source

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.

Source

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.

Source

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.

Source

pub fn charged_bytes(&self, key: &ApkKey, entry_name: &str) -> Option<usize>

Size the entry cached under entry_name is charged at.

Source

pub fn used_bytes(&self) -> usize

Bytes charged for the cached entries (the reserve not included).

Source

pub fn capacity_bytes(&self) -> usize

Bytes the entries and the reserve may take together.

Source

pub fn reserved_bytes(&self) -> usize

Bytes counted against the capacity besides the entries (MemoryCache::set_reserved).

Source

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.

Source

pub fn set_capacity(&mut self, cap_bytes: usize)

Change the capacity, evicting least recently used entries if the cache now holds more.

Source

pub fn peek(&self, key: &ApkKey, entry_name: &str) -> Option<Arc<T>>

The entry cached under entry_name, without touching its recency.

Source

pub fn values(&self) -> impl Iterator<Item = &Arc<T>>

Every cached value, in no particular order (recency is not touched).

Source

pub fn len(&self) -> usize

Number of cached entries, over every APK.

Source

pub fn is_empty(&self) -> bool

Whether no entry is cached.

Source

pub fn remove_apk(&mut self, key: &ApkKey) -> usize

Drop every entry cached for key and return the bytes released.

Source

pub fn clear(&mut self)

Drop every entry (the reserve is kept).

Auto Trait Implementations§

§

impl<T> Forget for MemoryCache<T>

§

impl<T> Freeze for MemoryCache<T>
where HashMap<ApkKey, HashMap<String, Slot<T>>>: Freeze,

§

impl<T> Move for MemoryCache<T>

§

impl<T> RefUnwindSafe for MemoryCache<T>

§

impl<T> Send for MemoryCache<T>
where HashMap<ApkKey, HashMap<String, Slot<T>>>: Send,

§

impl<T> Sync for MemoryCache<T>
where HashMap<ApkKey, HashMap<String, Slot<T>>>: Sync,

§

impl<T> Unpin for MemoryCache<T>
where HashMap<ApkKey, HashMap<String, Slot<T>>>: Unpin,

§

impl<T> UnsafeUnpin for MemoryCache<T>

§

impl<T> UnwindSafe for MemoryCache<T>
where HashMap<ApkKey, HashMap<String, Slot<T>>>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.