Skip to main content

SieveCache

Struct SieveCache 

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

A sharded, weighted, thread-safe SIEVE cache.

Implementations§

Source§

impl<K, V, S> SieveCache<K, V, S>

Source

pub const DEFAULT_SHARDS: usize = 256

Default power-of-two shard count used by SieveCache::new.

Source§

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

Source

pub fn new(total_capacity_bytes: usize) -> Self

Creates a cache with Self::DEFAULT_SHARDS shards.

total_capacity_bytes is split across shards, with any remainder assigned to the lowest-index shards.

Source

pub fn with_shards(total_capacity_bytes: usize, num_shards: usize) -> Self

Creates a cache with an explicit shard count.

§Panics

Panics if num_shards == 0 or if num_shards is not a power of two.

Source§

impl<K, V, S> SieveCache<K, V, S>
where K: Eq + Hash + Clone, V: Send + Sync + 'static, S: BuildHasher + Clone,

Source

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

Creates a cache with an explicit shard count and hash builder.

total_capacity_bytes is divided across shards as evenly as possible.

§Panics

Panics if num_shards == 0 or if num_shards is not a power of two.

Source

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

Inserts or replaces key with a caller-supplied weighted charge.

Returns the previous value if the key was already present.

If the insert pushes the shard over capacity, eviction runs before this call returns. An oversized entry may therefore be admitted and then immediately evicted.

Source

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

Looks up key, marks the entry visited, and returns a shared handle.

Source

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

Removes key if present and returns the removed value.

Source

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

Removes all entries matching predicate and returns the number removed.

Source

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

Returns true if key is currently present.

Source

pub fn len(&self) -> usize

Returns the current number of live entries across all shards.

Source

pub fn is_empty(&self) -> bool

Returns true when the cache has no live entries.

Source

pub fn total_charge(&self) -> usize

Returns the current total weighted charge across all shards.

Source

pub fn total_capacity(&self) -> usize

Returns the configured total weighted capacity.

Source

pub fn shard_count(&self) -> usize

Returns the number of shards in the cache.

Source

pub fn shard_usage(&self, idx: usize) -> (usize, usize)

Returns (used_charge, capacity) for shard idx.

§Panics

Panics if idx >= self.shard_count().

Auto Trait Implementations§

§

impl<K, V, S> Freeze for SieveCache<K, V, S>
where S: Freeze,

§

impl<K, V, S> RefUnwindSafe for SieveCache<K, V, S>
where S: RefUnwindSafe,

§

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

§

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

§

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

§

impl<K, V, S> UnsafeUnpin for SieveCache<K, V, S>
where S: UnsafeUnpin,

§

impl<K, V, S> UnwindSafe for SieveCache<K, V, S>
where S: UnwindSafe,

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.