From 879ab82caebc98398e4905895769ea8c13d2aeeb Mon Sep 17 00:00:00 2001 From: Shautvast Date: Fri, 3 Apr 2026 17:23:24 +0200 Subject: [PATCH] reuse redis connection --- backend/src/services/cache.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/backend/src/services/cache.rs b/backend/src/services/cache.rs index 36e10bd..86b85b4 100644 --- a/backend/src/services/cache.rs +++ b/backend/src/services/cache.rs @@ -1,20 +1,29 @@ use redis::AsyncCommands; use sha2::{Digest, Sha256}; +use tokio::sync::OnceCell; /// Redis caching layer implementing the key patterns and TTLs /// defined in DATA_MODEL.md section 3. pub struct CacheService { client: redis::Client, + conn: OnceCell, } impl CacheService { pub fn new(redis_url: &str) -> Result { let client = redis::Client::open(redis_url)?; - Ok(Self { client }) + Ok(Self { + client, + conn: OnceCell::new(), + }) } async fn conn(&self) -> Result { - self.client.get_multiplexed_async_connection().await + let conn = self + .conn + .get_or_try_init(|| self.client.get_multiplexed_async_connection()) + .await?; + Ok(conn.clone()) } // ── Tile cache ──────────────────────────────────────────────