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.
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(())
}
Set a default time-to-live and override it for individual keys. Expired entries are never returned.
Hit, miss and eviction counters are available at any time through stats().
Expiry is lazy and amortized, so an idle store costs nothing.
Keys are spread over independent shards, each with its own lock and eviction list.
Concurrent misses for the same key share a single load instead of hammering your database.
Eviction work is bounded per operation, so there are no long pauses under load.