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<V>where
K: Clone,
pub fn put(&self, key: K, value: V, size: u64) -> Option<V>where
K: Clone,
Inserts a key-value pair with its size into the cache.
Unlike other caches, GDSF requires the size of the object for priority calculation. Returns the old value if the key was already present.
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.