cot 0.5.0

The Rust web framework for lazy developers.
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
//! Redis cache store implementation.
//!
//! This store uses Redis as the backend for caching.
//! # Examples
//! ```no_run
//! # use cot::cache::store::redis::Redis;
//! # use cot::cache::store::CacheStore;
//! # use cot::config::CacheUrl;
//! # #[tokio::main]
//! # async fn main() {
//! let store = Redis::new(&CacheUrl::from("redis://127.0.0.1:6379"), 16).unwrap();
//! let key = "example_key".to_string();
//! let value = serde_json::json!({"data": "example_value"});
//! store.insert(key.clone(), value.clone(), Default::default()).await.unwrap();
//! let retrieved  = store.get(&key).await.unwrap();
//!
//! assert_eq!(retrieved, Some(value));
//! # }
use cot::cache::store::CacheStoreResult;
use cot::config::Timeout;
use deadpool_redis::{Config, Connection, Pool, Runtime};
use redis::{AsyncCommands, SetExpiry, SetOptions};
use serde_json::Value;
use thiserror::Error;

use crate::cache::store::{CacheStore, CacheStoreError};
use crate::config::CacheUrl;
use crate::error::error_impl::impl_into_cot_error;

const ERROR_PREFIX: &str = "redis cache store error:";

/// Errors specific to the Redis cache store.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum RedisCacheStoreError {
    /// An error occurred during Redis connection pool creation.
    #[error("{ERROR_PREFIX} redis pool creation error: {0}")]
    PoolCreation(Box<dyn std::error::Error + Send + Sync>),

    /// An error occurred during a pool connection or checkout.
    #[error("{ERROR_PREFIX} redis pool connection error: {0}")]
    PoolConnection(Box<dyn std::error::Error + Send + Sync>),

    /// An error occurred during a Redis command execution.
    #[error("{ERROR_PREFIX} redis command error: {0}")]
    RedisCommand(Box<dyn std::error::Error + Send + Sync>),

    /// The provided Redis connection string is invalid.
    #[error("{ERROR_PREFIX} invalid redis connection string: {0}")]
    InvalidConnectionString(String),

    /// An error occurred during JSON serialization.
    #[error("{ERROR_PREFIX} serialization error: {0}")]
    Serialize(Box<dyn std::error::Error + Send + Sync>),

    /// An error occurred during JSON deserialization.
    #[error("{ERROR_PREFIX} deserialization error: {0}")]
    Deserialize(Box<dyn std::error::Error + Send + Sync>),
}

impl_into_cot_error!(RedisCacheStoreError);

impl From<RedisCacheStoreError> for CacheStoreError {
    fn from(err: RedisCacheStoreError) -> Self {
        let full = err.to_string();

        match err {
            RedisCacheStoreError::Serialize(_) => CacheStoreError::Serialize(full),
            RedisCacheStoreError::Deserialize(_) => CacheStoreError::Deserialize(full),
            _ => CacheStoreError::Backend(full),
        }
    }
}

/// A Redis-backed cache store implementation.
///
/// This store uses Redis as the backend for caching.
///
/// # Examples
/// ```
/// use cot::cache::store::redis::Redis;
/// use cot::config::CacheUrl;
///
/// let store = Redis::new(&CacheUrl::from("redis://127.0.0.1/"), 16).unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct Redis {
    pool: Pool,
}

impl Redis {
    /// Creates and configures a new Redis cache store.
    ///
    /// This initializes a connection pool to the Redis server specified by the
    /// provided URL.
    ///
    /// # Errors
    ///
    /// Returns [`RedisCacheStoreError::InvalidConnectionString`] if the
    /// provided URL is not a valid Redis URL
    /// and [`RedisCacheStoreError::PoolCreation`] if the connection pool could
    /// not be created.
    ///
    /// # Examples
    ///
    /// ```
    /// use cot::config::CacheUrl;
    /// use cot::cache::store::redis::Redis;
    ///
    /// let store = Redis::new(&CacheUrl::from("redis://127.0.0.1/"), 16).unwrap();
    ///  ```
    pub fn new(url: &CacheUrl, pool_size: usize) -> CacheStoreResult<Self> {
        if url.scheme() != "redis" {
            return Err(
                RedisCacheStoreError::InvalidConnectionString(url.as_str().to_string()).into(),
            );
        }
        let cfg = Config::from_url(url.as_str())
            .builder()
            .map_err(|e| RedisCacheStoreError::PoolCreation(Box::new(e)))?
            .max_size(pool_size)
            .runtime(Runtime::Tokio1)
            .build()
            .map_err(|e| RedisCacheStoreError::PoolCreation(Box::new(e)))?;

        Ok(Self { pool: cfg })
    }

