headless_lms_utils/
cache.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//! Redis cache wrapper.

use crate::prelude::*;
use redis::{aio::ConnectionManager, AsyncCommands, Client, ToRedisArgs};
use serde::{de::DeserializeOwned, Serialize};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::OnceCell;

/// Wrapper for accessing a redis cache.
/// Operations are non-blocking and fail gracefully when Redis is unavailable.
pub struct Cache {
    connection_manager: Arc<OnceCell<ConnectionManager>>,
    initial_connection_successful: Arc<std::sync::atomic::AtomicBool>,
}

impl Clone for Cache {
    fn clone(&self) -> Self {
        Self {
            connection_manager: self.connection_manager.clone(),
            initial_connection_successful: self.initial_connection_successful.clone(),
        }
    }
}

impl std::fmt::Debug for Cache {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Cache")
            .field("client", &"<Client>")
            .field("connection", &"<ConnectionManager>")
            .field(
                "initial_connection_successful",
                &self
                    .initial_connection_successful
                    .load(std::sync::atomic::Ordering::SeqCst),
            )
            .finish()
    }
}

impl Cache {
    /// Creates a new Redis cache instance.
    ///
    /// This will succeed even if Redis is unavailable.
    /// Cache operations will be no-ops if Redis cannot be connected to.
    /// Will retry connecting in the background if initial connection fails.
    pub fn new(redis_url: &str) -> UtilResult<Self> {
        let client = Client::open(redis_url).map_err(|e| {
            UtilError::new(
                UtilErrorType::Other,
                format!("Failed to create Redis client: {e}"),
                Some(e.into()),
            )
        })?;

        let cache = Self {
            connection_manager: Arc::new(OnceCell::new()),
            initial_connection_successful: Arc::new(std::sync::atomic::AtomicBool::new(false)),
        };

        let client_clone = client.clone();
        let cache_clone = cache.clone();

        tokio::spawn(async move {
            let mut backoff = Duration::from_secs(1);
            const MAX_BACKOFF: Duration = Duration::from_secs(6000);
            const MAX_ATTEMPTS: usize = 1000;
            let mut attempt = 0;

            while attempt < MAX_ATTEMPTS {
                attempt += 1;
                info!("Attempting to establish Redis connection... (attempt {attempt})");
                let config = redis::aio::ConnectionManagerConfig::new()
                    .set_connection_timeout(Duration::from_secs(5))
                    .set_response_timeout(Duration::from_secs(2))
                    .set_number_of_retries(3);

                match ConnectionManager::new_with_config(client_clone.clone(), config).await {
                    Ok(conn_manager) => {
                        info!("Successfully established Redis connection");
                        match cache_clone.connection_manager.set(conn_manager) {
                            Ok(_) => {
                                info!("Connection manager set successfully");
                                cache_clone
                                    .initial_connection_successful
                                    .store(true, std::sync::atomic::Ordering::SeqCst);
                                trace!("Connection flag set to true");
                                break;
                            }
                            Err(_) => {
                                warn!("Failed to set connection manager - will retry");
                                continue;
                            }
                        }
                    }
                    Err(e) => {
                        warn!(
                            "Failed to establish Redis connection: {e}. Will retry in {:?}.",
                            backoff
                        );
                        tokio::time::sleep(backoff).await;
                        backoff = std::cmp::min(backoff * 2, MAX_BACKOFF);
                    }
                }
            }

            if attempt >= MAX_ATTEMPTS {
                error!("Failed to establish Redis connection after {MAX_ATTEMPTS} attempts");
                cache_clone
                    .initial_connection_successful
                    .store(false, std::sync::atomic::Ordering::SeqCst);
            }
        });

        Ok(cache)
    }

    /// Retrieves a value from cache, or executes the provided function to generate and cache the value.
    ///
    /// First checks if the key exists in the cache. If it does, returns the cached value.
    /// If not, executes the provided async function, caches its result, and returns it.
    /// Operations are non-blocking - if Redis is unavailable, the function is executed immediately.
    pub async fn get_or_set<V, F, Fut, K>(
        &self,
        key: K,
        expires_in: Duration,
        f: F,
    ) -> UtilResult<V>
    where
        V: DeserializeOwned + Serialize,
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = UtilResult<V>>,
        K: ToRedisArgs + Send + Sync + Clone + std::fmt::Debug,
    {
        if let Some(cached) = self.get_json::<V, K>(key.clone()).await {
            info!("Cache hit for key: {:?}", key);
            return Ok(cached);
        }

        info!("Cache miss for key: {:?}", key);

        // If not in cache or connection not available, execute the function
        let start = std::time::Instant::now();
        let value = f().await?;
        let duration = start.elapsed();
        info!("Generated value for key {:?} in {:?}", key, duration);

        self.cache_json(key.clone(), &value, expires_in).await;

        Ok(value)
    }

