Trait tower_sessions::SessionStore
source · pub trait SessionStore: Debug + Send + Sync + 'static {
// Required methods
fn save<'life0, 'life1, 'async_trait>(
&'life0 self,
session_record: &'life1 Record
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait;
fn load<'life0, 'life1, 'async_trait>(
&'life0 self,
session_id: &'life1 Id
) -> Pin<Box<dyn Future<Output = Result<Option<Record>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait;
fn delete<'life0, 'life1, 'async_trait>(
&'life0 self,
session_id: &'life1 Id
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait;
}Expand description
An arbitrary store which houses the session data.
Implementing your own store
This crate is designed such that any arbirary session storage backend can be
supported simply by implemeting the SessionStore trait. While a set of
common stores are provided, should those not meet your needs or otherwise we
lacking, it is encouraged to implement your own store.
For example, we might construct a session store for testing purposes that
wraps HashMap. To do so, we can write a struct that houses this hash map
and then implement SessionStore.
use std::{collections::HashMap, sync::Arc};
use async_trait::async_trait;
use parking_lot::Mutex;
use tower_sessions::{
session::{Id, Record},
session_store, Session, SessionStore,
};
#[derive(Debug, Clone)]
pub struct TestingStore(Arc<Mutex<HashMap<Id, Record>>>);
#[async_trait]
impl SessionStore for TestingStore {
async fn save(&self, record: &Record) -> session_store::Result<()> {
self.0.lock().insert(record.id, record.clone());
Ok(())
}
async fn load(&self, session_id: &Id) -> session_store::Result<Option<Record>> {
Ok(self.0.lock().get(session_id).cloned())
}
async fn delete(&self, session_id: &Id) -> session_store::Result<()> {
self.0.lock().remove(session_id);
Ok(())
}
}Required Methods§
sourcefn save<'life0, 'life1, 'async_trait>(
&'life0 self,
session_record: &'life1 Record
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn save<'life0, 'life1, 'async_trait>(
&'life0 self,
session_record: &'life1 Record
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
A method for saving a session in a store.