    /// Get a connection from the Redis connection pool.
    ///
    /// # Errors
    ///
    /// Returns [`RedisCacheStoreError::PoolConnection`] if a connection could
    /// not be obtained from the pool.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use cot::cache::store::redis::Redis;
    /// use cot::config::CacheUrl;
    ///
    /// # #[tokio::main]
    /// # async fn main() -> cot::cache::store::CacheStoreResult<()> {
    /// let store = Redis::new(&CacheUrl::from("redis://127.0.0.1/"), 16).unwrap();
    /// let mut conn = store.get_connection().await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_connection(&self) -> Result<Connection, RedisCacheStoreError> {
        self.pool
            .get()
            .await
            .map_err(|e| RedisCacheStoreError::PoolConnection(Box::new(e)))
    }
}

impl CacheStore for Redis {
    async fn get(&self, key: &str) -> CacheStoreResult<Option<Value>> {
        let mut conn = self.get_connection().await?;
        let data: Option<String> = conn
            .get(key)
            .await
            .map_err(|e| RedisCacheStoreError::RedisCommand(Box::new(e)))?;

        data.map(|d| {
            let value = serde_json::from_str::<Value>(&d)
                .map_err(|err| RedisCacheStoreError::Deserialize(Box::new(err)))?;
            Ok(value)
        })
        .transpose()
    }

    async fn insert(&self, key: String, value: Value, expiry: Timeout) -> CacheStoreResult<()> {
        let mut conn = self.get_connection().await?;
        let data = serde_json::to_string(&value)
            .map_err(|e| RedisCacheStoreError::Serialize(Box::new(e)))?;
        let mut options = SetOptions::default();

        match expiry {
            Timeout::After(duration) => {
                options = options.with_expiration(SetExpiry::EX(duration.as_secs()));
            }
            Timeout::AtDateTime(dt) => {
                let unix_timestamp = dt.timestamp().unsigned_abs();
                options = options.with_expiration(SetExpiry::EXAT(unix_timestamp));
            }
            _ => {}
        }

        let _: () = conn
            .set_options(key, data, options)
            .await
            .map_err(|e| RedisCacheStoreError::RedisCommand(Box::new(e)))?;
        Ok(())
    }

    async fn remove(&self, key: &str) -> CacheStoreResult<()> {
        let mut conn = self.get_connection().await?;
        let _: () = conn
            .del(key)
            .await
            .map_err(|e| RedisCacheStoreError::RedisCommand(Box::new(e)))?;
        Ok(())
    }

    async fn clear(&self) -> CacheStoreResult<()> {
        let mut conn = self.get_connection().await?;
        let _: () = conn
            .flushdb()
            .await
            .map_err(|e| RedisCacheStoreError::RedisCommand(Box::new(e)))?;
        Ok(())
    }

    async fn approx_size(&self) -> CacheStoreResult<usize> {
        let mut conn = self.get_connection().await?;
        let cmd = redis::cmd("DBSIZE");
        let val: usize = cmd
            .query_async(&mut conn)
            .await
            .map_err(|err| RedisCacheStoreError::RedisCommand(Box::new(err)))?;
        Ok(val)
    }

    async fn contains_key(&self, key: &str) -> CacheStoreResult<bool> {
        let mut conn = self.get_connection().await?;
        let exists = conn
            .exists(key)
            .await
            .map_err(|e| RedisCacheStoreError::RedisCommand(Box::new(e)))?;
        Ok(exists)
    }
}

#[cfg(test)]
mod tests {
    use std::env;
    use std::time::Duration;

    use serde_json::json;

    use super::*;
    use crate::config::Timeout;

    async fn make_store(db: &str) -> Redis {
        let redis_url =
            env::var("REDIS_URL").unwrap_or_else(|_| "redis://127.0.0.1:6379/".to_string());
        let mut url = CacheUrl::from(redis_url);
        url.inner_mut().set_path(db);
        let store = Redis::new(&url, 16).expect("failed to create redis store");
        store
            .get_connection()
            .await
            .expect("failed to get redis connection");
        store
    }

    #[cot::test]
    async fn test_new_redis_store_invalid_url() {
        let store = Redis::new(&CacheUrl::from("file://tmp/random"), 16);
        assert!(store.is_err());
    }

    #[cot::test]
    #[ignore = "requires a running redis instance"]
    async fn test_insert_and_get() {
        let store = make_store("1").await;
        let key = "test_key".to_string();
        let value = json!({"data": 123});

        store
            .insert(key.clone(), value.clone(), Timeout::default())
            .await
            .unwrap();
        let retrieved = store.get(&key).await.unwrap();
        assert_eq!(retrieved, Some(value));
    }

    #[cot::test]
    #[ignore = "requires a running redis instance"]
    async fn test_get_after_expiry() {
        let store = make_store("1").await;
        let key = "temp_key__".to_string();
        let value = json!({"data": "temporary"});
        let short_timeout = Timeout::After(Duration::from_secs(1));
        store
            .insert(key.clone(), value, short_timeout)
            .await
            .unwrap();
        tokio::time::sleep(Duration::from_secs(2)).await;
        let retrieved = store.get(&key).await.unwrap();
        assert_eq!(retrieved, None);
    }

