pub trait SessionStore: Clone + Send + Sync + 'static {
    type Error: Error + Send + Sync;

    // Required methods
    fn save<'life0, 'life1, 'async_trait>(
        &'life0 self,
        session: &'life1 Session
    ) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn load<'life0, 'life1, 'async_trait>(
        &'life0 self,
        session_id: &'life1 Id
    ) -> Pin<Box<dyn Future<Output = Result<Option<Session>, Self::Error>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn delete<'life0, 'life1, 'async_trait>(
        &'life0 self,
        session_id: &'life1 Id
    ) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: '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, convert::Infallible, sync::Arc};

use async_trait::async_trait;
use parking_lot::Mutex;
use tower_sessions::{session::Id, Session, SessionStore};

#[derive(Clone)]
pub struct TestingStore(Arc<Mutex<HashMap<Id, Session>>>);

#[async_trait]
impl SessionStore for TestingStore {
    type Error = Infallible;

    async fn save(&self, session: &Session) -> Result<(), Self::Error> {
        self.0.lock().insert(*session.id(), session.clone());
        Ok(())
    }

    async fn load(&self, session_id: &Id) -> Result<Option<Session>, Self::Error> {
        Ok(self.0.lock().get(session_id).cloned())
    }

    async fn delete(&self, session_id: &Id) -> Result<(), Self::Error> {
        self.0.lock().remove(session_id);
        Ok(())
    }
}

Required Associated Types§

source

type Error: Error + Send + Sync

An error that occurs when interacting with the store.

Required Methods§

source

fn save<'life0, 'life1, 'async_trait>( &'life0 self, session: &'life1 Session ) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'async_trait>>where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

A method for saving a session in a store.

source

fn load<'life0, 'life1, 'async_trait>( &'life0 self, session_id: &'life1 Id ) -> Pin<Box<dyn Future<Output = Result<Option<Session>, Self::Error>> + Send + 'async_trait>>where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

A method for loading a session from a store.

source

fn delete<'life0, 'life1, 'async_trait>( &'life0 self, session_id: &'life1 Id ) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'async_trait>>where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

A method for deleting a session from a store.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl SessionStore for MemoryStore

Available on crate feature memory-store only.
source§

impl SessionStore for MokaStore

Available on crate feature moka-store only.
source§

impl SessionStore for MongoDBStore

Available on crate feature mongodb-store only.
§

type Error = MongoDBStoreError

source§

impl SessionStore for MySqlStore

Available on crate features mysql-store and sqlx-store only.
source§

impl SessionStore for PostgresStore

Available on crate features postgres-store and sqlx-store only.
source§

impl SessionStore for RedisStore

Available on crate feature redis-store only.
§

type Error = RedisStoreError

source§

impl SessionStore for SqliteStore

Available on crate features sqlite-store and sqlx-store only.
source§

impl<Cache, Store> SessionStore for CachingSessionStore<Cache, Store>where Cache: SessionStore, Store: SessionStore,

§

type Error = CachingStoreError<Cache, Store>