sigstore_cache/
noop.rs

1//! No-op cache implementation (caching disabled)
2
3use std::time::Duration;
4
5use crate::{CacheAdapter, CacheKey};
6
7/// A no-op cache that doesn't store anything
8///
9/// This is useful when caching needs to be disabled, for testing,
10/// or for environments where caching is not desired.
11///
12/// # Example
13///
14/// ```
15/// use sigstore_cache::{NoCache, CacheAdapter, CacheKey};
16/// use std::time::Duration;
17///
18/// # async fn example() -> Result<(), sigstore_cache::Error> {
19/// let cache = NoCache;
20///
21/// // Set does nothing
22/// cache.set(CacheKey::RekorPublicKey, b"data", Duration::from_secs(3600)).await?;
23///
24/// // Get always returns None
25/// assert!(cache.get(CacheKey::RekorPublicKey).await?.is_none());
26/// # Ok(())
27/// # }
28/// ```
29#[derive(Debug, Clone, Copy, Default)]
30pub struct NoCache;
31
32impl CacheAdapter for NoCache {
33    fn get(&self, _key: CacheKey) -> crate::CacheGetFuture<'_> {
34        Box::pin(async { Ok(None) })
35    }
36
37    fn set(&self, _key: CacheKey, _value: &[u8], _ttl: Duration) -> crate::CacheOpFuture<'_> {
38        Box::pin(async { Ok(()) })
39    }
40
41    fn remove(&self, _key: CacheKey) -> crate::CacheOpFuture<'_> {
42        Box::pin(async { Ok(()) })
43    }
44
45    fn clear(&self) -> crate::CacheOpFuture<'_> {
46        Box::pin(async { Ok(()) })
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[tokio::test]
55    async fn test_noop_cache() {
56        let cache = NoCache;
57
58        // Set does nothing
59        cache
60            .set(CacheKey::RekorPublicKey, b"data", Duration::from_secs(3600))
61            .await
62            .unwrap();
63
64        // Get always returns None
65        assert!(cache.get(CacheKey::RekorPublicKey).await.unwrap().is_none());
66
67        // Remove and clear are no-ops
68        cache.remove(CacheKey::RekorPublicKey).await.unwrap();
69        cache.clear().await.unwrap();
70    }
71}