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>
pub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn put(&mut self, key: K, val: V, size: u64) -> Option<V>where
K: Clone,
pub fn pop<Q>(&mut self, key: &Q) -> Option<V>
pub fn clear(&mut self)
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);Trait Implementations§
Source§impl<K: Hash + Eq, V: Clone, S: BuildHasher> CacheMetrics for GdsfCache<K, V, S>
impl<K: Hash + Eq, V: Clone, S: BuildHasher> CacheMetrics for GdsfCache<K, V, S>
Auto Trait Implementations§
impl<K, V, S> Freeze for GdsfCache<K, V, S>where
S: Freeze,
impl<K, V, S> RefUnwindSafe for GdsfCache<K, V, S>
impl<K, V, S> Send for GdsfCache<K, V, S>
impl<K, V, S> Sync for GdsfCache<K, V, S>
impl<K, V, S> Unpin for GdsfCache<K, V, S>
impl<K, V, S> UnwindSafe for GdsfCache<K, V, S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more