Skip to main content

doido_cache/
store.rs

1use doido_core::Result;
2use serde_json::Value;
3use std::sync::Arc;
4
5#[async_trait::async_trait]
6pub trait CacheStore: Send + Sync {
7    async fn get(&self, key: &str) -> Result<Option<Value>>;
8    async fn set(&self, key: &str, value: Value, ttl_secs: Option<u64>) -> Result<()>;
9    async fn delete(&self, key: &str) -> Result<()>;
10    async fn exists(&self, key: &str) -> Result<bool>;
11    async fn increment(&self, key: &str, by: i64) -> Result<i64>;
12    async fn decrement(&self, key: &str, by: i64) -> Result<i64>;
13    async fn clear(&self) -> Result<()>;
14}
15
16/// Lets a type-erased `Arc<dyn CacheStore>` be used wherever a `CacheStore` is
17/// expected — notably so [`crate::NamespacedStore`] can wrap a backend selected
18/// at runtime from config.
19#[async_trait::async_trait]
20impl CacheStore for Arc<dyn CacheStore> {
21    async fn get(&self, key: &str) -> Result<Option<Value>> {
22        (**self).get(key).await
23    }
24    async fn set(&self, key: &str, value: Value, ttl_secs: Option<u64>) -> Result<()> {
25        (**self).set(key, value, ttl_secs).await
26    }
27    async fn delete(&self, key: &str) -> Result<()> {
28        (**self).delete(key).await
29    }
30    async fn exists(&self, key: &str) -> Result<bool> {
31        (**self).exists(key).await
32    }
33    async fn increment(&self, key: &str, by: i64) -> Result<i64> {
34        (**self).increment(key, by).await
35    }
36    async fn decrement(&self, key: &str, by: i64) -> Result<i64> {
37        (**self).decrement(key, by).await
38    }
39    async fn clear(&self) -> Result<()> {
40        (**self).clear().await
41    }
42}