LruCache

Struct LruCache 

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

An implementation of a Least Recently Used (LRU) cache.

The cache has a fixed capacity and supports O(1) operations for inserting, retrieving, and updating entries. When the cache reaches capacity, the least recently used entry will be evicted to make room for new entries.

§Examples

use cache_rs::LruCache;
use core::num::NonZeroUsize;

let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());

// Add items to the cache
cache.put("apple", 1);
cache.put("banana", 2);

// Accessing items updates their recency
assert_eq!(cache.get(&"apple"), Some(&1));

// Adding beyond capacity evicts the least recently used item
cache.put("cherry", 3);
assert_eq!(cache.get(&"banana"), None);
assert_eq!(cache.get(&"apple"), Some(&1));
assert_eq!(cache.get(&"cherry"), Some(&3));

Implementations§

Source§

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

Source

pub fn with_hasher(cap: NonZeroUsize, hash_builder: S) -> Self

Creates a new LRU cache with the specified capacity and hash builder.

Source

pub fn with_hasher_and_size( cap: NonZeroUsize, hash_builder: S, max_size_bytes: u64, ) -> Self

Creates a new LRU cache with specified capacity, hash builder, and size limit.

Source

pub fn cap(&self) -> NonZeroUsize

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

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

Source

pub fn record_miss(&mut self, object_size: u64)

Source

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

Source§

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

Source

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

Source

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

Source

pub fn clear(&mut self)

Source

pub fn iter(&self) -> Iter<'_, K, V>

Source

pub fn iter_mut(&mut self) -> IterMut<'_, K, V>

Source§

impl<K: Hash + Eq, V> LruCache<K, V>
where V: Clone,

Trait Implementations§

Source§

impl<K: Hash + Eq, V: Clone, S: BuildHasher> CacheMetrics for LruCache<K, V, S>

Source§

fn metrics(&self) -> BTreeMap<String, f64>

Returns all metrics as key-value pairs in deterministic order Read more
Source§

fn algorithm_name(&self) -> &'static str

Algorithm name for identification Read more
Source§

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

§

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, S: Send,

§

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

§

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

§

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, 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.