Struct DynamicCacheLocal

Source
pub struct DynamicCacheLocal<K, V, S = RandomState> { /* private fields */ }
Expand description

A cache that will only hold onto items that have been requested more than once in recent memory. Single-use items are not held at all. Once an item is requested twice, it is cached until all memory of seeing a request has expired. The length of the memory is adjustable, and must be set at initialization.

Implementations§

Source§

impl<K: Clone + Eq + Hash, V, S> DynamicCacheLocal<K, V, S>

Source

pub fn with_hasher( mem_len: usize, hash_builder: S, ) -> DynamicCacheLocal<K, V, S>

Create and initialize a new cache, using the given hash builder to hash keys. The same warnings given for HashMap::with_hasher apply here.

Source§

impl<K: Clone + Eq + Hash, V> DynamicCacheLocal<K, V>

Source

pub fn new(mem_len: usize) -> Self

Create and initialize a new cache.

Source

pub fn get(&mut self, key: &K) -> Option<Arc<V>>

Attempt to retrieve a value from the cache. This updates the cache’s memory of what values have been requested.

Source

pub fn pop(&mut self, key: &K) -> Option<Arc<V>>

Attempt to remove a value from the cache.

This will remove the value itself from the cache, but doesn’t change the cache’s stored request history. This means that any new [get_or_insert] calls will re-load the value into the cache.

Source

pub fn insert(&mut self, key: &K, v: V) -> Arc<V>

Insert a value into the cache. If the value is already present, an Arc<V> of the stored value is returned. If the value is not stored but has been requested more than once, then it is stored and returned. If the value is not stored and hasn’t been requested more than once, it is not stored and is simply returned, wrapped as an Arc<V>.

Source

pub fn get_or_insert<F: FnOnce() -> V>(&mut self, key: &K, f: F) -> Arc<V>

Fetch an item via the cache, potentially filling in the cache on a miss via the function f.

Source

pub fn size(&self) -> usize

Get the number of items currently stored in the cache.

Source

pub fn mem_len(&self) -> usize

Get the length of the cache’s recent request memory.

Source

pub fn set_mem_len(&mut self, new_len: usize)

Change the length of the cache’s recent request memory. Some contents of the cache may be removed immediately if the new memory length is shorter than the old memory length.

Source

pub fn clear_cache(&mut self)

Clear out all stored values and all memory in the cache.

Source

pub fn hits(&self) -> u64

Get the number of hits this cache has seen.

Source

pub fn misses(&self) -> u64

Get the number of misses this cache has seen.

Source

pub fn reset_metrics(&mut self)

Reset the cache hit/miss metrics.

Trait Implementations§

Source§

impl<K: Debug, V: Debug, S> Debug for DynamicCacheLocal<K, V, S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<K, V, S> Freeze for DynamicCacheLocal<K, V, S>
where S: Freeze,

§

impl<K, V, S> RefUnwindSafe for DynamicCacheLocal<K, V, S>

§

impl<K, V, S> Send for DynamicCacheLocal<K, V, S>
where S: Send, K: Send, V: Sync + Send,

§

impl<K, V, S> Sync for DynamicCacheLocal<K, V, S>
where S: Sync, K: Sync, V: Sync + Send,

§

impl<K, V, S> Unpin for DynamicCacheLocal<K, V, S>
where S: Unpin, K: Unpin,

§

impl<K, V, S> UnwindSafe for DynamicCacheLocal<K, V, S>

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.