#cache #hybrid #foyer #disk #in-memory #concurrency #hybrid-cache

app foyer-bench

bench tool for foyer - the hybrid cache for Rust

7 releases

new 0.2.1 Jul 8, 2024
0.2.0 Jul 2, 2024
0.1.4 Jun 14, 2024
0.1.1 May 31, 2024

#71 in Filesystem

Download history 139/week @ 2024-05-24 411/week @ 2024-05-31 53/week @ 2024-06-07 189/week @ 2024-06-14 1/week @ 2024-06-21 87/week @ 2024-06-28

356 downloads per month




Crates.io Version Crates.io MSRV GitHub License

CI (main) License Checker codecov

foyer aims to be a user-friendly hybrid cache lib in Rust.

foyer is inspired by Facebook/CacheLib, ben-manes/caffeine, and other projects, which is an excellent hybrid cache lib in C++. foyer is not only a 'rewrite in Rust project', but provide some features that CacheLib doesn't have for now.


  • Hybrid Cache: Seamlessly integrates both in-memory and disk-based caching for optimal performance and flexibility.
  • Plug-and-Play Algorithms: Empowers users with easily replaceable caching algorithms, ensuring adaptability to diverse use cases.
  • Fearless Concurrency: Built to handle high concurrency with robust thread-safe mechanisms, guaranteeing reliable performance under heavy loads.
  • User-Friendly Interface: Offers a simple and intuitive API, making cache integration effortless and accessible for developers of all levels.
  • Out-of-the-Box Observability: Integrate popular observation systems such as Prometheus, Grafana, Opentelemetry, and Jaeger in just ONE line.

Projects Using foyer

Feel free to open a PR and add your projects here:

  • RisingWave: SQL stream processing, analytics, and management.


To use foyer in your project, add this line to the dependencies section of Cargo.toml.

foyer = "0.10"

If your project is using the nightly rust toolchain, the nightly feature needs to be enabled.

foyer = { version = "0.10", features = ["nightly"] }

Out-of-the-box In-memory Cache

use foyer::{Cache, CacheBuilder};

fn main() {
    let cache: Cache<String, String> = CacheBuilder::new(16).build();

    let entry = cache.insert("hello".to_string(), "world".to_string());
    let e = cache.get("hello").unwrap();

    assert_eq!(entry.value(), e.value());

Easy-to-use Hybrid Cache

use foyer::{DirectFsDeviceOptionsBuilder, HybridCache, HybridCacheBuilder};

async fn main() -> anyhow::Result<()> {
    let dir = tempfile::tempdir()?;

    let hybrid: HybridCache<u64, String> = HybridCacheBuilder::new()
        .memory(64 * 1024 * 1024)
                .with_capacity(256 * 1024 * 1024)

    hybrid.insert(42, "The answer to life, the universe, and everything.".to_string());
        "The answer to life, the universe, and everything."


Fully Configured Hybrid Cache

use std::sync::Arc;

use anyhow::Result;
use chrono::Datelike;
use foyer::{
    DirectFsDeviceOptionsBuilder, FifoPicker, HybridCache, HybridCacheBuilder, LruConfig, RateLimitPicker, RecoverMode,
    RuntimeConfigBuilder, TombstoneLogConfigBuilder,
use tempfile::tempdir;

async fn main() -> Result<()> {
    let dir = tempdir()?;

    let hybrid: HybridCache<u64, String> = HybridCacheBuilder::new()
        .with_eviction_config(LruConfig {
            high_priority_pool_ratio: 0.1,
        .with_weighter(|_key, value: &String| value.len())
                .with_capacity(64 * 1024 * 1024)
                .with_file_size(4 * 1024 * 1024)
        .with_buffer_threshold(256 * 1024 * 1024)
        .with_admission_picker(Arc::new(RateLimitPicker::new(100 * 1024 * 1024)))
        .with_reinsertion_picker(Arc::new(RateLimitPicker::new(10 * 1024 * 1024)))

    hybrid.insert(42, "The answer to life, the universe, and everything.".to_string());
        "The answer to life, the universe, and everything."

    let e = hybrid
        .fetch(20230512, || async {
            let value = mock().await?;
    assert_eq!(e.key(), &20230512);
    assert_eq!(e.value(), "Hello, foyer.");



async fn mock() -> Result<String> {
    let now = chrono::Utc::now();
    if format!("{}{}{}", now.year(), now.month(), now.day()) == "20230512" {
        return Err(anyhow::anyhow!("Hi, time traveler!"));
    Ok("Hello, foyer.".to_string())

Other Examples

More examples and details can be found here.

Supported Rust Versions

foyer is built against the recent stable release. The minimum supported version is 1.77. The current foyer version is not guaranteed to build on Rust versions earlier than the minimum supported version.

Development State & Roadmap

Currently, foyer is still under heavy development.

The development state and the roadmap can be found here.


Contributions for foyer are warmly welcomed! 🥰

Don't forget to pass make fast (which means fast check & test) locally before submitting a PR. 🚀

If you want to run a broader range of checks locally, run make full. 🙌


~561K SLoC