1use std::{
4 sync::Arc,
5 time::{Duration, SystemTime},
6};
7
8use redis::{Client, IntoConnectionInfo, aio::ConnectionManager};
9
10use sisa_caching_core::{
11 cache::{CacheEntry, CacheError, CacheStore, CacheValue, KEY_SEPARATOR, KeyPrefix},
12 clock::{Clock, SystemClock},
13 locker::Locker,
14 metrics::{Layer, METRICS, Reads},
15 settings::CacheSettings,
16};
17
18use crate::{
19 connection::{RedisConnection, connection_config},
20 error::RedisCacheError,
21 settings::RedisCacheSettings,
22};
23
24const VALUE: &str = "value";
25const CREATED_AT: &str = "created_at";
26const TTL_MS: &str = "ttl_ms";
27
28const MAX_TTL_MS: u64 = i64::MAX as u64 / 2;
31
32type StoredFields = (Option<Vec<u8>>, Option<u64>, Option<u64>);
34
35fn escape_glob(text: &str) -> String {
37 let mut escaped = String::with_capacity(text.len());
38
39 for character in text.chars() {
40 if "*?[]\\".contains(character) {
41 escaped.push('\\');
42 }
43 escaped.push(character);
44 }
45
46 escaped
47}
48
49fn stored_entry<CV: CacheValue>(
52 (encoded_value, created_at_ms, ttl_ms): StoredFields,
53 pttl_ms: i64,
54 requested_at: SystemTime,
55) -> serde_json::Result<Option<CacheEntry<CV>>> {
56 let Some(encoded_value) = encoded_value.filter(|_| pttl_ms >= -1) else {
58 return Ok(None);
59 };
60 let expires_at = u64::try_from(pttl_ms)
62 .ok()
63 .and_then(|ms| requested_at.checked_add(Duration::from_millis(ms)));
64 let created_at = created_at_ms
67 .and_then(|ms| SystemTime::UNIX_EPOCH.checked_add(Duration::from_millis(ms)))
68 .unwrap_or(SystemTime::UNIX_EPOCH);
69 let entry = CacheEntry::new(
70 serde_json::from_slice(&encoded_value)?,
71 created_at,
72 ttl_ms.map(Duration::from_millis),
73 )
74 .with_expires_at(expires_at);
75
76 Ok((!entry.is_expired(SystemClock.now())).then_some(entry))
77}
78
79#[derive(Clone)]
88pub struct RedisCacheStore<Conn = ConnectionManager>
89where
90 Conn: RedisConnection,
91{
92 conn: Conn,
93 name: KeyPrefix,
94 settings: RedisCacheSettings,
95 locker: Arc<Locker>,
96 reads: &'static Reads,
97}
98
99impl<Conn: RedisConnection> std::fmt::Debug for RedisCacheStore<Conn> {
101 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102 formatter
103 .debug_struct("RedisCacheStore")
104 .field("name", &self.name)
105 .field("settings", &self.settings)
106 .finish_non_exhaustive()
107 }
108}
109
110impl<Conn> RedisCacheStore<Conn>
111where
112 Conn: RedisConnection,
113{
114 pub fn new(name: &str, conn: Conn) -> Result<Self, CacheError> {
131 let name = KeyPrefix::new(name)?;
132
133 Ok(Self {
134 reads: METRICS.reads(&name, Layer::Redis),
135 name,
136 conn,
137 settings: RedisCacheSettings::default(),
138 locker: Arc::default(),
139 })
140 }
141
142 pub fn with_settings(mut self, settings: RedisCacheSettings) -> Result<Self, CacheError> {
145 settings.validate()?;
146 self.settings = settings;
147
148 Ok(self)
149 }
150
151 pub fn settings(&self) -> &RedisCacheSettings {
153 &self.settings
154 }
155
156 async fn unlink(&self, prefix: Option<&str>) -> crate::error::Result<usize> {
157 let scope = escape_glob(&self.name.format_scope());
158 let pattern = match prefix {
159 Some(prefix) => format!("{scope}{}{KEY_SEPARATOR}*", escape_glob(prefix)),
160 None => format!("{scope}*"),
161 };
162
163 let mut conn = self.conn.clone();
164 let mut removed = 0;
165 let batch_size = self.settings.redis.scan_batch_size;
166
167 for node in conn.primaries().await? {
169 let mut cursor: u64 = 0;
170
171 loop {
172 let scan = redis::cmd("SCAN")
173 .arg(cursor)
174 .arg("MATCH")
175 .arg(&pattern)
176 .arg("COUNT")
177 .arg(batch_size)
178 .to_owned();
179 let reply = conn.query_node(node.as_ref(), &scan).await?;
180 let (next, keys): (u64, Vec<Vec<u8>>) =
181 redis::from_redis_value(reply.extract_error()?)
182 .map_err(redis::RedisError::from)?;
183
184 if !keys.is_empty() {
186 removed += redis::cmd("UNLINK")
187 .arg(&keys)
188 .query_async::<usize>(&mut conn)
189 .await?;
190 }
191
192 if next == 0 {
193 break;
194 }
195
196 cursor = next;
197 }
198 }
199
200 Ok(removed)
201 }
202}
203
204impl RedisCacheStore {
205 pub async fn connect(name: &str, info: impl IntoConnectionInfo) -> crate::error::Result<Self> {
211 Self::from_client(name, Client::open(info)?).await
212 }
213
214 pub async fn from_client(name: &str, client: Client) -> crate::error::Result<Self> {
240 KeyPrefix::validate(name)?;
241
242 let conn = ConnectionManager::new_with_config(client, connection_config()).await?;
243
244 Ok(Self::new(name, conn)?)
245 }
246}
247
248impl<Conn: RedisConnection> sisa_caching_core::sealed::Sealed for RedisCacheStore<Conn> {}
250
251impl<Conn> CacheStore for RedisCacheStore<Conn>
253where
254 Conn: RedisConnection,
255{
256 type Error = RedisCacheError;
257 type Clock = SystemClock;
259
260 fn name(&self) -> &KeyPrefix {
261 &self.name
262 }
263
264 fn cache_settings(&self) -> &CacheSettings {
265 &self.settings.common
266 }
267
268 fn locker(&self) -> &Locker {
269 &self.locker
270 }
271
272 fn clock(&self) -> &SystemClock {
273 &SystemClock
274 }
275
276 #[tracing::instrument(level = "debug", name = "redis get", skip_all, fields(key = %self.name.format_key(key)), err(level = "debug"))]
277 async fn get_entry<CV: CacheValue>(
278 &self,
279 key: &str,
280 ) -> crate::error::Result<Option<CacheEntry<CV>>> {
281 let redis_key = self.name.format_key(key);
282 let mut conn = self.conn.clone();
283 let requested_at = SystemClock.now();
285 let (fields, pttl_ms): (StoredFields, i64) = redis::pipe()
286 .atomic()
287 .cmd("HMGET")
288 .arg(&redis_key)
289 .arg(VALUE)
290 .arg(CREATED_AT)
291 .arg(TTL_MS)
292 .cmd("PTTL")
293 .arg(&redis_key)
294 .query_async(&mut conn)
295 .await?;
296 let entry = stored_entry(fields, pttl_ms, requested_at);
297
298 self.reads.record(matches!(entry, Ok(Some(_))));
300
301 Ok(entry?)
302 }
303
304 #[tracing::instrument(level = "debug", name = "redis set", skip_all, fields(key = %self.name.format_key(key)), err(level = "debug"))]
305 async fn set_entry<CV: CacheValue>(
306 &self,
307 key: &str,
308 entry: CacheEntry<CV>,
309 ) -> crate::error::Result<()> {
310 let redis_key = self.name.format_key(key);
311 let encoded_value = serde_json::to_vec(&entry.value)?;
312 let created_at = entry
313 .created_at()
314 .duration_since(SystemTime::UNIX_EPOCH)
315 .unwrap_or_default()
316 .as_millis() as u64;
317 let to_ms = |ttl: Duration| {
319 u64::try_from(ttl.as_nanos().div_ceil(1_000_000))
320 .ok()
321 .filter(|ms| *ms <= MAX_TTL_MS)
322 };
323 let ttl_ms = entry.ttl().and_then(to_ms);
324 let remaining_ms = entry.remaining_ttl(SystemClock.now()).and_then(to_ms);
325 let mut conn = self.conn.clone();
326 let mut pipe = redis::pipe();
327
328 pipe.atomic().cmd("DEL").arg(&redis_key).ignore();
329
330 if remaining_ms != Some(0) {
332 let hset = pipe
333 .cmd("HSET")
334 .arg(&redis_key)
335 .arg(VALUE)
336 .arg(&encoded_value[..])
337 .arg(CREATED_AT)
338 .arg(created_at);
339
340 if let Some(ttl_ms) = ttl_ms {
341 hset.arg(TTL_MS).arg(ttl_ms);
342 }
343
344 hset.ignore();
345
346 if let Some(remaining_ms) = remaining_ms {
347 pipe.cmd("PEXPIRE")
348 .arg(&redis_key)
349 .arg(remaining_ms)
350 .ignore();
351 }
352 }
353
354 pipe.query_async::<()>(&mut conn).await?;
355
356 Ok(())
357 }
358
359 #[tracing::instrument(level = "debug", name = "redis remove", skip_all, fields(key = %self.name.format_key(key)), err(level = "debug"))]
360 async fn remove(&self, key: &str) -> crate::error::Result<bool> {
361 let removed: usize = redis::cmd("UNLINK")
362 .arg(self.name.format_key(key))
363 .query_async(&mut self.conn.clone())
364 .await?;
365
366 Ok(removed > 0)
367 }
368
369 #[tracing::instrument(level = "debug", name = "redis remove_prefix", skip_all, fields(store = %self.name, %prefix), err(level = "debug"))]
370 async fn remove_prefix(&self, prefix: &str) -> crate::error::Result<usize> {
371 KeyPrefix::validate(prefix)?;
372
373 self.unlink(Some(prefix)).await
374 }
375
376 #[tracing::instrument(level = "debug", name = "redis clear", skip_all, fields(store = %self.name), err(level = "debug"))]
377 async fn clear(&self) -> crate::error::Result<()> {
378 self.unlink(None).await?;
379
380 Ok(())
381 }
382}
383
384#[cfg(test)]
385mod tests {
386 use super::*;
387
388 #[test]
389 fn glob_characters_are_escaped() {
390 assert_eq!(escape_glob(r"a*b?c[d]e\f"), r"a\*b\?c\[d\]e\\f");
391 assert_eq!(escape_glob("app::user"), "app::user");
392 }
393}