Skip to main content

LruCache

Struct LruCache 

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

A weighted, thread-safe LRU cache.

Implementations§

Source§

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

Source

pub const DEFAULT_SHARDS: usize = 1

Default shard count reported by LruCache::shard_count.

Source§

impl<K, V> LruCache<K, V, RandomState>
where K: Ord + Clone + Debug, V: CacheEntry + Clone,

Source

pub fn new(max_cost: usize) -> Self

Creates a cache with the default hash builder.

Source§

impl<K, V, S> LruCache<K, V, S>
where K: Ord + Clone + Debug, V: CacheEntry + Clone,

Source

pub fn with_hasher(max_cost: usize, _hash_builder: S) -> Self

Creates a cache with an explicit hash builder.

Source

pub fn insert(&self, key: K, value: V)

Inserts or replaces key with value.

Source

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

Looks up key and returns a clone of the stored value.

Source

pub fn remove(&self, key: &K) -> Option<V>

Removes key if present and returns the removed value.

Source

pub fn remove_if<F>(&self, predicate: F)
where F: Fn(&K) -> bool,

Removes every entry whose key matches predicate.

Source

pub fn remove_range<R>(&self, range: R) -> usize
where R: RangeBounds<K>,

Removes every entry whose key falls within range.

Source

pub fn contains_key(&self, key: &K) -> bool

Returns true if key is currently resident.

Source

pub fn len(&self) -> usize

Returns the number of resident entries.

Source

pub fn total_charge(&self) -> usize

Returns the total resident cost.

Source

pub fn total_capacity(&self) -> usize

Returns the configured total cost capacity.

Source

pub fn shard_count(&self) -> usize

Returns the number of shards reported by this backend.

Trait Implementations§

Source§

impl<K, V, S> BufferCache<K, V> for LruCache<K, V, S>
where K: Ord + Clone + Debug + Send + Sync + 'static, V: CacheEntry + Clone + Send + Sync + 'static, S: Send + Sync + 'static,

Source§

fn as_any(&self) -> &dyn Any

Returns this backend as Any for backend-specific downcasts.
Source§

fn strategy(&self) -> BufferCacheStrategy

Returns the eviction strategy used by this cache.
Source§

fn insert(&self, key: K, value: V)

Inserts or replaces key with value. Read more
Source§

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

Looks up key and returns a clone of the stored value.
Source§

fn remove(&self, key: &K) -> Option<V>

Removes key if it is present and returns the removed value.
Source§

fn remove_if(&self, predicate: &dyn Fn(&K) -> bool)

Removes every entry whose key matches predicate.
Source§

fn contains_key(&self, key: &K) -> bool

Returns true if key is currently resident.
Source§

fn len(&self) -> usize

Returns the number of resident entries.
Source§

fn total_charge(&self) -> usize

Returns the total resident weight.
Source§

fn total_capacity(&self) -> usize

Returns the configured total weight capacity.
Source§

fn shard_count(&self) -> usize

Returns the number of shards used by this backend.
Source§

fn is_empty(&self) -> bool

Returns true if the cache has no resident entries.

Auto Trait Implementations§

§

impl<K, V, S = RandomState> !Freeze for LruCache<K, V, S>

§

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

§

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

§

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

§

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

§

impl<K, V, S> UnsafeUnpin for LruCache<K, V, S>

§

impl<K, V, S> UnwindSafe for LruCache<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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more