fibre_cache 0.4.7

Best in-class comprehensive, most flexible, high-performance, concurrent multi-mode sync/async caching library for Rust. It provides a rich, ergonomic API including a runtime-agnostic CacheLoader, an atomic `entry` API, and a wide choice of modern cache policies like W-TinyLFU, SIEVE, ARC, LRU, Clock, SLRU, Random.
Documentation
use fibre_cache::CacheBuilder;
use tokio::time::{Duration, sleep};

const TINY_TTI: Duration = Duration::from_millis(200);
const JANITOR_TICK: Duration = Duration::from_millis(10);
// A sleep duration safely longer than the TTI
const SLEEP_DURATION: Duration = Duration::from_millis(400);

#[tokio::test]
async fn test_async_item_expires_after_tti() {
  let cache = CacheBuilder::<&str, &str>::new()
    .shards(1)
    .time_to_idle(TINY_TTI)
    .janitor_tick_interval(JANITOR_TICK)
    .maintenance_chance(1)
    .build_async()
    .unwrap();

  cache.insert("key", "value", 1).await;

  // Wait for TTI to expire
  sleep(SLEEP_DURATION).await;

  // The get call will see the item is expired and return None.
  assert!(
    cache.fetch(&"key").await.is_none(),
    "Item should expire after being idle"
  );

  assert_eq!(
    cache.metrics().evicted_by_tti,
    1,
    "TTI eviction metric should be updated by janitor"
  );
}

#[tokio::test]
async fn test_async_tti_is_reset_on_access() {
  let cache = CacheBuilder::<&str, &str>::new()
    .time_to_idle(TINY_TTI)
    .janitor_tick_interval(JANITOR_TICK)
    .maintenance_chance(1)
    .build_async()
    .unwrap();

  cache.insert("key", "value", 1).await;

  // Loop several times, each time sleeping for less than the TTI
  // and then accessing the key to reset the idle timer.
  for _ in 0..3 {
    sleep(TINY_TTI / 2).await;
    assert!(
      cache.fetch(&"key").await.is_some(),
      "Item should be present before TTI expires"
    );
  }

  // Now, wait for the full TTI duration without access
  sleep(SLEEP_DURATION).await;

  // The item should finally be expired.
  assert!(
    cache.fetch(&"key").await.is_none(),
    "Item should have expired after final idle period"
  );
}