use cached::macros::concurrent_cached;
use cached::time::Duration;
use std::io;
use std::io::Write;
use thiserror::Error;
#[derive(Error, Debug, PartialEq, Clone)]
enum ExampleError {
#[error("error with disk cache `{0}`")]
DiskError(String),
}
#[concurrent_cached(
disk = true,
ttl = 30,
name = "ASYNC_DISK_SLEEP_SECS",
map_error = r##"|e| ExampleError::DiskError(format!("{:?}", e))"##
)]
async fn async_disk_sleep_secs(secs: u64) -> Result<(), ExampleError> {
std::thread::sleep(Duration::from_secs(secs));
Ok(())
}
#[tokio::main]
async fn main() {
print!("1. first async call with a 2 seconds sleep...");
io::stdout().flush().unwrap();
async_disk_sleep_secs(2).await.unwrap();
println!("done");
print!("second async call (served from disk cache, should be fast)...");
io::stdout().flush().unwrap();
async_disk_sleep_secs(2).await.unwrap();
println!("done");
print!("2. prime the cache for secs=5, then call (should be fast)...");
io::stdout().flush().unwrap();
async_disk_sleep_secs_prime_cache(5).await.unwrap();
async_disk_sleep_secs(5).await.unwrap();
println!("done");
}