Documentation
use anyhow::Result;
use redis::{aio::Connection, AsyncCommands, RedisResult, ToRedisArgs};
use tracing::error;
/*
use tokio::sync::OnceCell;
static CLI: OnceCell<Client> = OnceCell::const_new();

async fn init() -> Client {
    Client::open("redis://127.0.0.1/").unwrap()
}
 */
pub async fn ttl(con: &mut Connection, key: &str) -> i64 {
    let tt: RedisResult<i64> = con.ttl(key).await;
    match tt {
        Ok(t) => t,
        Err(err) => {
            error!("{err}");
            0
        }
    }
}

pub async fn expire(con: &mut Connection, key: &str, sec: usize) {
    let expire: RedisResult<bool> = con.expire(key, sec).await;
    println!("{expire:?}");
}

pub async fn set<T>(con: &mut Connection, key: &str, v: T)
where
    T: ToRedisArgs + Send + Sync,
{
    let expire: RedisResult<bool> = con.set(key, v).await;
    println!("{expire:?}")
}
pub async fn set_ex<T>(con: &mut Connection, key: &str, v: T, sec: usize)
where
    T: ToRedisArgs + Send + Sync,
{
    let expire: RedisResult<bool> = con.set_ex(key, v, sec).await;
    println!("{expire:?}")
}
pub async fn get_int(con: &mut Connection, key: &str) -> i64 {
    let get: Result<i64, redis::RedisError> = con.get(key).await;
    match get {
        Ok(ret) => ret,
        Err(_) => 0,
    }
}