v3.9.0 Codeberg

A sharded in-memory cache for Rust

ferncache is a concurrency-safe in-memory cache for Rust. It spreads keys across shards to keep lock contention low, evicts entries by TTL and by total cost, and deduplicates concurrent loads of the same key.

Get started View on Codeberg
$ cargo add ferncache
Rust · Rust 1.75+
use std::time::Duration;
use ferncache::{Store, Error};

#[tokio::main]
async fn main() -> Result<(), Error> {
    let store = Store::builder()
        .max_entries(10_000)
        .default_ttl(Duration::from_secs(300))
        .build()?;

    store.set("user:42", b"Ada").await?;

    let v = store.get("user:42").await?;
    println!("{v:?}");
    Ok(())
}

Per-entry TTL

Set a default time-to-live and override it for individual keys. Expired entries are never returned.

Observable

Hit, miss and eviction counters are available at any time through stats().

No background threads

Expiry is lazy and amortized, so an idle store costs nothing.

Low contention

Keys are spread over independent shards, each with its own lock and eviction list.

Stampede protection

Concurrent misses for the same key share a single load instead of hammering your database.

Predictable latency

Eviction work is bounded per operation, so there are no long pauses under load.