use std::collections::HashMap;
use std::sync::Mutex;
use super::SessionBackend;
#[derive(Default)]
pub struct InMemorySessionBackend {
data: Mutex<HashMap<String, String>>,
}
impl InMemorySessionBackend {
pub fn new() -> Self {
Self::default()
}
}
impl SessionBackend for InMemorySessionBackend {
fn load(&self, key: &str) -> Option<String> {
self.data.lock().unwrap().get(key).cloned()
}
fn save(&self, key: &str, value: &str) -> Result<(), Box<dyn std::error::Error>> {
self.data
.lock()
.unwrap()
.insert(key.to_string(), value.to_string());
Ok(())
}
fn clear(&self, key: &str) {
self.data.lock().unwrap().remove(key);
}
}