Skip to main content

simple_agents_cache/
noop.rs

1//! No-op cache implementation for testing and disabling caching.
2
3use async_trait::async_trait;
4use simple_agent_type::cache::Cache;
5use simple_agent_type::error::Result;
6use std::time::Duration;
7
8/// A cache that doesn't actually cache anything.
9///
10/// Useful for:
11/// - Testing without caching
12/// - Disabling caching in production
13/// - Debugging cache-related issues
14///
15/// # Example
16/// ```no_run
17/// use simple_agents_cache::NoOpCache;
18/// use simple_agent_type::cache::Cache;
19/// use std::time::Duration;
20///
21/// # async fn example() {
22/// let cache = NoOpCache;
23///
24/// cache.set("key1", b"value1".to_vec(), Duration::from_secs(60)).await.unwrap();
25/// let value = cache.get("key1").await.unwrap();
26/// assert_eq!(value, None); // NoOpCache always returns None
27/// # }
28/// ```
29#[derive(Debug, Clone, Copy, Default)]
30pub struct NoOpCache;
31
32#[async_trait]
33impl Cache for NoOpCache {
34    async fn get(&self, _key: &str) -> Result<Option<Vec<u8>>> {
35        Ok(None)
36    }
37
38    async fn set(&self, _key: &str, _value: Vec<u8>, _ttl: Duration) -> Result<()> {
39        Ok(())
40    }
41
42    async fn delete(&self, _key: &str) -> Result<()> {
43        Ok(())
44    }
45
46    async fn clear(&self) -> Result<()> {
47        Ok(())
48    }
49
50    fn is_enabled(&self) -> bool {
51        false
52    }
53
54    fn name(&self) -> &str {
55        "noop"
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[tokio::test]
64    async fn test_noop_always_returns_none() {
65        let cache = NoOpCache;
66
67        cache
68            .set("key1", b"value1".to_vec(), Duration::from_secs(60))
69            .await
70            .unwrap();
71        let value = cache.get("key1").await.unwrap();
72        assert_eq!(value, None);
73    }
74
75    #[tokio::test]
76    async fn test_noop_is_disabled() {
77        let cache = NoOpCache;
78        assert!(!cache.is_enabled());
79    }
80
81    #[tokio::test]
82    async fn test_noop_name() {
83        let cache = NoOpCache;
84        assert_eq!(cache.name(), "noop");
85    }
86
87    #[tokio::test]
88    async fn test_noop_delete_does_nothing() {
89        let cache = NoOpCache;
90        cache.delete("key1").await.unwrap();
91        // Should not panic or error
92    }
93
94    #[tokio::test]
95    async fn test_noop_clear_does_nothing() {
96        let cache = NoOpCache;
97        cache.clear().await.unwrap();
98        // Should not panic or error
99    }
100}