pub struct ConcurrentGdsfCache<K, V, S = DefaultHashBuilder> { /* private fields */ }Expand description
A thread-safe GDSF cache with segmented storage for high concurrency.
GDSF (Greedy Dual-Size Frequency) is designed for caching variable-size objects.
The put method requires specifying the object size in addition to key and value.
Implementations§
Source§impl<K, V> ConcurrentGdsfCache<K, V, DefaultHashBuilder>
impl<K, V> ConcurrentGdsfCache<K, V, DefaultHashBuilder>
Sourcepub fn init(
config: ConcurrentGdsfCacheConfig,
hasher: Option<DefaultHashBuilder>,
) -> Self
pub fn init( config: ConcurrentGdsfCacheConfig, hasher: Option<DefaultHashBuilder>, ) -> Self
Creates a new concurrent GDSF cache from a configuration.
This is the recommended way to create a concurrent GDSF cache.
§Arguments
config- The cache configuration specifying capacity, max size, and segmentshasher- Optional custom hash builder. IfNone, uses the default hash builder.
Source§impl<K, V, S> ConcurrentGdsfCache<K, V, S>
impl<K, V, S> ConcurrentGdsfCache<K, V, S>
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the total capacity across all segments (in size units).
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, size: u64) -> Option<Vec<(K, V)>>where
K: Clone,
pub fn put(&self, key: K, value: V, size: u64) -> Option<Vec<(K, V)>>where
K: Clone,
Inserts a key-value pair with its size into the cache.
GDSF uses size for priority calculation. Use SIZE_UNIT (1) for count-based caching.
Returns None if no entries were evicted. Returns Some(vec) containing all
(key, value) pairs evicted from the cache as part of this operation to make
room for the new entry.
Overwriting an existing key without triggering eviction returns None.
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 peek<Q>(&self, key: &Q) -> Option<V>
pub fn peek<Q>(&self, key: &Q) -> Option<V>
Returns a clone of the value without updating priority or access metadata.
Unlike get(), this does NOT recalculate the entry’s GDSF
priority or change its position. Returns a cloned value because the
internal lock cannot be held across the return boundary.
§Example
let value = cache.peek(&"key".to_string());Sourcepub fn current_size(&self) -> u64
pub fn current_size(&self) -> u64
Returns the current total size of cached content across all segments.