pub struct Session<S: SessionStore = RedisStore> { /* private fields */ }Expand description
A parsed on-demand session store.
The default store is the RedisStore
Implementations§
Source§impl<S> Session<S>where
S: SessionStore,
impl<S> Session<S>where
S: SessionStore,
Sourcepub async fn get<T>(&self, field: &str) -> Result<Option<T>, Error>
pub async fn get<T>(&self, field: &str) -> Result<Option<T>, Error>
Retrieves the value of a field from the session store.
§Example
use ruts::{Session};
use fred::clients::Client;
use serde::Deserialize;
use ruts::store::redis::RedisStore;
#[derive(Clone, Deserialize)]
struct User {
id: i64,
name: String,
}
#[derive(Clone, Deserialize)]
enum Theme {
Light,
Dark,
}
#[derive(Clone, Deserialize)]
struct AppSession {
user: User,
theme: Option<Theme>,
}
async fn some_handler_could_be_axum(session: Session<RedisStore<Client>>) {
session.get::<AppSession>("app").await.unwrap();
}Sourcepub async fn get_all<T>(&self) -> Result<Option<T>, Error>
pub async fn get_all<T>(&self) -> Result<Option<T>, Error>
Retrieves values for all fields from the session store.
Sourcepub async fn insert<T>(&self, field: &str, value: &T) -> Result<bool, Error>
pub async fn insert<T>(&self, field: &str, value: &T) -> Result<bool, Error>
Inserts a value into the session store.
Returns true if the value was successfully inserted.
§Example
use ruts::{Session};
use fred::clients::Client;
use serde::Serialize;
use ruts::store::redis::RedisStore;
#[derive(Serialize)]
struct User {
id: i64,
name: String,
}
#[derive(Serialize)]
enum Theme {
Light,
Dark,
}
#[derive(Serialize)]
struct AppSession {
user: User,
theme: Option<Theme>,
}
async fn some_handler_could_be_axum(session: Session<RedisStore<Client>>) {
let app = AppSession {
user: User {
id: 34895634,
name: String::from("John Doe"),
},
theme: Some(Theme::Dark),
};
session.insert("app", &app).await.unwrap();
}Sourcepub async fn update<T>(&self, field: &str, value: &T) -> Result<bool, Error>
pub async fn update<T>(&self, field: &str, value: &T) -> Result<bool, Error>
Updates a value in the session store.
If the key doesn’t exist, it will be inserted.
Returns true if the value was successfully updated or inserted.
§Example
use ruts::{Session};
use fred::clients::Client;
use serde::Serialize;
use ruts::store::redis::RedisStore;
#[derive(Serialize)]
struct User {
id: i64,
name: String,
}
#[derive(Serialize)]
enum Theme {
Light,
Dark,
}
#[derive(Serialize)]
struct AppSession {
user: User,
theme: Option<Theme>,
}
async fn some_handler_could_be_axum(session: Session<RedisStore<Client>>) {
let app = AppSession {
user: User {
id: 21342365,
name: String::from("Jane Doe"),
},
theme: Some(Theme::Light),
};
let updated = session.update("app", &app).await.unwrap();
}Sourcepub async fn remove(&self, field: &str) -> Result<i8, Error>
pub async fn remove(&self, field: &str) -> Result<i8, Error>
Removes a field along with its value from the session store.
Returns true if the field was successfully removed.
§Example
use ruts::{Session};
use fred::clients::Client;
use ruts::store::redis::RedisStore;
async fn some_handler_could_be_axum(session: Session<RedisStore<Client>>) {
let removed = session.remove("app").await.unwrap();
}Sourcepub async fn delete(&self) -> Result<bool, Error>
pub async fn delete(&self) -> Result<bool, Error>
Deletes the entire session from the store.
Returns true if the session was successfully deleted.
§Example
use ruts::{Session};
use fred::clients::Client;
use ruts::store::redis::RedisStore;
async fn some_handler_could_be_axum(session: Session<RedisStore<Client>>) {
let deleted = session.delete().await.unwrap();
}Sourcepub async fn expire(&self, seconds: i64) -> Result<bool, Error>
pub async fn expire(&self, seconds: i64) -> Result<bool, Error>
Updates the cookie’s max-age and session expiry time in the store.
A value of -1 or 0 immediately expires the session and deletes it.
Returns true if the expiry was successfully updated.
§Example
use ruts::{Session};
use fred::clients::Client;
use ruts::store::redis::RedisStore;
async fn some_handler_could_be_axum(session: Session<RedisStore<Client>>) {
session.expire(30).await.unwrap();
}Sourcepub fn set_expiration(&self, seconds: i64)
pub fn set_expiration(&self, seconds: i64)
Updates the cookie max-age.
Any subsequent call to insert, update or regenerate within this request cycle
will use this value.
NOTE: This does not change the max-age value set in the CookieOptions.
Sourcepub async fn regenerate(&self) -> Result<Option<Id>, Error>
pub async fn regenerate(&self) -> Result<Option<Id>, Error>
Regenerates the session with a new ID.
Returns the new session ID if successful.
§Example
use ruts::{Session};
use fred::clients::Client;
use ruts::store::redis::RedisStore;
async fn some_handler_could_be_axum(session: Session<RedisStore<Client>>) {
let id = session.regenerate().await.unwrap();
}Trait Implementations§
Source§impl<S, T> FromRequestParts<S> for Session<T>
axum extractor for Session.
impl<S, T> FromRequestParts<S> for Session<T>
axum extractor for Session.
Auto Trait Implementations§
impl<S> Freeze for Session<S>
impl<S = RedisStore> !RefUnwindSafe for Session<S>
impl<S> Send for Session<S>
impl<S> Sync for Session<S>
impl<S> Unpin for Session<S>
impl<S = RedisStore> !UnwindSafe for Session<S>
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<S, T> FromRequest<S, ViaParts> for T
impl<S, T> FromRequest<S, ViaParts> for T
Source§type Rejection = <T as FromRequestParts<S>>::Rejection
type Rejection = <T as FromRequestParts<S>>::Rejection
Source§async fn from_request(
req: Request<Body>,
state: &S,
) -> Result<T, <T as FromRequest<S, ViaParts>>::Rejection>
async fn from_request( req: Request<Body>, state: &S, ) -> Result<T, <T as FromRequest<S, ViaParts>>::Rejection>
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