Skip to main content

mocra_core/cacheable/
dag_store.rs

1//! Adapts the multi-level [`CacheService`] into `mocra-dag`'s distributed KV / atomic
2//! [`DagStore`] backend.
3//!
4//! This impl lives here (not in the host) because both `CacheService` (this crate) and
5//! `DagStore` (mocra-dag) are now defined outside the host — the orphan rule requires the
6//! impl to sit in the crate that owns one of them.
7
8use std::time::Duration;
9
10use async_trait::async_trait;
11use mocra_dag::DagStore;
12
13use super::CacheService;
14
15#[async_trait]
16impl DagStore for CacheService {
17    async fn get(&self, key: &str) -> Result<Option<Vec<u8>>, String> {
18        CacheService::get(self, key)
19            .await
20            .map_err(|e| e.to_string())
21    }
22    async fn set(&self, key: &str, value: &[u8], ttl: Option<Duration>) -> Result<(), String> {
23        CacheService::set(self, key, value, ttl)
24            .await
25            .map_err(|e| e.to_string())
26    }
27    async fn del(&self, key: &str) -> Result<(), String> {
28        CacheService::del(self, key)
29            .await
30            .map_err(|e| e.to_string())
31    }
32    async fn incr(&self, key: &str, delta: i64) -> Result<i64, String> {
33        CacheService::incr(self, key, delta)
34            .await
35            .map_err(|e| e.to_string())
36    }
37}