#![allow(clippy::unnecessary_wraps, clippy::unused_async, clippy::unwrap_used)]
use kash::{RedisCacheError, kash};
use std::io;
use std::io::Write;
use std::time::Duration;
use thiserror::Error;
#[derive(Error, Debug, PartialEq, Clone)]
enum ExampleError {
#[error("error with redis cache `{0}`")]
RedisError(String),
}
impl From<RedisCacheError> for ExampleError {
fn from(e: RedisCacheError) -> Self {
ExampleError::RedisError(format!("{e:?}"))
}
}
#[kash(redis(prefix_block = r#"{ "cache-redis-example-1:" }"#), ttl = "30")]
async fn kash_sleep_secs(secs: u64) -> Result<(), ExampleError> {
std::thread::sleep(Duration::from_secs(secs));
Ok(())
}
#[kash(redis)]
async fn async_kash_sleep_secs(secs: u64) -> Result<String, ExampleError> {
std::thread::sleep(Duration::from_secs(secs));
Ok(secs.to_string())
}
#[tokio::main]
async fn main() {
print!("1. first sync call with a 2-second sleep...");
io::stdout().flush().unwrap();
kash_sleep_secs(2).await.unwrap();
println!("done");
print!("second sync call with a 2-second sleep (it should be fast)...");
io::stdout().flush().unwrap();
kash_sleep_secs(2).await.unwrap();
println!("done");
print!("2. first async call with a 2-second sleep...");
io::stdout().flush().unwrap();
async_kash_sleep_secs(2).await.unwrap();
println!("done");
print!("second async call with a 2-second sleep (it should be fast)...");
io::stdout().flush().unwrap();
async_kash_sleep_secs(2).await.unwrap();
println!("done");
}