Struct axum_session::SessionStore
source · pub struct SessionStore<T>where
T: DatabasePool + Clone + Debug + Sync + Send + 'static,{
pub client: Option<T>,
pub(crate) inner: Arc<DashMap<String, SessionData>>,
pub(crate) keys: Arc<DashMap<String, SessionKey>>,
pub config: SessionConfig,
pub(crate) timers: Arc<RwLock<SessionTimers>>,
pub(crate) filter: Arc<RwLock<CountingBloomFilter>>,
}Expand description
Fields§
§client: Option<T>Client for the database.
inner: Arc<DashMap<String, SessionData>>locked Hashmap containing UserID and their session data.
keys: Arc<DashMap<String, SessionKey>>locked Hashmap containing KeyID and their Key data.
config: SessionConfigSession Configuration.
timers: Arc<RwLock<SessionTimers>>Session Timers used for Clearing Memory and Database.
filter: Arc<RwLock<CountingBloomFilter>>Filter used to keep track of what uuid’s exist.
Implementations§
source§impl<T> SessionStore<T>where
T: DatabasePool + Clone + Debug + Sync + Send + 'static,
impl<T> SessionStore<T>where T: DatabasePool + Clone + Debug + Sync + Send + 'static,
sourcepub async fn new(
client: Option<T>,
config: SessionConfig
) -> Result<Self, SessionError>
pub async fn new( client: Option<T>, config: SessionConfig ) -> Result<Self, SessionError>
Constructs a New SessionStore and Creates the Database Table
needed for the Session if it does not exist if client is not None.
Examples
use axum_session::{SessionNullPool, SessionConfig, SessionStore};
let config = SessionConfig::default();
let session_store = SessionStore::<SessionNullPool>::new(None, config).await.unwrap();sourcepub(crate) async fn create_filter(
client: &Option<T>,
config: &SessionConfig
) -> Result<CountingBloomFilter, SessionError>
pub(crate) async fn create_filter( client: &Option<T>, config: &SessionConfig ) -> Result<CountingBloomFilter, SessionError>
Used to create and Fill the Filter.
sourcepub fn is_persistent(&self) -> bool
pub fn is_persistent(&self) -> bool
Checks if the database is in persistent mode.
Returns true if client is Some().
Examples
use axum_session::{SessionNullPool, SessionConfig, SessionStore};
let config = SessionConfig::default();
let session_store = SessionStore::<SessionNullPool>::new(None, config).await.unwrap();
let is_persistent = session_store.is_persistent();sourcepub async fn cleanup(&self) -> Result<Vec<String>, SessionError>
pub async fn cleanup(&self) -> Result<Vec<String>, SessionError>
Cleans Expired sessions from the Database based on Utc::now().
If client is None it will return Ok(()).
Errors
- [‘SessionError::Sqlx’] is returned if database connection has failed or user does not have permissions.
Examples
use axum_session::{SessionNullPool, SessionConfig, SessionStore};
let config = SessionConfig::default();
let session_store = SessionStore::<SessionNullPool>::new(None, config).await.unwrap();
async {
let _ = session_store.cleanup().await.unwrap();
};sourcepub async fn count(&self) -> Result<i64, SessionError>
pub async fn count(&self) -> Result<i64, SessionError>
Returns count of existing sessions within database.
If client is None it will return Ok(0).
Errors
- [‘SessionError::Sqlx’] is returned if database connection has failed or user does not have permissions.
Examples
use axum_session::{SessionNullPool, SessionConfig, SessionStore};
let config = SessionConfig::default();
let session_store = SessionStore::<SessionNullPool>::new(None, config).await.unwrap();
async {
let count = session_store.count().await.unwrap();
};sourcepub(crate) async fn load_session(
&self,
cookie_value: String
) -> Result<Option<SessionData>, SessionError>
pub(crate) async fn load_session( &self, cookie_value: String ) -> Result<Option<SessionData>, SessionError>
private internal function that loads a session’s data from the database using a UUID string.
If client is None it will return Ok(None).
Errors
- [‘SessionError::Sqlx’] is returned if database connection has failed or user does not have permissions.
- [‘SessionError::SerdeJson’] is returned if it failed to deserialize the sessions data.
Examples
use axum_session::{SessionNullPool, SessionConfig, SessionStore};
use uuid::Uuid;
let config = SessionConfig::default();
let session_store = SessionStore::<SessionNullPool>::new(None, config).await.unwrap();
let token = Uuid::new_v4();
async {
let session_data = session_store.load_session(token.to_string()).await.unwrap();
};sourcepub(crate) async fn load_key(
&self,
cookie_value: String
) -> Result<Option<SessionKey>, SessionError>
pub(crate) async fn load_key( &self, cookie_value: String ) -> Result<Option<SessionKey>, SessionError>
private internal function that loads an encryption key for the session’s cookie from the database using a UUID string.
If client is None it will return Ok(None).
Errors
- [‘SessionError::Sqlx’] is returned if database connection has failed or user does not have permissions.
Examples
use axum_session::{SessionNullPool, SessionConfig, SessionStore};
use uuid::Uuid;
let config = SessionConfig::default();
let session_store = SessionStore::<SessionNullPool>::new(None, config).await.unwrap();
let token = Uuid::new_v4();
let key = Key::generate();
async {
let session_key = session_store.load_key(token.to_string(), key).await.unwrap();
};sourcepub(crate) async fn store_session(
&self,
session: &SessionData
) -> Result<(), SessionError>
pub(crate) async fn store_session( &self, session: &SessionData ) -> Result<(), SessionError>
private internal function that stores a session’s data to the database.
If client is None it will return Ok(()).
Errors
- [‘SessionError::Sqlx’] is returned if database connection has failed or user does not have permissions.
- [‘SessionError::SerdeJson’] is returned if it failed to serialize the sessions data.
Examples
use axum_session::{SessionNullPool, SessionConfig, SessionStore, SessionData};
use uuid::Uuid;
let config = SessionConfig::default();
let session_store = SessionStore::<SessionNullPool>::new(None, config.clone()).await.unwrap();
let token = Uuid::new_v4();
let session_data = SessionData::new(token, true, &config);
async {
let _ = session_store.store_session(&session_data).await.unwrap();
};sourcepub(crate) async fn store_key(
&self,
key: &SessionKey,
expires: i64
) -> Result<(), SessionError>
pub(crate) async fn store_key( &self, key: &SessionKey, expires: i64 ) -> Result<(), SessionError>
private internal function that stores a keys data to the database as a session.
If client is None it will return Ok(()).
Errors
- [‘SessionError::Sqlx’] is returned if database connection has failed or user does not have permissions.
Examples
use axum_session::{SessionNullPool, SessionConfig, SessionStore, SessionKey};
use uuid::Uuid;
let config = SessionConfig::default();
let session_store = SessionStore::<SessionNullPool>::new(None, config.clone()).await.unwrap();
let token = Uuid::new_v4();
let key = Key::generate();
let session_key = SessionKey::new(token);
async {
let _ = session_store.store_key(&session_key, key).await.unwrap();
};sourcepub async fn clear_store(&self) -> Result<(), SessionError>
pub async fn clear_store(&self) -> Result<(), SessionError>
Deletes all sessions in the database.
If client is None it will return Ok(()).
Errors
- [‘SessionError::Sqlx’] is returned if database connection has failed or user does not have permissions.
Examples
use axum_session::{SessionNullPool, SessionConfig, SessionStore};
use uuid::Uuid;
let config = SessionConfig::default();
let session_store = SessionStore::<SessionNullPool>::new(None, config.clone()).await.unwrap();
async {
let _ = session_store.clear_store().await.unwrap();
};sourcepub async fn clear(&mut self)
pub async fn clear(&mut self)
Deletes all sessions in Memory. This will also Clear those keys from the filter cache if a persistent database does not exist.
Examples
use axum_session::{SessionNullPool, SessionConfig, SessionStore};
use uuid::Uuid;
let config = SessionConfig::default();
let session_store = SessionStore::<SessionNullPool>::new(None, config.clone()).await.unwrap();
async {
let _ = session_store.clear().await.unwrap();
};sourcepub(crate) fn service_session_data(&self, session: &Session<T>) -> bool
pub(crate) fn service_session_data(&self, session: &Session<T>) -> bool
Attempts to load check and clear Data.
If no session is found returns false.
pub(crate) fn renew(&self, id: String)
pub(crate) fn renew_key(&self, id: String)
pub(crate) fn destroy(&self, id: String)
pub(crate) fn set_longterm(&self, id: String, longterm: bool)
pub(crate) fn set_store(&self, id: String, storable: bool)
pub(crate) fn update(&self, id: String)
pub(crate) fn get<N: DeserializeOwned>( &self, id: String, key: &str ) -> Option<N>
pub(crate) fn get_remove<N: DeserializeOwned>( &self, id: String, key: &str ) -> Option<N>
pub(crate) fn set(&self, id: String, key: &str, value: impl Serialize)
pub(crate) fn remove(&self, id: String, key: &str)
pub(crate) fn clear_session_data(&self, id: String)
pub(crate) fn set_session_request(&self, id: String)
pub(crate) fn remove_session_request(&self, id: String)
pub(crate) fn is_session_parallel(&self, id: String) -> bool
pub(crate) async fn count_sessions(&self) -> i64
pub(crate) fn auto_handles_expiry(&self) -> bool
pub(crate) fn verify(&self, id: String) -> Result<(), SessionError>
pub(crate) fn update_database_expires( &self, id: String ) -> Result<(), SessionError>
pub(crate) fn update_memory_expires( &self, id: String ) -> Result<(), SessionError>
pub(crate) async fn force_database_update( &self, id: String ) -> Result<(), SessionError>
pub(crate) fn memory_remove_session( &self, id: String ) -> Result<(), SessionError>
pub(crate) async fn database_remove_session( &self, id: String ) -> Result<(), SessionError>
Trait Implementations§
source§impl<T> Clone for SessionStore<T>where
T: DatabasePool + Clone + Debug + Sync + Send + 'static + Clone,
impl<T> Clone for SessionStore<T>where T: DatabasePool + Clone + Debug + Sync + Send + 'static + Clone,
source§fn clone(&self) -> SessionStore<T>
fn clone(&self) -> SessionStore<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl<T> Debug for SessionStore<T>where
T: DatabasePool + Clone + Debug + Sync + Send + 'static + Debug,
impl<T> Debug for SessionStore<T>where T: DatabasePool + Clone + Debug + Sync + Send + 'static + Debug,
source§impl<T, S> FromRequestParts<S> for SessionStore<T>where
T: DatabasePool + Clone + Debug + Sync + Send + 'static,
S: Send + Sync,
impl<T, S> FromRequestParts<S> for SessionStore<T>where T: DatabasePool + Clone + Debug + Sync + Send + 'static, S: Send + Sync,
§type Rejection = (StatusCode, &'static str)
type Rejection = (StatusCode, &'static str)
Auto Trait Implementations§
impl<T> !RefUnwindSafe for SessionStore<T>
impl<T> Send for SessionStore<T>
impl<T> Sync for SessionStore<T>
impl<T> Unpin for SessionStore<T>where T: Unpin,
impl<T> !UnwindSafe for SessionStore<T>
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<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where Self: Binary,
self to use its Binary implementation when Debug-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where Self: Display,
self to use its Display implementation when
Debug-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where Self: Octal,
self to use its Octal implementation when Debug-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where &'a Self: for<'a> IntoIterator,
source§impl<S, T> FromRequest<S, ViaParts> for Twhere
S: Send + Sync,
T: FromRequestParts<S>,
impl<S, T> FromRequest<S, ViaParts> for Twhere S: Send + Sync, T: FromRequestParts<S>,
§type Rejection = <T as FromRequestParts<S>>::Rejection
type Rejection = <T as FromRequestParts<S>>::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>
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere Self: Borrow<B>, B: 'a + ?Sized, R: 'a,
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> Rwhere Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere Self: AsRef<U>, U: 'a + ?Sized, R: 'a,
self, then passes self.as_ref() into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere Self: AsMut<U>, U: 'a + ?Sized, R: 'a,
self, then passes self.as_mut() into the pipe
function.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
Borrow<B> of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
BorrowMut<B> of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
AsRef<R> view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
AsMut<R> view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere Self: Deref<Target = T>, T: ?Sized,
Deref::Target of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere Self: DerefMut<Target = T> + Deref, T: ?Sized,
Deref::Target of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
.tap_borrow() only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
.tap_borrow_mut() only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
.tap_ref() only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
.tap_ref_mut() only in debug builds, and is erased in release
builds.