2 releases
Uses new Rust 2024
0.0.1-alpha.2 | May 28, 2025 |
---|
#122 in Caching
267 downloads per month
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 backendredis
: Enable Redis distributed cache backendserde
(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
- Original cachified library by Kent C. Dodds and contributors
Dependencies
~6–34MB
~499K SLoC