pub struct GdsfCache<K, V, S = DefaultHashBuilder> { /* private fields */ }Expand description
An implementation of a Greedy Dual-Size Frequency (GDSF) cache.
Implementations§
Source§impl<K: Hash + Eq, V: Clone, S: BuildHasher> GdsfCache<K, V, S>
impl<K: Hash + Eq, V: Clone, S: BuildHasher> GdsfCache<K, V, S>
pub fn cap(&self) -> NonZeroUsize
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
Sourcepub fn current_size(&self) -> u64
pub fn current_size(&self) -> u64
Returns the current total size of cached content.
pub fn global_age(&self) -> f64
pub fn record_miss(&mut self, object_size: u64)
pub fn get<Q>(&mut self, key: &Q) -> Option<V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
Sourcepub fn put(&mut self, key: K, val: V, size: u64) -> Option<Vec<(K, V)>>where
K: Clone,
pub fn put(&mut self, key: K, val: V, size: u64) -> Option<Vec<(K, V)>>where
K: Clone,
Inserts a key-value pair with explicit size into the cache.
GDSF is inherently size-aware: the size parameter affects priority
calculation (priority = global_age + frequency / size), favoring
small, frequently-accessed items.
§Arguments
key- The key to insertval- The value to insertsize- Optional size in bytes. UseSIZE_UNIT(1) for count-based caching.
§Returns
Some(vec)containing evicted entries (not replaced entries)Noneif no entries were evicted (zero allocation)
pub fn clear(&mut self)
Sourcepub fn contains<Q>(&self, key: &Q) -> bool
pub fn contains<Q>(&self, key: &Q) -> bool
Check if key exists without updating its priority or access metadata.
Unlike get(), this method does NOT update the entry’s frequency
or access metadata.
§Example
use cache_rs::GdsfCache;
use cache_rs::config::GdsfCacheConfig;
use core::num::NonZeroUsize;
let config = GdsfCacheConfig {
capacity: NonZeroUsize::new(2).unwrap(),
initial_age: 0.0,
max_size: u64::MAX,
};
let mut cache = GdsfCache::init(config, None);
cache.put("a", 1, 10);
cache.put("b", 2, 10);
// contains() does NOT update priority
assert!(cache.contains(&"a"));Sourcepub fn peek<Q>(&self, key: &Q) -> Option<&V>
pub fn peek<Q>(&self, key: &Q) -> Option<&V>
Returns a reference to the value without updating priority or access metadata.
Unlike get(), this does NOT recalculate the entry’s GDSF
priority or change its position in the priority lists.
§Example
use cache_rs::GdsfCache;
use cache_rs::config::GdsfCacheConfig;
use core::num::NonZeroUsize;
let config = GdsfCacheConfig {
capacity: NonZeroUsize::new(3).unwrap(),
initial_age: 0.0,
max_size: u64::MAX,
};
let mut cache = GdsfCache::init(config, None);
cache.put("a", 1, 1);
// peek does not change priority
assert_eq!(cache.peek(&"a"), Some(&1));
assert_eq!(cache.peek(&"missing"), None);Source§impl<K: Hash + Eq, V: Clone> GdsfCache<K, V, DefaultHashBuilder>
impl<K: Hash + Eq, V: Clone> GdsfCache<K, V, DefaultHashBuilder>
Sourcepub fn init(config: GdsfCacheConfig, hasher: Option<DefaultHashBuilder>) -> Self
pub fn init(config: GdsfCacheConfig, hasher: Option<DefaultHashBuilder>) -> Self
Creates a new GDSF cache from a configuration.
This is the recommended way to create a GDSF cache. All configuration
is specified through the GdsfCacheConfig struct.
§Arguments
config- Configuration specifying capacity and optional size limit/initial agehasher- Optional custom hash builder. PassNoneto use the default.
§Example
use cache_rs::GdsfCache;
use cache_rs::config::GdsfCacheConfig;
use core::num::NonZeroUsize;
// Simple capacity-only cache
let config = GdsfCacheConfig {
capacity: NonZeroUsize::new(100).unwrap(),
initial_age: 0.0,
max_size: u64::MAX,
};
let mut cache: GdsfCache<&str, i32> = GdsfCache::init(config, None);
cache.put("key", 42, 1);
// Cache with size limit (recommended for GDSF)
let config = GdsfCacheConfig {
capacity: NonZeroUsize::new(1000).unwrap(),
initial_age: 0.0,
max_size: 10 * 1024 * 1024, // 10MB
};
let cache: GdsfCache<String, Vec<u8>> = GdsfCache::init(config, None);