[][src]Struct async_sqlx_session::SqliteSessionStore

pub struct SqliteSessionStore { /* fields omitted */ }

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:%3Amemory:").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

impl SqliteSessionStore[src]

pub fn from_client(client: SqlitePool) -> Self[src]

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::new("sqlite:%3Amemory:").await.unwrap();
let store = SqliteSessionStore::from_client(pool)
    .with_table_name("custom_table_name");
store.migrate().await;

pub async fn new<'_>(database_url: &'_ str) -> Result<Self>[src]

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:%3Amemory:").await?;
store.migrate().await;

pub async fn new_with_table_name<'_, '_>(
    database_url: &'_ str,
    table_name: &'_ str
) -> Result<Self>
[src]

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:%3Amemory:", "custom_table_name").await?;
store.migrate().await;

pub fn with_table_name(self, table_name: impl AsRef<str>) -> Self[src]

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:%3Amemory:").await?
    .with_table_name("custom_name");
store.migrate().await;
This example panics
let store = SqliteSessionStore::new("sqlite:%3Amemory:").await?
    .with_table_name("johnny (); drop users;");

pub async fn migrate<'_>(&'_ self) -> Result<()>[src]

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:%3Amemory:").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);

pub fn spawn_cleanup_task(&self, period: Duration) -> JoinHandle<()>[src]

Spawns an async_std::task that clears out stale (expired) sessions on a periodic basis.

let store = SqliteSessionStore::new("sqlite:%3Amemory:").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);

pub async fn cleanup<'_>(&'_ self) -> Result<()>[src]

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:%3Amemory:").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);

pub async fn count<'_>(&'_ self) -> Result<i32>[src]

retrieves the number of sessions currently stored, including expired sessions

let store = SqliteSessionStore::new("sqlite:%3Amemory:").await?;
store.migrate().await?;
assert_eq!(store.count().await?, 0);
store.store_session(Session::new()).await?;
assert_eq!(store.count().await?, 1);

Trait Implementations

impl Clone for SqliteSessionStore[src]

impl Debug for SqliteSessionStore[src]

impl SessionStore for SqliteSessionStore[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,