use crate::{auth::token::TokenData, error::Result, storage::SessionStorage};
use async_trait::async_trait;
use std::collections::HashMap;
use tokio::sync::RwLock;
pub struct MemoryStorage {
data: RwLock<HashMap<String, TokenData>>,
}
impl MemoryStorage {
pub fn new() -> Self {
Self {
data: RwLock::new(HashMap::new()),
}
}
}
impl Default for MemoryStorage {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl SessionStorage for MemoryStorage {
async fn get(&self, key: &str) -> Result<Option<TokenData>> {
Ok(self.data.read().await.get(key).cloned())
}
async fn set(&self, key: &str, token: TokenData) -> Result<()> {
self.data.write().await.insert(key.to_string(), token);
Ok(())
}
async fn delete(&self, key: &str) -> Result<()> {
self.data.write().await.remove(key);
Ok(())
}
}