TtlObjectCache

Struct TtlObjectCache 

Source
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); // expired

Implementations§

Source§

impl<V> TtlObjectCache<V>

Source

pub fn new(policy: RenewalPolicy) -> Self

Create a new empty cache with the given renewal policy.

Source

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.

Source

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.

Source

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.

Source

pub fn remove(&mut self, key: &str) -> Option<V>

Remove an entry by key, returning the value if it existed.

Source

pub fn prune_expired(&mut self, now: u64) -> usize

Remove all expired entries, returning the number pruned.

Source

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).

Source

pub fn len(&self) -> usize

Number of entries currently in the cache (including expired but not yet pruned).

Source

pub fn is_empty(&self) -> bool

Returns true if the cache contains no entries.

Source

pub fn renewal_policy(&self) -> &RenewalPolicy

Access the renewal policy associated with this cache.

Auto Trait Implementations§

§

impl<V> Freeze for TtlObjectCache<V>

§

impl<V> RefUnwindSafe for TtlObjectCache<V>
where V: RefUnwindSafe,

§

impl<V> Send for TtlObjectCache<V>
where V: Send,

§

impl<V> Sync for TtlObjectCache<V>
where V: Sync,

§

impl<V> Unpin for TtlObjectCache<V>

§

impl<V> UnwindSafe for TtlObjectCache<V>
where V: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.