pub struct SqliteSessionStore { /* private fields */ }Expand description
sqlx sqlite session store for async-sessions
use async_sqlx_session::SqliteSessionStore;
use async_session::{Session, SessionStore};
use std::time::Duration;
let store = SqliteSessionStore::new("sqlite::memory:").await?;
store.migrate().await?;
store.spawn_cleanup_task(Duration::from_secs(60 * 60));
let mut session = Session::new();
session.insert("key", vec![1,2,3]);
let cookie_value = store.store_session(session).await?.unwrap();
let session = store.load_session(cookie_value).await?.unwrap();
assert_eq!(session.get::<Vec<i8>>("key").unwrap(), vec![1,2,3]);Implementations§
Source§impl SqliteSessionStore
impl SqliteSessionStore
Sourcepub fn from_client(client: SqlitePool) -> Self
pub fn from_client(client: SqlitePool) -> Self
constructs a new SqliteSessionStore from an existing
sqlx::SqlitePool. the default table name for this session
store will be “async_sessions”. To override this, chain this
with with_table_name.
let pool = sqlx::SqlitePool::connect("sqlite::memory:").await.unwrap();
let store = SqliteSessionStore::from_client(pool)
.with_table_name("custom_table_name");
store.migrate().await;Sourcepub async fn new(database_url: &str) -> Result<Self>
pub async fn new(database_url: &str) -> Result<Self>
Constructs a new SqliteSessionStore from a sqlite: database url. note
that this documentation uses the special :memory: sqlite
database for convenient testing, but a real application would
use a path like sqlite:///path/to/database.db. The default
table name for this session store will be “async_sessions”. To
override this, either chain with
with_table_name or
use
new_with_table_name
let store = SqliteSessionStore::new("sqlite::memory:").await?;
store.migrate().await;Sourcepub async fn new_with_table_name(
database_url: &str,
table_name: &str,
) -> Result<Self>
pub async fn new_with_table_name( database_url: &str, table_name: &str, ) -> Result<Self>
constructs a new SqliteSessionStore from a sqlite: database url. the
default table name for this session store will be
“async_sessions”. To override this, either chain with
with_table_name or
use
new_with_table_name
let store = SqliteSessionStore::new_with_table_name("sqlite::memory:", "custom_table_name").await?;
store.migrate().await;Sourcepub fn with_table_name(self, table_name: impl AsRef<str>) -> Self
pub fn with_table_name(self, table_name: impl AsRef<str>) -> Self
Chainable method to add a custom table name. This will panic
if the table name is not [a-zA-Z0-9_-]+.
let store = SqliteSessionStore::new("sqlite::memory:").await?
.with_table_name("custom_name");
store.migrate().await;let store = SqliteSessionStore::new("sqlite::memory:").await?
.with_table_name("johnny (); drop users;");Sourcepub async fn migrate(&self) -> Result<()>
pub async fn migrate(&self) -> Result<()>
Creates a session table if it does not already exist. If it does, this will noop, making it safe to call repeatedly on store initialization. In the future, this may make exactly-once modifications to the schema of the session table on breaking releases.
let store = SqliteSessionStore::new("sqlite::memory:").await?;
assert!(store.count().await.is_err());
store.migrate().await?;
store.store_session(Session::new()).await?;
store.migrate().await?; // calling it a second time is safe
assert_eq!(store.count().await?, 1);Sourcepub fn spawn_cleanup_task(&self, period: Duration) -> JoinHandle<()>
pub fn spawn_cleanup_task(&self, period: Duration) -> JoinHandle<()>
Spawns an async_std::task that clears out stale (expired) sessions on a periodic basis. Only available with the async_std feature enabled.
let store = SqliteSessionStore::new("sqlite::memory:").await?;
store.migrate().await?;
store.spawn_cleanup_task(Duration::from_secs(1));
let mut session = Session::new();
session.expire_in(Duration::from_secs(0));
store.store_session(session).await?;
assert_eq!(store.count().await?, 1);
async_std::task::sleep(Duration::from_secs(2)).await;
assert_eq!(store.count().await?, 0);Sourcepub async fn cleanup(&self) -> Result<()>
pub async fn cleanup(&self) -> Result<()>
Performs a one-time cleanup task that clears out stale (expired) sessions. You may want to call this from cron.
let store = SqliteSessionStore::new("sqlite::memory:").await?;
store.migrate().await?;
let mut session = Session::new();
session.set_expiry(Utc::now() - Duration::seconds(5));
store.store_session(session).await?;
assert_eq!(store.count().await?, 1);
store.cleanup().await?;
assert_eq!(store.count().await?, 0);Sourcepub async fn count(&self) -> Result<i32>
pub async fn count(&self) -> Result<i32>
retrieves the number of sessions currently stored, including expired sessions
let store = SqliteSessionStore::new("sqlite::memory:").await?;
store.migrate().await?;
assert_eq!(store.count().await?, 0);
store.store_session(Session::new()).await?;
assert_eq!(store.count().await?, 1);Trait Implementations§
Source§impl Clone for SqliteSessionStore
impl Clone for SqliteSessionStore
Source§fn clone(&self) -> SqliteSessionStore
fn clone(&self) -> SqliteSessionStore
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SqliteSessionStore
impl Debug for SqliteSessionStore
Source§impl SessionStore for SqliteSessionStore
impl SessionStore for SqliteSessionStore
Source§fn load_session<'life0, 'async_trait>(
&'life0 self,
cookie_value: String,
) -> Pin<Box<dyn Future<Output = Result<Option<Session>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn load_session<'life0, 'async_trait>(
&'life0 self,
cookie_value: String,
) -> Pin<Box<dyn Future<Output = Result<Option<Session>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Source§fn store_session<'life0, 'async_trait>(
&'life0 self,
session: Session,
) -> Pin<Box<dyn Future<Output = Result<Option<String>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn store_session<'life0, 'async_trait>(
&'life0 self,
session: Session,
) -> Pin<Box<dyn Future<Output = Result<Option<String>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Auto Trait Implementations§
impl Freeze for SqliteSessionStore
impl !RefUnwindSafe for SqliteSessionStore
impl Send for SqliteSessionStore
impl Sync for SqliteSessionStore
impl Unpin for SqliteSessionStore
impl UnsafeUnpin for SqliteSessionStore
impl !UnwindSafe for SqliteSessionStore
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more