Skip to main content

ShardedCache

Struct ShardedCache 

Source
pub struct ShardedCache { /* private fields */ }
Expand description

A concurrent cache backed by N power-of-two LRU shards.

Keys are routed to shards via hash(key) & (n_shards - 1). Each shard is independently protected by a Mutex<LruCache<Vec<u8>, Vec<u8>>>.

§Panics

new panics if n_shards is not a power of two, or if n_shards is 0.

Implementations§

Source§

impl ShardedCache

Source

pub fn new(n_shards: usize, shard_cap: usize) -> Self

Create a new sharded cache.

  • n_shards: number of shards — must be a power of two.
  • shard_cap: capacity per shard (entry count).
§Panics

Panics if n_shards == 0 or !n_shards.is_power_of_two().

Source

pub fn n_shards(&self) -> usize

Return the number of shards.

Source

pub fn shard_cap(&self) -> usize

Capacity per shard.

Source

pub fn get(&self, key: &[u8]) -> Option<Vec<u8>>

Look up key, returning a cloned copy of the value if present.

Returns None if the key is absent or expired.

Source

pub fn put(&self, key: Vec<u8>, value: Vec<u8>)

Insert or update key -> value.

Source

pub fn remove(&self, key: &[u8]) -> Option<Vec<u8>>

Remove key, returning its value if present.

Source

pub fn contains(&self, key: &[u8]) -> bool

Return true if key is present and not expired.

Source

pub fn len(&self) -> usize

Return the total number of live entries across all shards.

Source

pub fn is_empty(&self) -> bool

Return true if no entries are in any shard.

Source

pub fn clear(&self)

Clear all shards.

Auto Trait Implementations§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.