pub struct ExpiringMemoryStore { /* private fields */ }Expand description
Why? The tower_session MemoryStore by default does not delete entries in time. Which may cause memory leak over long run. Java’s auto session management has no memory leak issue. Rust Axum should not have that issue, too!
The ExpiringMemoryStore automatically deletes object that is expired
The data is stored internally as Arc<RwLock<HashMap<Id, Record>>
The expiry is tracked by the ascending VecDequeue using creation time
Upon create, a background tokio thread is immediately started to delete expired session automatically every 5 seconds.
Upon dropping the ExpiringMemoryStore, the background tokio thread is automatically cancelled.
#Example
use axum::routing::get;
use axum::Router;
use std::net::SocketAddr;
use tower_sessions::{cookie::time::{self, Duration, OffsetDateTime}, Expiry, Session, SessionManagerLayer};
#[tokio::main]
async fn main() {
let session_store = tower_sessions_auto_memory_store::ExpiringMemoryStore::new();
let session_layer = SessionManagerLayer::new(session_store)
.with_secure(false)
.with_expiry(Expiry::OnInactivity(Duration::seconds(1800)));
let app = Router::new()
.route("/debug/test", get(root))
.layer(session_layer);
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
println!("listening on {}", addr);
axum_server::bind(addr)
.serve(app.into_make_service())
.await
.unwrap();
}
async fn root() -> &'static str {
"Hello, World!"
}Implementations§
Trait Implementations§
Source§impl Clone for ExpiringMemoryStore
impl Clone for ExpiringMemoryStore
Source§fn clone(&self) -> ExpiringMemoryStore
fn clone(&self) -> ExpiringMemoryStore
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ExpiringMemoryStore
impl Debug for ExpiringMemoryStore
Source§impl Drop for ExpiringMemoryStore
Upon dropping, the background thread will be killed.
impl Drop for ExpiringMemoryStore
Upon dropping, the background thread will be killed.
Source§impl SessionStore for ExpiringMemoryStore
impl SessionStore for ExpiringMemoryStore
Source§fn save<'life0, 'life1, 'async_trait>(
&'life0 self,
record: &'life1 Record,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn save<'life0, 'life1, 'async_trait>(
&'life0 self,
record: &'life1 Record,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Saves the provided session record to the store. Read more
Source§fn create<'life0, 'life1, 'async_trait>(
&'life0 self,
record: &'life1 mut Record,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn create<'life0, 'life1, 'async_trait>(
&'life0 self,
record: &'life1 mut Record,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Creates a new session in the store with the provided session record. Read more
Source§fn load<'life0, 'life1, 'async_trait>(
&'life0 self,
session_id: &'life1 Id,
) -> Pin<Box<dyn Future<Output = Result<Option<Record>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn load<'life0, 'life1, 'async_trait>(
&'life0 self,
session_id: &'life1 Id,
) -> Pin<Box<dyn Future<Output = Result<Option<Record>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Loads an existing session record from the store using the provided ID. Read more
Auto Trait Implementations§
impl Freeze for ExpiringMemoryStore
impl !RefUnwindSafe for ExpiringMemoryStore
impl Send for ExpiringMemoryStore
impl Sync for ExpiringMemoryStore
impl Unpin for ExpiringMemoryStore
impl !UnwindSafe for ExpiringMemoryStore
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
Mutably borrows from an owned value. Read more