pub struct ConcurrentSlruCache<K, V, S = DefaultHashBuilder> { /* private fields */ }Expand description
A thread-safe SLRU cache with segmented storage for high concurrency.
Implementations§
Source§impl<K, V> ConcurrentSlruCache<K, V, DefaultHashBuilder>
impl<K, V> ConcurrentSlruCache<K, V, DefaultHashBuilder>
Sourcepub fn init(
config: ConcurrentSlruCacheConfig,
hasher: Option<DefaultHashBuilder>,
) -> Self
pub fn init( config: ConcurrentSlruCacheConfig, hasher: Option<DefaultHashBuilder>, ) -> Self
Creates a new concurrent SLRU cache from a configuration.
This is the recommended way to create a concurrent SLRU cache.
§Arguments
config- The cache configurationhasher- Optional custom hasher. IfNone, uses the default hasher.
Source§impl<K, V, S> ConcurrentSlruCache<K, V, S>
impl<K, V, S> ConcurrentSlruCache<K, V, S>
Sourcepub fn segment_count(&self) -> usize
pub fn segment_count(&self) -> usize
Returns the number of segments in the cache.
Sourcepub fn get<Q>(&self, key: &Q) -> Option<V>
pub fn get<Q>(&self, key: &Q) -> Option<V>
Gets a value from the cache.
This clones the value to avoid holding the lock. For zero-copy access,
use get_with() instead.
Sourcepub fn get_with<Q, F, R>(&self, key: &Q, f: F) -> Option<R>
pub fn get_with<Q, F, R>(&self, key: &Q, f: F) -> Option<R>
Gets a value and applies a function to it while holding the lock.
This is more efficient than get() when you only need to read from the value,
as it avoids cloning.
Sourcepub fn put(&self, key: K, value: V) -> Option<(K, V)>
pub fn put(&self, key: K, value: V) -> Option<(K, V)>
Inserts a key-value pair into the cache.
New items enter the probationary segment and are promoted to the protected segment on subsequent access.
Sourcepub fn put_with_size(&self, key: K, value: V, size: u64) -> Option<(K, V)>
pub fn put_with_size(&self, key: K, value: V, size: u64) -> Option<(K, V)>
Inserts a key-value pair with explicit size tracking.
Sourcepub fn remove<Q>(&self, key: &Q) -> Option<V>
pub fn remove<Q>(&self, key: &Q) -> Option<V>
Removes a key from the cache, returning the value if it existed.
Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
Returns true if the cache contains the specified key.
Sourcepub fn current_size(&self) -> u64
pub fn current_size(&self) -> u64
Returns the current total size of cached content across all segments.