Skip to main content

esh_cache/
memory.rs

1//! In-memory TTL cache.
2
3use async_trait::async_trait;
4use dashmap::DashMap;
5use esh_core::{Cache, CacheEntry, CacheKey, ConfigValue, Result};
6use std::time::Duration;
7
8/// Process-local memory cache.
9#[derive(Debug, Default)]
10pub struct MemoryCache {
11    inner: DashMap<String, CacheEntry>,
12    default_ttl: Duration,
13}
14
15impl MemoryCache {
16    /// Create with a default TTL used by helpers.
17    #[must_use]
18    pub fn ttl(ttl: Duration) -> Self {
19        Self {
20            inner: DashMap::new(),
21            default_ttl: ttl,
22        }
23    }
24
25    /// Default 300s TTL.
26    #[must_use]
27    pub fn new() -> Self {
28        Self::ttl(Duration::from_secs(300))
29    }
30
31    fn map_key(key: &CacheKey) -> String {
32        match &key.namespace {
33            Some(ns) => format!("{ns}::{}", key.key),
34            None => key.key.clone(),
35        }
36    }
37
38    /// Default TTL.
39    #[must_use]
40    pub fn default_ttl(&self) -> Duration {
41        self.default_ttl
42    }
43}
44
45#[async_trait]
46impl Cache for MemoryCache {
47    async fn get(&self, key: &CacheKey) -> Result<Option<ConfigValue>> {
48        let k = Self::map_key(key);
49        if let Some(entry) = self.inner.get(&k) {
50            if entry.is_fresh() {
51                return Ok(Some(entry.value.clone()));
52            }
53            drop(entry);
54            self.inner.remove(&k);
55        }
56        Ok(None)
57    }
58
59    async fn put(&self, key: &CacheKey, value: ConfigValue, ttl: Duration) -> Result<()> {
60        self.inner
61            .insert(Self::map_key(key), CacheEntry::with_ttl(value, ttl));
62        Ok(())
63    }
64
65    async fn invalidate(&self, key: &CacheKey) -> Result<()> {
66        self.inner.remove(&Self::map_key(key));
67        Ok(())
68    }
69
70    async fn clear(&self) -> Result<()> {
71        self.inner.clear();
72        Ok(())
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79    use esh_core::ConfigValue;
80    use esh_test_support::{run_cache_conformance, CacheConformanceCfg};
81    use std::time::Duration;
82
83    #[tokio::test]
84    async fn memory_roundtrip() {
85        let cache = MemoryCache::ttl(Duration::from_secs(60));
86        let key = CacheKey::new("a");
87        cache
88            .put(&key, ConfigValue::secret("v"), Duration::from_secs(60))
89            .await
90            .unwrap();
91        let got = cache.get(&key).await.unwrap().unwrap();
92        assert_eq!(got.as_str(), Some("v"));
93    }
94
95    #[tokio::test]
96    async fn memory_conformance() {
97        let cache = MemoryCache::new();
98        run_cache_conformance(
99            &cache,
100            &CacheConformanceCfg {
101                supports_clear: true,
102                short_ttl: Duration::from_millis(40),
103                test_expiry: true,
104            },
105        )
106        .await;
107    }
108}