$ cached
v0.58.0MAJOR UPDATEGeneric cache implementations and simplified function memoization
Latest Update Summary
Crate
Name: cached New version: 0.58.0 Release date: 2026-03-09T05:42:27.031439Z
Crate readme
Short description Caching structures and simplified function memoization
Long description
cached provides implementations of several caching structures and macros for defining memoized functions with thread-safe caching, async support, and Redis integration.
Features • default • proc_macro • ahash • async • async_tokio_rt_multi_thread • redis_store • redis_smol • redis_tokio • redis_connection_manager • disk_store
Code Examples Basic usage
use cached::proc_macro::cached;
#[cached]
fn fib(n: u64) -> u64 {
if n == 0 || n == 1 { return n }
fib(n-1) + fib(n-2)
}
Use an explicit cache-type
use cached::proc_macro::cached;
use cached::SizedCache;
#[cached(
ty = "SizedCache<String, usize>",
create = "{ SizedCache::with_size(100) }",
convert = r#"{ format!("{}{}", a, b) }"#
)]
fn keyed(a: &str, b: &str) -> usize {
let size = a.len() + b.len();
sleep(Duration::new(size as u64, 0));
size
}
Only cache the initial function call
#[once(time=10, option = true, sync_writes = true)]
fn keyed(a: String) -> Option<usize> {
if a == "a" {
Some(a.len())
} else {
None
}
}
Cache results of an async function in redis
#[io_cached(
map_error = r##"|e| ExampleError::RedisError(format!("{:?}", e))"##,
ty = "AsyncRedisCache<u64, String>",
create = r##" {
AsyncRedisCache::new("cached_redis_prefix", Duration::from_secs(1))
.set_refresh(true)
.build()
.await
.expect("error building example redis cache")
} "##
)]
async fn async_cached_sleep_secs(secs: u64) -> Result<String, ExampleError> {
std::thread::sleep(std::time::Duration::from_secs(secs));
Ok(secs.to_string())
}
Cache results of a function on disk
#[io_cached(
map_error = r##"|e| ExampleError::DiskError(format!("{:?}", e))"##,
disk = true
)]
fn cached_sleep_secs(secs: u64) -> Result<String, ExampleError> {
std::thread::sleep(std::time::Duration::from_secs(secs));
Ok(secs.to_string())
}
Links • https://crates.io/crates/cached • https://docs.rs/cached
Release info
Release version: N/A
Release description N/A
Code Examples N/A
Minor update: 0.56.0 → 0.58.0
$ DOWNLOADS TREND
$ VERSION HISTORY
$ LINKS
$ INSTALL
cargo add cachedOr add to Cargo.toml: cached = "0.58.0"