ShardedLruCache

Struct ShardedLruCache 

Source
pub struct ShardedLruCache<K, V>
where K: Clone + Debug + Hash + Eq + Send + Sync + 'static, V: Clone + Debug + Send + Sync + 'static,
{ /* private fields */ }
Expand description

A sharded LRU cache implementation for high-concurrency scenarios.

This implementation divides the cache into multiple shards, each protected by its own mutex, to reduce contention in concurrent access scenarios. The number of shards is automatically determined based on the total capacity, but will not exceed MAX_SHARDS.

§Type Parameters

  • K - The type of keys used in the cache. Must implement Clone + Debug + Hash + Eq + Send + Sync + 'static
  • V - The type of values stored in the cache. Must implement Clone + Debug + Send + Sync + 'static

§Examples

use lrust_cache::ShardedLruCache;

let cache = ShardedLruCache::new(1000);
cache.put("key1".to_string(), "value1".to_string());
assert_eq!(cache.get(&"key1".to_string()), Some("value1".to_string()));

Implementations§

Source§

impl<K, V> ShardedLruCache<K, V>
where K: Clone + Debug + Hash + Eq + Send + Sync + 'static, V: Clone + Debug + Send + Sync + 'static,

Source

pub fn new(capacity: usize) -> Self

Creates a new sharded LRU cache with the specified total capacity.

The number of shards is automatically determined based on the capacity, ensuring that each shard has at least MIN_SHARD_CAPACITY entries and the total number of shards is a power of 2 not exceeding MAX_SHARDS.

§Arguments
  • capacity - The total capacity of the cache
§Panics

Panics if capacity is 0

Source

pub fn capacity(&self) -> usize

Returns the total capacity of the cache.

Source

pub fn num_shards(&self) -> usize

Returns the number of shards in the cache.

Source

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

Retrieves a value from the cache by its key.

If the key exists, the value is cloned and returned, and the entry is marked as most recently used.

§Arguments
  • key - The key to look up
§Returns
  • Some(V) if the key exists
  • None if the key doesn’t exist
Source

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

Inserts a key-value pair into the cache.

If the key already exists, the value is updated and the old value is returned. If the cache is at capacity, the least recently used entry is removed to make space.

§Arguments
  • key - The key to insert
  • value - The value to insert
§Returns
  • Some(V) if the key already existed (returns the old value)
  • None if the key didn’t exist
Source

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

Removes an entry from the cache by its key.

§Arguments
  • key - The key to remove
§Returns
  • Some(V) if the key existed (returns the removed value)
  • None if the key didn’t exist
Source

pub fn len(&self) -> usize

Returns the number of entries in the cache.

Source

pub fn is_empty(&self) -> bool

Returns true if the cache is empty.

Source

pub fn clear(&self)

Removes all entries from the cache.

Auto Trait Implementations§

§

impl<K, V> Freeze for ShardedLruCache<K, V>

§

impl<K, V> !RefUnwindSafe for ShardedLruCache<K, V>

§

impl<K, V> Send for ShardedLruCache<K, V>

§

impl<K, V> Sync for ShardedLruCache<K, V>

§

impl<K, V> Unpin for ShardedLruCache<K, V>
where K: Unpin,

§

impl<K, V> UnwindSafe for ShardedLruCache<K, V>

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.