DynamicCache

Struct DynamicCache 

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

A cache that will only hold onto items that have been reqeuested 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 it requested has expired. The length of the memory is adjustable, and must be set at initialization.

This version of the cache can be shared across threads without issue, as it is an instance of DynamicCacheLocal held by an Arc<Mutex<T>>.

Implementations§

Source§

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

Source

pub fn with_hasher(mem_len: usize, hash_builder: S) -> DynamicCache<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> DynamicCache<K, V>

Source

pub fn new(mem_len: usize) -> Self

Create an initialize a new cache.

Source

pub fn get(&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(&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(&self, key: &K, value: 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>(&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. The cache is unlocked while calling the function, so f may be called more than once with the same parameters if there are several threads using the cache.

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(&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(&self)

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

Source

pub fn hits_misses(&self) -> (u64, u64)

Get the cache metrics as a pair (hits, misses).

Source

pub fn reset_metrics(&self)

Reset the cache hit/miss metrics.

Trait Implementations§

Source§

impl<K: Clone, V: Clone, S: Clone> Clone for DynamicCache<K, V, S>

Source§

fn clone(&self) -> DynamicCache<K, V, S>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug, V: Debug, S: Debug> Debug for DynamicCache<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 DynamicCache<K, V, S>

§

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

§

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

§

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

§

impl<K, V, S> Unpin for DynamicCache<K, V, S>

§

impl<K, V, S> UnwindSafe for DynamicCache<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.