    #[cot::test]
    #[ignore = "requires a running redis instance"]
    async fn test_insert_with_expiry_types() {
        let store = make_store("1").await;

        macro_rules! run_expiry {
            ($idx:expr, $timeout:expr) => {
                {
                    let key = format!("temp_key_{}", $idx);
                    let value = json!({"data": "temporary"});
                    store
                        .insert(key.clone(), value.clone(), $timeout)
                        .await
                        .unwrap();
                    tokio::time::sleep(Duration::from_secs(3)).await;
                    let retrieved = store.get(&key).await.unwrap();
                    if $timeout == Timeout::Never {
                        assert_eq!(retrieved, Some(value));
                    }
                    else {
                        assert_eq!(retrieved, None);
                    }
                }
            };
        }

        let timeouts = vec![
            Timeout::After(Duration::from_secs(1)),
            Timeout::AtDateTime(
                (chrono::Utc::now() + chrono::Duration::seconds(1))
                    .with_timezone(&chrono::FixedOffset::east_opt(0).unwrap()),
            ),
            Timeout::Never,
        ];

        for (i, t) in timeouts.into_iter().enumerate() {
            run_expiry!(i, t);
        }
    }

    #[cot::test]
    #[ignore = "requires a running redis instance"]
    async fn test_remove() {
        let store = make_store("1").await;
        let key = "test_key_remove".to_string();
        let value = json!({"data": 123});
        store
            .insert(key.clone(), value, Timeout::default())
            .await
            .unwrap();
        store.remove(&key).await.unwrap();
        let retrieved = store.get(&key).await.unwrap();
        assert_eq!(retrieved, None);
    }

    #[cot::test]
    #[ignore = "requires a running redis instance"]
    async fn test_clear() {
        let store = make_store("2").await;
        store
            .insert("key1".to_string(), json!(1), Timeout::default())
            .await
            .unwrap();
        store
            .insert("key2".to_string(), json!(2), Timeout::default())
            .await
            .unwrap();
        assert_eq!(store.approx_size().await.unwrap(), 2);
        store.clear().await.unwrap();
        assert_eq!(store.approx_size().await.unwrap(), 0);
    }

    #[cot::test]
    #[ignore = "requires a running redis instance"]
    async fn test_contains_key() {
        let store = make_store("1").await;
        let key = "test_key".to_string();
        let value = json!({"data": 123});
        store
            .insert(key.clone(), value, Timeout::default())
            .await
            .unwrap();
        assert!(store.contains_key(&key).await.unwrap());
        store.remove(&key).await.unwrap();
        assert!(!store.contains_key(&key).await.unwrap());
    }

    #[cot::test]
    #[ignore = "requires a running redis instance"]
    async fn test_approx_size() {
        let store = make_store("3").await;
        store.clear().await.unwrap();
        store
            .insert("key1".to_string(), json!(1), Timeout::default())
            .await
            .unwrap();
        store
            .insert("key2".to_string(), json!(2), Timeout::default())
            .await
            .unwrap();
        let size = store.approx_size().await.unwrap();
        assert_eq!(size, 2);
    }

    #[cot::test]
    async fn from_redis_cache_store_error_to_cache_store_error() {
        let redis_cache_store_error =
            RedisCacheStoreError::PoolCreation(Box::new(std::io::Error::other("test")));
        let cache_store_error: CacheStoreError = redis_cache_store_error.into();
        assert_eq!(
            cache_store_error.to_string(),
            "cache store error: backend error: redis cache store error: redis pool creation error: test"
        );

        let redis_cache_store_error =
            RedisCacheStoreError::PoolConnection(Box::new(std::io::Error::other("test")));
        let cache_store_error: CacheStoreError = redis_cache_store_error.into();
        assert_eq!(
            cache_store_error.to_string(),
            "cache store error: backend error: redis cache store error: redis pool connection error: test"
        );

        let redis_cache_store_error =
            RedisCacheStoreError::RedisCommand(Box::new(std::io::Error::other("test")));
        let cache_store_error: CacheStoreError = redis_cache_store_error.into();
        assert_eq!(
            cache_store_error.to_string(),
            "cache store error: backend error: redis cache store error: redis command error: test"
        );

        let redis_cache_store_error =
            RedisCacheStoreError::InvalidConnectionString("test".to_string());
        let cache_store_error: CacheStoreError = redis_cache_store_error.into();
        assert_eq!(
            cache_store_error.to_string(),
            "cache store error: backend error: redis cache store error: invalid redis connection string: test"
        );

        let redis_cache_store_error =
            RedisCacheStoreError::Serialize(Box::new(std::io::Error::other("test")));
        let cache_store_error: CacheStoreError = redis_cache_store_error.into();
        assert_eq!(
            cache_store_error.to_string(),
            "cache store error: serialization error: redis cache store error: serialization error: test"
        );

        let redis_cache_store_error =
            RedisCacheStoreError::Deserialize(Box::new(std::io::Error::other("test")));
        let cache_store_error: CacheStoreError = redis_cache_store_error.into();
        assert_eq!(
            cache_store_error.to_string(),
            "cache store error: deserialization error: redis cache store error: deserialization error: test"
        );
    }
}