by_loco/cache/drivers/
null.rs

1//! # Null Cache Driver
2//!
3//! The Null Cache Driver is the default cache driver implemented when the Loco
4//! framework is initialized. The primary purpose of this driver is to simplify
5//! the user workflow by avoiding the need for feature flags or optional cache
6//! driver configurations.
7use std::time::Duration;
8
9use async_trait::async_trait;
10
11use super::CacheDriver;
12use crate::cache::{CacheError, CacheResult};
13
14/// Represents the in-memory cache driver.
15#[derive(Debug)]
16pub struct Null {}
17
18/// Creates a new null cache instance
19///
20/// # Returns
21///
22/// A boxed [`CacheDriver`] instance.
23#[must_use]
24pub fn new() -> Box<dyn CacheDriver> {
25    Box::new(Null {})
26}
27
28#[async_trait]
29impl CacheDriver for Null {
30    /// Checks if a key exists in the cache.
31    ///
32    /// # Errors
33    ///
34    /// Returns always error
35    async fn contains_key(&self, _key: &str) -> CacheResult<bool> {
36        Err(CacheError::Any(
37            "Operation not supported by null cache".into(),
38        ))
39    }
40
41    /// Retrieves a value from the cache based on the provided key.
42    ///
43    /// # Errors
44    ///
45    /// Returns always error
46    async fn get(&self, _key: &str) -> CacheResult<Option<String>> {
47        Ok(None)
48    }
49
50    /// Inserts a key-value pair into the cache.
51    ///
52    /// # Errors
53    ///
54    /// Returns always error
55    async fn insert(&self, _key: &str, _value: &str) -> CacheResult<()> {
56        Err(CacheError::Any(
57            "Operation not supported by null cache".into(),
58        ))
59    }
60
61    /// Inserts a key-value pair into the cache that expires after the
62    /// provided duration.
63    ///
64    /// # Errors
65    ///
66    /// Returns always error
67    async fn insert_with_expiry(
68        &self,
69        _key: &str,
70        _value: &str,
71        _duration: Duration,
72    ) -> CacheResult<()> {
73        Err(CacheError::Any(
74            "Operation not supported by null cache".into(),
75        ))
76    }
77
78    /// Removes a key-value pair from the cache.
79    ///
80    /// # Errors
81    ///
82    /// Returns always error
83    async fn remove(&self, _key: &str) -> CacheResult<()> {
84        Err(CacheError::Any(
85            "Operation not supported by null cache".into(),
86        ))
87    }
88
89    /// Clears all key-value pairs from the cache.
90    ///
91    /// # Errors
92    ///
93    /// Returns always error
94    async fn clear(&self) -> CacheResult<()> {
95        Err(CacheError::Any(
96            "Operation not supported by null cache".into(),
97        ))
98    }
99}