1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
//! An arbitrary store which houses the session data.
use std::fmt::Debug;
use async_trait::async_trait;
use crate::session::{Id, Record};
/// Stores must map any errors that might occur during their use to this type.
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Encoding failed with: {0}")]
Encode(String),
#[error("Decoding failed with: {0}")]
Decode(String),
#[error("{0}")]
Backend(String),
}
pub type Result<T> = std::result::Result<T, Error>;
/// 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`.
///
/// ```rust
/// 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(())
/// }
/// }
/// ```
#[async_trait]
pub trait SessionStore: Debug + Send + Sync + 'static {
/// A method for saving a session in a store.
async fn save(&self, session_record: &Record) -> Result<()>;
/// A method for loading a session from a store.
async fn load(&self, session_id: &Id) -> Result<Option<Record>>;
/// A method for deleting a session from a store.
async fn delete(&self, session_id: &Id) -> Result<()>;
}
/// A session store for layered caching.
///
/// Contains both a cache, which acts as a frontend, and a store which acts as a
/// backend. Both cache and store implement `SessionStore`.
///
/// By using a cache, the cost of reads can be greatly reduced as once cached,
/// reads need only interact with the frontend, forgoing the cost of retrieving
/// the session record from the backend.
///
/// # Examples
///
/// ```rust,ignore
/// # tokio_test::block_on(async {
/// use tower_sessions::CachingSessionStore;
/// use tower_sessions_moka_store::MokaStore;
/// use tower_sessions_sqlx_store::{SqlitePool, SqliteStore};
/// let pool = SqlitePool::connect("sqlite::memory:").await.unwrap();
/// let sqlite_store = SqliteStore::new(pool);
/// let moka_store = MokaStore::new(Some(2_000));
/// let caching_store = CachingSessionStore::new(moka_store, sqlite_store);
/// # })
/// ```
#[derive(Debug, Clone)]
pub struct CachingSessionStore<Cache: SessionStore, Store: SessionStore> {
cache: Cache,
store: Store,
}
impl<Cache: SessionStore, Store: SessionStore> CachingSessionStore<Cache, Store> {
/// Create a new `CachingSessionStore`.
pub fn new(cache: Cache, store: Store) -> Self {
Self { cache, store }
}
}
#[async_trait]
impl<Cache, Store> SessionStore for CachingSessionStore<Cache, Store>
where
Cache: SessionStore,
Store: SessionStore,
{
async fn save(&self, record: &Record) -> Result<()> {
let cache_save_fut = self.store.save(record);
let store_save_fut = self.cache.save(record);
futures::try_join!(cache_save_fut, store_save_fut)?;
Ok(())
}
async fn load(&self, session_id: &Id) -> Result<Option<Record>> {
match self.cache.load(session_id).await {
// We found a session in the cache, so let's use it.
Ok(Some(session_record)) => Ok(Some(session_record)),
// We didn't find a session in the cache, so we'll try loading from the backend.
//
// When we find a session in the backend, we'll hydrate our cache with it.
Ok(None) => {
let session_record = self.store.load(session_id).await?;
if let Some(ref session_record) = session_record {
self.cache.save(session_record).await?;
}
Ok(session_record)
}
// Some error occurred with our cache so we'll bubble this up.
Err(err) => Err(err),
}
}
async fn delete(&self, session_id: &Id) -> Result<()> {
let store_delete_fut = self.store.delete(session_id);
let cache_delete_fut = self.cache.delete(session_id);
futures::try_join!(store_delete_fut, cache_delete_fut)?;
Ok(())
}
}
/// A trait providing a deletion method for expired methods and optionally a
/// method that runs indefinitely, deleting expired sessions.
#[async_trait]
pub trait ExpiredDeletion: SessionStore
where
Self: Sized,
{
/// A method for deleting expired sessions from the store.
async fn delete_expired(&self) -> Result<()>;
/// This function will keep running indefinitely, deleting expired rows and
/// then waiting for the specified period before deleting again.
///
/// Generally this will be used as a task, for example via
/// `tokio::task::spawn`.
///
/// # Errors
///
/// This function returns a `Result` that contains an error of type
/// `sqlx::Error` if the deletion operation fails.
///
/// # Examples
///
/// ```rust,no_run,ignore
/// use tower_sessions::session_store::ExpiredDeletion;
/// use tower_sessions_sqlx_store::{sqlx::SqlitePool, SqliteStore};
///
/// # {
/// # tokio_test::block_on(async {
/// let pool = SqlitePool::connect("sqlite::memory:").await.unwrap();
/// let session_store = SqliteStore::new(pool);
///
/// tokio::task::spawn(
/// session_store
/// .clone()
/// .continuously_delete_expired(tokio::time::Duration::from_secs(60)),
/// );
/// # })
/// ```
#[cfg(feature = "deletion-task")]
#[cfg_attr(docsrs, doc(cfg(feature = "deletion-task")))]
async fn continuously_delete_expired(self, period: tokio::time::Duration) -> Result<()> {
let mut interval = tokio::time::interval(period);
loop {
self.delete_expired().await?;
interval.tick().await;
}
}
}