    /// Stores the given value in the redis cache as JSON.
    /// If Redis is unavailable, this function silently does nothing.
    /// This is a non-blocking operation.
    pub async fn cache_json<V, K>(&self, key: K, value: &V, expires_in: Duration) -> bool
    where
        V: Serialize,
        K: ToRedisArgs + Send + Sync,
    {
        if !self.initial_connection_successful() {
            warn!("Skipping cache_json because initial connection not successful");
            return false;
        }

        let mut connection = match self.connection_manager.get() {
            Some(conn) => conn.clone(),
            None => {
                warn!("Skipping cache_json because no connection manager");
                return false;
            }
        };

        let value = match serde_json::to_vec(value) {
            Ok(v) => v,
            Err(e) => {
                error!("Failed to serialize value to JSON: {e}");
                return false;
            }
        };

        match connection
            .set_ex::<_, _, ()>(key, value, expires_in.as_secs())
            .await
        {
            Ok(_) => {
                debug!("Successfully cached value");
                true
            }
            Err(e) => {
                error!("Failed to cache value: {e}");
                false
            }
        }
    }

    /// Retrieves and deserializes a value from cache.
    /// If Redis is unavailable, returns None.
    /// This is a non-blocking operation.
    pub async fn get_json<V, K>(&self, key: K) -> Option<V>
    where
        V: DeserializeOwned,
        K: ToRedisArgs + Send + Sync,
    {
        if !self.initial_connection_successful() {
            warn!("Skipping get_json because initial connection not successful");
            return None;
        }

        let mut connection = match self.connection_manager.get() {
            Some(conn) => conn.clone(),
            None => {
                warn!("Skipping get_json because no connection manager");
                return None;
            }
        };

        match connection.get::<_, Option<Vec<u8>>>(key).await {
            Ok(Some(bytes)) => match serde_json::from_slice(&bytes) {
                Ok(value) => {
                    debug!("Successfully retrieved and deserialized value from cache");
                    Some(value)
                }
                Err(e) => {
                    error!("Failed to deserialize value from cache: {e}");
                    None
                }
            },
            Ok(None) => {
                debug!("Key not found in cache");
                None
            }
            Err(e) => {
                if e.to_string().contains("response was nil") {
                    debug!("Got nil response from Redis");
                    return None;
                }
                error!("Failed to get value from cache: {e}");
                None
            }
        }
    }

    /// Delete a key from the cache.
    /// Returns true if the key was deleted, false otherwise.
    /// This is a non-blocking operation.
    pub async fn invalidate<K>(&self, key: K) -> bool
    where
        K: ToRedisArgs + Send + Sync,
    {
        if !self.initial_connection_successful() {
            return false;
        }

        let mut connection = match self.connection_manager.get() {
            Some(conn) => conn.clone(),
            None => return false,
        };

        match connection.del::<_, i64>(key).await {
            Ok(1) => true,
            Ok(0) => false,
            Ok(_) => true,
            Err(e) => {
                error!("Failed to invalidate cache key: {e}");
                false
            }
        }
    }

    /// Returns whether the initial connection was successful
    pub fn initial_connection_successful(&self) -> bool {
        self.initial_connection_successful
            .load(std::sync::atomic::Ordering::SeqCst)
    }

    /// Waits for the initial connection to be established, with a timeout.
    /// Returns true if connected, false if timed out.
    /// This is primarily intended for testing.
    #[cfg(test)]
    pub async fn wait_for_initial_connection(&self, timeout: Duration) -> bool {
        let start = std::time::Instant::now();
        while !self.initial_connection_successful() || self.connection_manager.get().is_none() {
            if start.elapsed() >= timeout {
                error!(
                    "Timed out waiting for Redis connection after {:?}. Flag is: {}, Connection manager: {}",
                    timeout,
                    self.initial_connection_successful(),
                    self.connection_manager.get().is_some()
                );
                return false;
            }
            tokio::time::sleep(Duration::from_millis(100)).await;
        }
        true
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use serde::Deserialize;

    #[tokio::test]
    async fn caches() {
        tracing_subscriber::fmt().init();
        let redis_url = std::env::var("REDIS_URL")
            .unwrap_or("redis://redis.default.svc.cluster.local/1".to_string());
        info!("Redis URL: {redis_url}");

        #[derive(Deserialize, Serialize, Debug, PartialEq)]
        struct S {
            field: String,
        }

        let cache = Cache::new(&redis_url).unwrap();
        let value = S {
            field: "value".to_string(),
        };

        // Wait for connection to be established
        assert!(
            cache
                .wait_for_initial_connection(Duration::from_secs(10))
                .await,
            "Failed to connect to Redis within timeout"
        );

        // Test cache_json and get_json
        let _cache_result = cache
            .cache_json("key", &value, Duration::from_secs(10))
            .await;

        let retrieved = cache.get_json::<S, _>("key").await;
        assert_eq!(
            retrieved,
            Some(S {
                field: "value".to_string()
            })
        );

        // Test get_or_set
        let result = cache
            .get_or_set("test_key", Duration::from_secs(10), || async {
                Ok(S {
                    field: "computed".to_string(),
                })
            })
            .await
            .expect("get_or_set failed");

        assert_eq!(
            result,
            S {
                field: "computed".to_string()
            }
        );

        // Test invalidate
        assert!(cache.invalidate("key").await);
        let retrieved = cache.get_json::<S, _>("key").await;
        assert_eq!(retrieved, None);
    }
}