LruCache

Struct LruCache 

Source
pub struct LruCache<K, V, T = AtomicInstant, S = DefaultHashBuilder> { /* private fields */ }

Implementations§

Source§

impl<K, V, T> LruCache<K, V, T, DefaultHashBuilder>

Source

pub fn new(num_shards: usize) -> Self

Source§

impl<K, V, T, S> LruCache<K, V, T, S>

Source

pub fn with_hasher(num_shards: usize, hash_builder: S) -> Self

Source§

impl<K, V, T, S> LruCache<K, V, T, S>
where S: Clone, K: Clone, V: Clone, T: AtomicTimestamp,

Source

pub async fn duplicate(&self) -> Self

Attempts to duplicate/clone the LruCache. An LruCache cannot be cloned regularly due to internal asynchronous locking.

Source§

impl<K, V, T, S> LruCache<K, V, T, S>
where K: Hash + Eq, S: BuildHasher, T: AtomicTimestamp,

Source

pub fn size(&self) -> usize

Source

pub fn hash_builder(&self) -> &S

Source

pub fn num_shards(&self) -> usize

Source

pub async fn retain<F>(&self, f: F)
where F: Fn(&K, &mut V) -> bool,

Source

pub async fn clear(&self)

Source

pub async fn peek<Q>(&self, key: &Q) -> Option<ReadHandle<impl Erased, V>>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source

pub async fn peek_mut<Q>(&self, key: &Q) -> Option<WriteHandle<impl Erased, V>>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source

pub async fn get<Q>(&self, key: &Q) -> Option<ReadHandle<impl Erased, V>>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source

pub async fn get_mut<Q>(&self, key: &Q) -> Option<WriteHandle<impl Erased, V>>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source

pub async fn insert(&self, key: K, value: V) -> Option<V>

Source

pub async fn remove<Q>(&self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Source

pub async fn evict<F>(&self, rng: impl Rng, predicate: F) -> Vec<(K, V)>
where F: FnMut(&K, &mut V) -> Evict,

Fair element eviction based on 2-random sampling of two shards at once, and performs a random walk through all shards as necessary to remain unbiased.

NOTE: This method acquires one write lock per element, and can be inefficient for many evictions.

If you want fair eviction of a handful of items, this is the method to use. For less-predictable bulk-eviction look at evict_many_fast

Source

pub async fn evict_many(&self, count: usize, rng: impl Rng) -> Vec<(K, V)>

Fairly evict many elements, based on 2-random sampling of two shards at once, and performs a random walk through all shards as necessary to remain unbiased.

NOTE: This method acquires one write lock per element, and can be inefficient for many evictions.

If you want fair eviction of a handful of items, this is the method to use. For less-predictable bulk-eviction look at evict_many_fast

Source

pub async fn evict_one(&self, rng: impl Rng) -> Option<(K, V)>

Source

pub async fn evict_many_fast(&self, count: usize, rng: impl Rng) -> Vec<(K, V)>

Less-fair and less-predictable algorithm that only acquires shard locks once at most, but may not evict the exact number of requested elements (a couple more or less)

Compare to evict or evict_many that acquires a shard lock per-item evicted, but is more fair and unbiased in doing so.

Trait Implementations§

Source§

impl<K: Debug, V: Debug, T: Debug, S: Debug> Debug for LruCache<K, V, T, S>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<K, V> Default for LruCache<K, V, AtomicInstant, DefaultHashBuilder>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<K, V, T = AtomicInstant, S = BuildHasherDefault<AHasher>> !Freeze for LruCache<K, V, T, S>

§

impl<K, V, T = AtomicInstant, S = BuildHasherDefault<AHasher>> !RefUnwindSafe for LruCache<K, V, T, S>

§

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

§

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

§

impl<K, V, T, S> Unpin for LruCache<K, V, T, S>
where S: Unpin,

§

impl<K, V, T = AtomicInstant, S = BuildHasherDefault<AHasher>> !UnwindSafe for LruCache<K, V, T, 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V