use crate::error::Result;
use crate::storage::StorageBackend;
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
#[derive(Debug, Clone, Default)]
pub struct MemoryStorage {
data: Arc<RwLock<HashMap<String, Vec<u8>>>>,
}
impl MemoryStorage {
pub fn new() -> Self {
Self {
data: Arc::new(RwLock::new(HashMap::new())),
}
}
}
#[async_trait]
impl StorageBackend for MemoryStorage {
async fn store(&self, key: &str, value: &[u8]) -> Result<()> {
let mut data = self.data.write().await;
data.insert(key.to_string(), value.to_vec());
Ok(())
}
async fn load(&self, key: &str) -> Result<Option<Vec<u8>>> {
let data = self.data.read().await;
Ok(data.get(key).cloned())
}
async fn delete(&self, key: &str) -> Result<()> {
let mut data = self.data.write().await;
data.remove(key);
Ok(())
}
async fn list(&self, prefix: &str) -> Result<Vec<String>> {
let data = self.data.read().await;
Ok(data
.keys()
.filter(|k| k.starts_with(prefix))
.cloned()
.collect())
}
}