Skip to main content

multi_tier_cache/backends/
redis_cache.rs

1use crate::error::CacheResult;
2use crate::traits::{CacheBackend, L2CacheBackend};
3use bytes::Bytes;
4use futures_util::future::BoxFuture;
5use redis::aio::ConnectionManager;
6use redis::{AsyncCommands, Client};
7use std::sync::Arc;
8use std::sync::atomic::{AtomicU64, Ordering};
9use std::time::Duration;
10use tracing::{debug, info};
11
12/// Redis distributed cache with `ConnectionManager` for automatic reconnection
13pub struct RedisCache {
14    /// Redis connection manager
15    conn_manager: ConnectionManager,
16    /// Hit counter
17    hits: Arc<AtomicU64>,
18    /// Miss counter
19    misses: Arc<AtomicU64>,
20    /// Set counter
21    sets: Arc<AtomicU64>,
22}
23
24impl RedisCache {
25    /// Create new Redis cache
26    ///
27    /// # Errors
28    ///
29    /// Returns an error if the Redis URL is invalid or connection fails.
30    pub async fn new() -> CacheResult<Self> {
31        let redis_url =
32            std::env::var("REDIS_URL").unwrap_or_else(|_| "redis://127.0.0.1:6379".to_string());
33        Self::with_url(&redis_url).await
34    }
35
36    /// Create new Redis cache with custom URL
37    ///
38    /// # Errors
39    ///
40    /// Returns an error if the Redis URL is invalid or connection fails.
41    pub async fn with_url(redis_url: &str) -> CacheResult<Self> {
42        info!(redis_url = %redis_url, "Initializing Redis Cache with ConnectionManager");
43
44        let client = Client::open(redis_url).map_err(|e| {
45            crate::error::CacheError::ConfigError(format!("Failed to create Redis client: {e}"))
46        })?;
47
48        let conn_manager = ConnectionManager::new(client).await.map_err(|e| {
49            crate::error::CacheError::BackendError(format!(
50                "Failed to establish Redis connection manager: {e}"
51            ))
52        })?;
53
54        let mut conn = conn_manager.clone();
55        let _: String = redis::cmd("PING")
56            .query_async(&mut conn)
57            .await
58            .map_err(|e| {
59                crate::error::CacheError::BackendError(format!(
60                    "Redis PING health check failed: {e}"
61                ))
62            })?;
63
64        info!(redis_url = %redis_url, "Redis Cache connected successfully");
65
66        Ok(Self {
67            conn_manager,
68            hits: Arc::new(AtomicU64::new(0)),
69            misses: Arc::new(AtomicU64::new(0)),
70            sets: Arc::new(AtomicU64::new(0)),
71        })
72    }
73
74    /// Scan keys matching a pattern
75    ///
76    /// # Errors
77    ///
78    /// Returns an error if the SCAN command fails.
79    pub async fn scan_keys(&self, pattern: &str) -> CacheResult<Vec<String>> {
80        let mut conn = self.conn_manager.clone();
81        let mut keys = Vec::new();
82        let mut cursor: u64 = 0;
83
84        loop {
85            let result: (u64, Vec<String>) = redis::cmd("SCAN")
86                .arg(cursor)
87                .arg("MATCH")
88                .arg(pattern)
89                .arg("COUNT")
90                .arg(100)
91                .query_async(&mut conn)
92                .await?;
93
94            cursor = result.0;
95            keys.extend(result.1);
96
97            if cursor == 0 {
98                break;
99            }
100        }
101
102        debug!(pattern = %pattern, count = keys.len(), "[Redis] Scanned keys matching pattern");
103        Ok(keys)
104    }
105
106    /// Remove multiple keys at once
107    ///
108    /// # Errors
109    ///
110    /// Returns an error if the DEL command fails.
111    pub async fn remove_bulk(&self, keys: &[String]) -> CacheResult<usize> {
112        if keys.is_empty() {
113            return Ok(0);
114        }
115
116        let mut conn = self.conn_manager.clone();
117        let count: usize = conn.del(keys).await?;
118        debug!(count = count, "[Redis] Removed keys in bulk");
119        Ok(count)
120    }
121}
122
123// ===== Trait Implementations =====
124
125/// Implement `CacheBackend` trait for `RedisCache`
126impl CacheBackend for RedisCache {
127    fn get<'a>(&'a self, key: &'a str) -> BoxFuture<'a, Option<Bytes>> {
128        Box::pin(async move {
129            let mut conn = self.conn_manager.clone();
130            let result: redis::RedisResult<Option<Vec<u8>>> = conn.get(key).await;
131            if let Ok(Some(bytes)) = result {
132                self.hits.fetch_add(1, Ordering::Relaxed);
133                Some(Bytes::from(bytes))
134            } else {
135                self.misses.fetch_add(1, Ordering::Relaxed);
136                None
137            }
138        })
139    }
140
141    fn set_with_ttl<'a>(
142        &'a self,
143        key: &'a str,
144        value: Bytes,
145        ttl: Duration,
146    ) -> BoxFuture<'a, CacheResult<()>> {
147        Box::pin(async move {
148            let mut conn = self.conn_manager.clone();
149            let ttl_ms = u64::try_from(ttl.as_millis()).unwrap_or(u64::MAX);
150            let result = conn.pset_ex(key, value.to_vec(), ttl_ms).await;
151            if result.is_ok() {
152                self.sets.fetch_add(1, Ordering::Relaxed);
153                debug!(key = %key, ttl_ms = %ttl.as_millis(), "[Redis] Cached key bytes with TTL");
154            }
155            result.map_err(|e| {
156                crate::error::CacheError::BackendError(format!("Redis set failed: {e}"))
157            })
158        })
159    }
160
161    fn remove<'a>(&'a self, key: &'a str) -> BoxFuture<'a, CacheResult<()>> {
162        Box::pin(async move {
163            let mut conn = self.conn_manager.clone();
164            let _: usize = conn.del(key).await?;
165            Ok(())
166        })
167    }
168
169    fn health_check(&self) -> BoxFuture<'_, bool> {
170        Box::pin(async move {
171            let mut conn = self.conn_manager.clone();
172            let result: redis::RedisResult<String> =
173                redis::cmd("PING").query_async(&mut conn).await;
174            result.is_ok()
175        })
176    }
177
178    fn remove_pattern<'a>(&'a self, pattern: &'a str) -> BoxFuture<'a, CacheResult<()>> {
179        Box::pin(async move {
180            let keys = self.scan_keys(pattern).await?;
181            if !keys.is_empty() {
182                self.remove_bulk(&keys).await?;
183            }
184            Ok(())
185        })
186    }
187
188    fn name(&self) -> &'static str {
189        "Redis"
190    }
191}
192
193impl L2CacheBackend for RedisCache {
194    fn get_with_ttl<'a>(
195        &'a self,
196        key: &'a str,
197    ) -> BoxFuture<'a, Option<(Bytes, Option<Duration>)>> {
198        Box::pin(async move {
199            let mut conn = self.conn_manager.clone();
200            // Pipelining is better:
201            let (bytes, ttl_secs): (Option<Vec<u8>>, i64) =
202                match redis::pipe().get(key).ttl(key).query_async(&mut conn).await {
203                    Ok(res) => res,
204                    Err(_) => return None,
205                };
206
207            if let Some(b) = bytes {
208                self.hits.fetch_add(1, Ordering::Relaxed);
209                let ttl = if ttl_secs > 0 {
210                    Some(Duration::from_secs(ttl_secs.unsigned_abs()))
211                } else {
212                    None
213                };
214                Some((Bytes::from(b), ttl))
215            } else {
216                self.misses.fetch_add(1, Ordering::Relaxed);
217                None
218            }
219        })
220    }
221}