use larust_core::AppError;
use redis::aio::ConnectionManager;
use tokio::sync::OnceCell;
static CONNECTION: OnceCell<ConnectionManager> = OnceCell::const_new();
pub(crate) async fn connection() -> Result<ConnectionManager, AppError> {
let manager = CONNECTION
.get_or_try_init(|| async {
let url =
std::env::var("REDIS_URL").unwrap_or_else(|_| "redis://127.0.0.1:6379".to_string());
let client = redis::Client::open(url).map_err(|e| AppError::Config(Box::new(e)))?;
ConnectionManager::new(client)
.await
.map_err(|e| AppError::Internal(Box::new(e)))
})
.await?;
Ok(manager.clone())
}