1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use std::collections::HashMap;
use async_trait::async_trait;
use tokio::sync::Mutex;
#[async_trait]
pub trait KvStore: Send + Sync {
async fn get(&self, key: &str) -> Option<String>;
async fn set(&self, key: &str, value: String);
}
#[derive(Debug, Default)]
pub struct MemoryKvStore {
inner: Mutex<HashMap<String, String>>,
}
#[async_trait]
impl KvStore for MemoryKvStore {
async fn get(&self, key: &str) -> Option<String> {
self.inner.lock().await.get(key).cloned()
}
async fn set(&self, key: &str, value: String) {
self.inner.lock().await.insert(key.to_string(), value);
}
}