loco-rs 0.16.0

The one-person framework for Rust
Documentation
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! # Cache Module
//!
//! This module provides a generic cache interface for various cache drivers.
pub mod drivers;

use std::{future::Future, time::Duration};

use serde::{de::DeserializeOwned, Serialize};

use self::drivers::CacheDriver;
use crate::config;
use crate::Result as LocoResult;
use std::sync::Arc;

/// Errors related to cache operations
#[derive(thiserror::Error, Debug)]
#[allow(clippy::module_name_repetitions)]
pub enum CacheError {
    #[error(transparent)]
    Any(#[from] Box<dyn std::error::Error + Send + Sync>),

    #[error("Serialization error: {0}")]
    Serialization(String),

    #[error("Deserialization error: {0}")]
    Deserialization(String),

    #[cfg(feature = "cache_redis")]
    #[error(transparent)]
    Redis(#[from] bb8_redis::redis::RedisError),

    #[cfg(feature = "cache_redis")]
    #[error(transparent)]
    RedisConnectionError(#[from] bb8_redis::bb8::RunError<bb8_redis::redis::RedisError>),
}

pub type CacheResult<T> = std::result::Result<T, CacheError>;

/// Create a provider
///
/// # Errors
///
/// This function will return an error if fails to build
#[allow(clippy::unused_async)]
pub async fn create_cache_provider(config: &config::Config) -> crate::Result<Arc<Cache>> {
    match &config.cache {
        #[cfg(feature = "cache_redis")]
        config::CacheConfig::Redis(config) => {
            let cache = crate::cache::drivers::redis::new(config).await?;
            Ok(Arc::new(cache))
        }
        #[cfg(feature = "cache_inmem")]
        config::CacheConfig::InMem(config) => {
            let cache = crate::cache::drivers::inmem::new(config);
            Ok(Arc::new(cache))
        }
        config::CacheConfig::Null => {
            let driver = crate::cache::drivers::null::new();
            Ok(Arc::new(Cache::new(driver)))
        }
    }
}

/// Represents a cache instance
pub struct Cache {
    /// The cache driver used for underlying operations
    pub driver: Box<dyn CacheDriver>,
}

impl Cache {
    /// Creates a new cache instance with the specified cache driver.
    #[must_use]
    pub fn new(driver: Box<dyn CacheDriver>) -> Self {
        Self { driver }
    }

    /// Checks if a key exists in the cache.
    ///
    /// # Example
    /// ```
    /// use loco_rs::cache::{self, CacheResult};
    /// use loco_rs::config::InMemCacheConfig;
    ///
    /// pub async fn contains_key() -> CacheResult<bool> {
    ///     let config = InMemCacheConfig { max_capacity: 100 };
    ///     let cache = cache::Cache::new(cache::drivers::inmem::new(&config).driver);
    ///     cache.contains_key("key").await
    /// }
    /// ```
    ///
    /// # Errors
    /// A [`CacheResult`] indicating whether the key exists in the cache.
    pub async fn contains_key(&self, key: &str) -> CacheResult<bool> {
        self.driver.contains_key(key).await
    }

    /// Retrieves a value from the cache based on the provided key and deserializes it.
    ///
    /// # Example
    /// ```
    /// use loco_rs::cache::{self, CacheResult};
    /// use loco_rs::config::InMemCacheConfig;
    /// use serde::Deserialize;
    ///
    /// #[derive(Deserialize)]
    /// struct User {
    ///     name: String,
    ///     age: u32,
    /// }
    ///
    /// pub async fn get_user() -> CacheResult<Option<User>> {
    ///     let config = InMemCacheConfig { max_capacity: 100 };
    ///     let cache = cache::Cache::new(cache::drivers::inmem::new(&config).driver);
    ///     cache.get::<User>("user:1").await
    /// }
    /// ```
    ///
    /// # Example with String
    /// ```
    /// use loco_rs::cache::{self, CacheResult};
    /// use loco_rs::config::InMemCacheConfig;
    ///
    /// pub async fn get_string() -> CacheResult<Option<String>> {
    ///     let config = InMemCacheConfig { max_capacity: 100 };
    ///     let cache = cache::Cache::new(cache::drivers::inmem::new(&config).driver);
    ///     cache.get::<String>("key").await
    /// }
    /// ```
    ///
    /// # Errors
    /// A [`CacheResult`] containing an `Option` representing the retrieved
    /// and deserialized value.
    pub async fn get<T: DeserializeOwned>(&self, key: &str) -> CacheResult<Option<T>> {
        let result = self.driver.get(key).await?;
        if let Some(value) = result {
            let deserialized = serde_json::from_str::<T>(&value)
                .map_err(|e| CacheError::Deserialization(e.to_string()))?;
            Ok(Some(deserialized))
        } else {
            Ok(None)
        }
    }

    /// Inserts a serializable value into the cache with the provided key.
    ///
    /// # Example
    /// ```
    /// use loco_rs::cache::{self, CacheResult};
    /// use loco_rs::config::InMemCacheConfig;
    /// use serde::Serialize;
    ///
    /// #[derive(Serialize)]
    /// struct User {
    ///     name: String,
    ///     age: u32,
    /// }
    ///
    /// pub async fn insert() -> CacheResult<()> {
    ///     let config = InMemCacheConfig { max_capacity: 100 };
    ///     let cache = cache::Cache::new(cache::drivers::inmem::new(&config).driver);
    ///     let user = User { name: "Alice".to_string(), age: 30 };
    ///     cache.insert("user:1", &user).await
    /// }
    /// ```
    ///
    /// # Example with String
    /// ```
    /// use loco_rs::cache::{self, CacheResult};
    /// use loco_rs::config::InMemCacheConfig;
    ///
    /// pub async fn insert_string() -> CacheResult<()> {
    ///     let config = InMemCacheConfig { max_capacity: 100 };
    ///     let cache = cache::Cache::new(cache::drivers::inmem::new(&config).driver);
    ///     cache.insert("key", &"value".to_string()).await
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// A [`CacheResult`] indicating the success of the operation.
    pub async fn insert<T: Serialize + Sync + ?Sized>(
        &self,
        key: &str,
        value: &T,
    ) -> CacheResult<()> {
        let serialized =
            serde_json::to_string(value).map_err(|e| CacheError::Serialization(e.to_string()))?;
        self.driver.insert(key, &serialized).await
    }

    /// Inserts a serializable value into the cache with the provided key and expiry duration.
    ///
    /// # Example
    /// ```
    /// use std::time::Duration;
    /// use loco_rs::cache::{self, CacheResult};
    /// use loco_rs::config::InMemCacheConfig;
    /// use serde::Serialize;
    ///
    /// #[derive(Serialize)]
    /// struct User {
    ///     name: String,
    ///     age: u32,
    /// }
    ///
    /// pub async fn insert() -> CacheResult<()> {
    ///     let config = InMemCacheConfig { max_capacity: 100 };
    ///     let cache = cache::Cache::new(cache::drivers::inmem::new(&config).driver);
    ///     let user = User { name: "Alice".to_string(), age: 30 };
    ///     cache.insert_with_expiry("user:1", &user, Duration::from_secs(300)).await
    /// }
    /// ```
    ///
    /// # Example with String
    /// ```
    /// use std::time::Duration;
    /// use loco_rs::cache::{self, CacheResult};
    /// use loco_rs::config::InMemCacheConfig;
    ///
    /// pub async fn insert_string() -> CacheResult<()> {
    ///     let config = InMemCacheConfig { max_capacity: 100 };
    ///     let cache = cache::Cache::new(cache::drivers::inmem::new(&config).driver);
    ///     cache.insert_with_expiry("key", &"value".to_string(), Duration::from_secs(300)).await
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// A [`CacheResult`] indicating the success of the operation.
    pub async fn insert_with_expiry<T: Serialize + Sync + ?Sized>(
        &self,
        key: &str,
        value: &T,
        duration: Duration,
    ) -> CacheResult<()> {
        let serialized =
            serde_json::to_string(value).map_err(|e| CacheError::Serialization(e.to_string()))?;
        self.driver
            .insert_with_expiry(key, &serialized, duration)
            .await
    }

    /// Retrieves and deserializes the value associated with the given key from the cache,
    /// or inserts it if it does not exist, using the provided closure to
    /// generate the value.
    ///
    /// # Example
    /// ```
    /// use loco_rs::{app::AppContext};
    /// use loco_rs::tests_cfg::app::*;
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(Serialize, Deserialize, PartialEq, Debug)]
    /// struct User {
    ///     name: String,
    ///     age: u32,
    /// }
    ///
    /// pub async fn get_or_insert(){
    ///    let app_ctx = get_app_context().await;
    ///    let user = app_ctx.cache.get_or_insert::<User, _>("user:1", async {
    ///            Ok(User { name: "Alice".to_string(), age: 30 })
    ///     }).await.unwrap();
    ///    assert_eq!(user.name, "Alice");
    /// }
    /// ```
    ///
    /// # Example with String
    /// ```
    /// use loco_rs::{app::AppContext};
    /// use loco_rs::tests_cfg::app::*;
    ///
    /// pub async fn get_or_insert_string(){
    ///    let app_ctx = get_app_context().await;
    ///    let res = app_ctx.cache.get_or_insert::<String, _>("key", async {
    ///            Ok("value".to_string())
    ///     }).await.unwrap();
    ///    assert_eq!(res, "value");
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// A [`LocoResult`] indicating the success of the operation.
    pub async fn get_or_insert<T, F>(&self, key: &str, f: F) -> LocoResult<T>
    where
        T: Serialize + DeserializeOwned + Send + Sync,
        F: Future<Output = LocoResult<T>> + Send,
    {
        if let Some(value) = self.get::<T>(key).await? {
            Ok(value)
        } else {
            let value = f.await?;
            self.insert(key, &value).await?;
            Ok(value)
        }
    }

    /// Retrieves and deserializes the value associated with the given key from the cache,
    /// or inserts it (with expiry after provided duration) if it does not
    /// exist, using the provided closure to generate the value.
    ///
    /// # Example
    /// ```
    /// use std::time::Duration;
    /// use loco_rs::{app::AppContext};
    /// use loco_rs::tests_cfg::app::*;
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(Serialize, Deserialize, PartialEq, Debug)]
    /// struct User {
    ///     name: String,
    ///     age: u32,
    /// }
    ///
    /// pub async fn get_or_insert(){
    ///    let app_ctx = get_app_context().await;
    ///    let user = app_ctx.cache.get_or_insert_with_expiry::<User, _>("user:1", Duration::from_secs(300), async {
    ///            Ok(User { name: "Alice".to_string(), age: 30 })
    ///     }).await.unwrap();
    ///    assert_eq!(user.name, "Alice");
    /// }
    /// ```
    ///
    /// # Example with String
    /// ```
    /// use std::time::Duration;
    /// use loco_rs::{app::AppContext};
    /// use loco_rs::tests_cfg::app::*;
    ///
    /// pub async fn get_or_insert_string(){
    ///    let app_ctx = get_app_context().await;
    ///    let res = app_ctx.cache.get_or_insert_with_expiry::<String, _>("key", Duration::from_secs(300), async {
    ///            Ok("value".to_string())
    ///     }).await.unwrap();
    ///    assert_eq!(res, "value");
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// A [`LocoResult`] indicating the success of the operation.
    pub async fn get_or_insert_with_expiry<T, F>(
        &self,
        key: &str,
        duration: Duration,
        f: F,
    ) -> LocoResult<T>
    where
        T: Serialize + DeserializeOwned + Send + Sync,
        F: Future<Output = LocoResult<T>> + Send,
    {
        if let Some(value) = self.get::<T>(key).await? {
            Ok(value)
        } else {
            let value = f.await?;
            self.insert_with_expiry(key, &value, duration).await?;
            Ok(value)
        }
    }

    /// Removes a key-value pair from the cache.
    ///
    /// # Example
    /// ```
    /// use loco_rs::cache::{self, CacheResult};
    /// use loco_rs::config::InMemCacheConfig;
    ///
    /// pub async fn remove() -> CacheResult<()> {
    ///     let config = InMemCacheConfig { max_capacity: 100 };
    ///     let cache = cache::Cache::new(cache::drivers::inmem::new(&config).driver);
    ///     cache.remove("key").await
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// A [`CacheResult`] indicating the success of the operation.
    pub async fn remove(&self, key: &str) -> CacheResult<()> {
        self.driver.remove(key).await
    }

    /// Clears all key-value pairs from the cache.
    ///
    /// # Example
    /// ```
    /// use loco_rs::cache::{self, CacheResult};
    /// use loco_rs::config::InMemCacheConfig;
    ///
    /// pub async fn clear() -> CacheResult<()> {
    ///     let config = InMemCacheConfig { max_capacity: 100 };
    ///     let cache = cache::Cache::new(cache::drivers::inmem::new(&config).driver);
    ///     cache.clear().await
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// A [`CacheResult`] indicating the success of the operation.
    pub async fn clear(&self) -> CacheResult<()> {
        self.driver.clear().await
    }
}

#[cfg(test)]
mod tests {

