pub struct DbSessionStore { /* private fields */ }model-mysql or model-postgres or model-sqlite only.Expand description
Session store backed by a relational database via the model-layer
DbPool.
Sessions survive process restarts and are visible to every process that connects to the same database, making this suitable for multi-instance deployments and zero-downtime restarts.
The first call to DbSessionStore::new creates the rws_sessions table
if it does not already exist:
CREATE TABLE IF NOT EXISTS rws_sessions (
id TEXT PRIMARY KEY,
data TEXT NOT NULL DEFAULT '',
expires_at INTEGER NOT NULL
)Session data is serialized as a URL-encoded string
(key1=val1&key2=val2). Expired sessions are not removed
automatically — call purge_expired
periodically.
All methods are async fn — model-* features imply the http2 feature
which provides a tokio runtime.
§Example
use rust_web_server::model::DbPool;
use rust_web_server::session::DbSessionStore;
let pool = DbPool::memory().await?;
let store = DbSessionStore::new(pool, 3600).await?;
let mut sess = store.create().await?;
sess.set("user_id", "42");
store.save(&sess).await?;
let loaded = store.load(&sess.id).await?.unwrap();
assert_eq!(Some("42"), loaded.get("user_id"));Implementations§
Source§impl DbSessionStore
impl DbSessionStore
Sourcepub async fn new(pool: DbPool, ttl_secs: u64) -> Result<Self, DbError>
pub async fn new(pool: DbPool, ttl_secs: u64) -> Result<Self, DbError>
Open (or reuse) a DbSessionStore backed by pool.
Creates the rws_sessions table on the first call if it is absent.
Returns Err if the DDL fails.
Sourcepub async fn create(&self) -> Result<Session, DbError>
pub async fn create(&self) -> Result<Session, DbError>
Create a new empty session and persist it immediately.
Sourcepub async fn create_with_id(&self, id: String) -> Result<Session, DbError>
pub async fn create_with_id(&self, id: String) -> Result<Session, DbError>
Create a new empty session with a caller-supplied ID and persist it.
Sourcepub async fn load(&self, id: &str) -> Result<Option<Session>, DbError>
pub async fn load(&self, id: &str) -> Result<Option<Session>, DbError>
Load a session by ID. Returns None if unknown or expired.
Sourcepub async fn save(&self, session: &Session) -> Result<(), DbError>
pub async fn save(&self, session: &Session) -> Result<(), DbError>
Persist a session’s data back to the store.
Sourcepub async fn purge_expired(&self) -> Result<(), DbError>
pub async fn purge_expired(&self) -> Result<(), DbError>
Delete all sessions whose TTL has elapsed.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for DbSessionStore
impl !UnwindSafe for DbSessionStore
impl Freeze for DbSessionStore
impl Send for DbSessionStore
impl Sync for DbSessionStore
impl Unpin for DbSessionStore
impl UnsafeUnpin for DbSessionStore
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
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