use std::collections::HashMap;
use bytes::Bytes;
use tokio::sync::RwLock;
use tracing::instrument;
#[async_trait::async_trait]
pub trait StoreManager: Send + Sync {
async fn get(&self, key: &str) -> anyhow::Result<Option<Bytes>>;
async fn put(&self, key: &str, value: Bytes) -> anyhow::Result<()>;
async fn del(&self, key: &str) -> anyhow::Result<()>;
}
#[derive(Default)]
pub struct DefaultStore {
store: RwLock<HashMap<String, Bytes>>,
}
#[async_trait::async_trait]
impl StoreManager for DefaultStore {
#[instrument(skip(self))]
async fn get(&self, key: &str) -> anyhow::Result<Option<Bytes>> {
Ok(self.store.read().await.get(key).cloned())
}
#[instrument(skip(self, value))]
async fn put(&self, key: &str, value: Bytes) -> anyhow::Result<()> {
self.store.write().await.insert(key.to_string(), value);
Ok(())
}
#[instrument(skip(self))]
async fn del(&self, key: &str) -> anyhow::Result<()> {
self.store.write().await.remove(key);
Ok(())
}
}