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>
impl<K, V, S> SieveCache<K, V, S>
Sourcepub const DEFAULT_SHARDS: usize = 256
pub const DEFAULT_SHARDS: usize = 256
Default power-of-two shard count used by SieveCache::new.
Source§impl<K, V> SieveCache<K, V, RandomState>
impl<K, V> SieveCache<K, V, RandomState>
Sourcepub fn new(total_capacity_bytes: usize) -> Self
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.
Sourcepub fn with_shards(total_capacity_bytes: usize, num_shards: usize) -> Self
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>
impl<K, V, S> SieveCache<K, V, S>
Sourcepub fn with_hasher(
total_capacity_bytes: usize,
num_shards: usize,
hash_builder: S,
) -> Self
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.
Sourcepub fn insert(&self, key: K, value: V, charge: usize) -> Option<Arc<V>>
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.
Sourcepub fn get(&self, key: &K) -> Option<Arc<V>>
pub fn get(&self, key: &K) -> Option<Arc<V>>
Looks up key, marks the entry visited, and returns a shared handle.
Sourcepub fn remove(&self, key: &K) -> Option<Arc<V>>
pub fn remove(&self, key: &K) -> Option<Arc<V>>
Removes key if present and returns the removed value.
Sourcepub fn remove_if<F>(&self, predicate: F) -> usize
pub fn remove_if<F>(&self, predicate: F) -> usize
Removes all entries matching predicate and returns the number removed.
Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Returns true if key is currently present.
Sourcepub fn total_charge(&self) -> usize
pub fn total_charge(&self) -> usize
Returns the current total weighted charge across all shards.
Sourcepub fn total_capacity(&self) -> usize
pub fn total_capacity(&self) -> usize
Returns the configured total weighted capacity.
Sourcepub fn shard_count(&self) -> usize
pub fn shard_count(&self) -> usize
Returns the number of shards in the cache.