    use crate::tests_cfg;
    use serde::{Deserialize, Serialize};

    #[tokio::test]
    async fn can_get_or_insert() {
        let app_ctx = tests_cfg::app::get_app_context().await;
        let get_key = "loco";

        assert_eq!(app_ctx.cache.get::<String>(get_key).await.unwrap(), None);

        let result = app_ctx
            .cache
            .get_or_insert::<String, _>(get_key, async { Ok("loco-cache-value".to_string()) })
            .await
            .unwrap();

        assert_eq!(result, "loco-cache-value".to_string());
        assert_eq!(
            app_ctx.cache.get::<String>(get_key).await.unwrap(),
            Some("loco-cache-value".to_string())
        );
    }

    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct TestUser {
        name: String,
        age: u32,
    }

    #[tokio::test]
    async fn can_serialize_deserialize() {
        let app_ctx = tests_cfg::app::get_app_context().await;
        let key = "user:test";

        // Test user data
        let user = TestUser {
            name: "Test User".to_string(),
            age: 42,
        };

        // Insert serialized user
        app_ctx.cache.insert(key, &user).await.unwrap();

        // Retrieve and deserialize user
        let retrieved: Option<TestUser> = app_ctx.cache.get(key).await.unwrap();
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap(), user);
    }

    #[tokio::test]
    async fn can_get_or_insert_generic() {
        let app_ctx = tests_cfg::app::get_app_context().await;
        let key = "user:get_or_insert";

        // The key should not exist initially
        let no_user: Option<TestUser> = app_ctx.cache.get(key).await.unwrap();
        assert!(no_user.is_none());

        // Get or insert should create the user
        let user = app_ctx
            .cache
            .get_or_insert::<TestUser, _>(key, async {
                Ok(TestUser {
                    name: "Alice".to_string(),
                    age: 30,
                })
            })
            .await
            .unwrap();

        assert_eq!(user.name, "Alice");
        assert_eq!(user.age, 30);

        // Verify the user was stored in the cache
        let retrieved: TestUser = app_ctx
            .cache
            .get_or_insert::<TestUser, _>(key, async {
                // This should not be called
                Ok(TestUser {
                    name: "Bob".to_string(),
                    age: 25,
                })
            })
            .await
            .unwrap();

        // Should retrieve Alice, not Bob
        assert_eq!(retrieved.name, "Alice");
        assert_eq!(retrieved.age, 30);
    }
}