#cache #ttl #redis-cache #swr

cachified

A Rust port of the cachified library

2 releases

Uses new Rust 2024

0.0.1-alpha.2 May 28, 2025

#122 in Caching

Download history 267/week @ 2025-05-28

267 downloads per month

MIT license

44KB
767 lines

Cachified-rs

A work-in-progress port of the cachified library from TypeScript to Rust.

Features

  • moka (default): Enable Moka in-memory cache backend
  • redis: Enable Redis distributed cache backend
  • serde (default): Enable serialization support (required for Redis)
  • tracing: Enable tracing support

Quick Start

With Moka (in-memory cache)

use cachified::{cachified, CachifiedOptionsBuilder, MokaCache};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let cache = MokaCache::new(1000);
    
    let value: String = cachified(
        CachifiedOptionsBuilder::new(cache, "user-1")
            .ttl(Duration::from_secs(300)) // 5 minutes
            .get_fresh_value(|| async { 
                // This would typically be a database call, API request, etc.
                Ok("fresh-value".to_string())
            })
    ).await?;
    
    println!("Cached value: {}", value);
    Ok(())
}

With Redis (distributed cache)

use cachified::{cachified, CachifiedOptionsBuilder, RedisCache};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let cache = RedisCache::new("redis://localhost:6379").await?;
    
    let value: String = cachified(
        CachifiedOptionsBuilder::new(cache, "user-1")
            .ttl(Duration::from_secs(300)) // 5 minutes
            .get_fresh_value(|| async { 
                // This would typically be a database call, API request, etc.
                Ok("fresh-value".to_string())
            })
    ).await?;
    
    println!("Cached value: {}", value);
    Ok(())
}

cachified-rs

A work-in-progress port of the cachified library by Kent C. Dodds from TypeScript to Rust.

Note: This library is still under development and production use is therefore not recommended (though I won't stop you if you want to try it out).

Motivation

I loved the simplicity and functionality of the original cachified library, but I wanted to use it in a Rust project. Since there was no direct Rust equivalent, I decided to port it myself.

My goal was to create a similar API and functionality, though I took some creative liberties where I saw fit.

Also, I'm by no means a Rust expert, so contributions and feedback are welcome!

License

This project is licensed under the MIT License.

Credits

Dependencies

~6–34MB
~499K SLoC