Fibre Cache

A flexible, concurrent sync and async cache with modern eviction policies.

fibre_cache is a concurrent in-memory cache for Rust with the same API shape for sync and async code. You build it with CacheBuilder and get back either a Cache for blocking code or an AsyncCache for async code. to_sync() and to_async() switch between the two handles at no cost.

It covers the jobs a cache usually ends up doing in a real service: a capacity measured in cost units, time-to-live and time-to-idle expiry plus a loader that fills misses for you and runs only once when many callers miss the same key at the same time. Stale-while-revalidate, an eviction listener, an atomic entry API, bulk operations and serde snapshots are there when you need them.

Values are stored behind an Arc, so V does not have to implement Clone. Reads hand back an Arc<V>. If you only need to look at the value, get runs a closure against it in place. The cache is split into shards with their own locks and eviction state. A background janitor does the cleanup work so reads and writes stay short. The default policy for a bounded cache is TinyLFU. SIEVE, SLRU, ARC, LRU, Clock, FIFO and random are available as alternatives.

use std::time::Duration;
use fibre_cache::CacheBuilder;

let cache = CacheBuilder::default()
    .capacity(10_000)
    .time_to_live(Duration::from_secs(600))
    .async_loader(|id: u32| async move { (format!("user-{id}"), 1) })
    .build_async()?;

let name = cache.fetch_with(&7).await;

Benchmarks

Tutorials