pub struct TtlObjectCache<V> { /* private fields */ }Expand description
Object cache with per-entry TTL and poll-driven expiry.
Values are indexed by string keys and associated with a
MemRegionLocator describing their location in fabric memory.
Expired entries are not returned by get and are
removed by prune_expired or
tick.
§Example
use grafos_cache::ttl::{TtlObjectCache, CacheStats};
use grafos_leasekit::RenewalPolicy;
use grafos_locator::locator::MemRegionLocator;
let mut cache = TtlObjectCache::<u64>::new(RenewalPolicy::default());
let loc = MemRegionLocator::new(1, 0, 1024);
cache.put("sensor-a".into(), 42, loc, 60, 1000);
assert_eq!(cache.get("sensor-a", 1030), Some(&42));
assert_eq!(cache.get("sensor-a", 1061), None); // expiredImplementations§
Source§impl<V> TtlObjectCache<V>
impl<V> TtlObjectCache<V>
Sourcepub fn new(policy: RenewalPolicy) -> Self
pub fn new(policy: RenewalPolicy) -> Self
Create a new empty cache with the given renewal policy.
Sourcepub fn get(&mut self, key: &str, now: u64) -> Option<&V>
pub fn get(&mut self, key: &str, now: u64) -> Option<&V>
Look up a value by key.
Returns None if the key does not exist or has expired. On a
successful hit, last_accessed is updated to now.
Sourcepub fn locator(&mut self, key: &str, now: u64) -> Option<&MemRegionLocator>
pub fn locator(&mut self, key: &str, now: u64) -> Option<&MemRegionLocator>
Look up the locator for a cached entry.
Returns None if the key does not exist or has expired.
Sourcepub fn put(
&mut self,
key: String,
value: V,
locator: MemRegionLocator,
ttl_secs: u64,
now: u64,
)
pub fn put( &mut self, key: String, value: V, locator: MemRegionLocator, ttl_secs: u64, now: u64, )
Insert or replace a cached value with the given TTL.
locator tracks the value’s location in fabric memory.
ttl_secs is the time-to-live from now.
Sourcepub fn remove(&mut self, key: &str) -> Option<V>
pub fn remove(&mut self, key: &str) -> Option<V>
Remove an entry by key, returning the value if it existed.
Sourcepub fn prune_expired(&mut self, now: u64) -> usize
pub fn prune_expired(&mut self, now: u64) -> usize
Remove all expired entries, returning the number pruned.
Sourcepub fn tick(&mut self, now: u64) -> CacheStats
pub fn tick(&mut self, now: u64) -> CacheStats
Prune expired entries and return cache statistics.
An entry is “near expiry” if its remaining TTL is at most 25% of its original TTL (i.e. it has consumed 75% or more of its lifetime).
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Number of entries currently in the cache (including expired but not yet pruned).
Sourcepub fn renewal_policy(&self) -> &RenewalPolicy
pub fn renewal_policy(&self) -> &RenewalPolicy
Access the renewal policy associated with this cache.