pub struct RedisSessionStore { /* private fields */ }Expand description
Session store backed by a Redis server.
Sessions are stored as Redis strings keyed by rws:sess:{id} and given
a Redis TTL via SET … EX. Expired sessions are removed automatically by
Redis — no purge_expired sweep is needed.
Cloning is cheap — all clones share the same underlying TCP connection
(one persistent connection per RedisSessionStore instance).
§Connection
Specify the server address as host:port (e.g. "127.0.0.1:6379").
Pass Some("password") for Redis servers that require AUTH.
Use RedisSessionStore::from_env to read connection details from
RWS_REDIS_HOST, RWS_REDIS_PORT, and RWS_REDIS_PASSWORD.
§Example
use rust_web_server::session::RedisSessionStore;
let store = RedisSessionStore::new("127.0.0.1:6379", None, 3600);
let mut sess = store.create().expect("create session");
sess.set("user_id", "42");
store.save(&sess).expect("save session");
let loaded = store.load(&sess.id).expect("load session").unwrap();
assert_eq!(Some("42"), loaded.get("user_id"));Implementations§
Source§impl RedisSessionStore
impl RedisSessionStore
Sourcepub fn new(
addr: impl Into<String>,
password: Option<String>,
ttl_secs: u64,
) -> Self
pub fn new( addr: impl Into<String>, password: Option<String>, ttl_secs: u64, ) -> Self
Create a store that connects to addr (e.g. "127.0.0.1:6379").
password is passed to Redis AUTH if Some.
Sourcepub fn from_env() -> Self
pub fn from_env() -> Self
Build a store from environment variables:
RWS_REDIS_HOST(default127.0.0.1)RWS_REDIS_PORT(default6379)RWS_REDIS_PASSWORD(optional)RWS_REDIS_TTL_SECS(default3600)
Sourcepub fn create_with_id(&self, id: String) -> Result<Session>
pub fn create_with_id(&self, id: String) -> Result<Session>
Create a new empty session with a caller-supplied ID and persist it.
Sourcepub fn load(&self, id: &str) -> Result<Option<Session>>
pub fn load(&self, id: &str) -> Result<Option<Session>>
Load a session by ID. Returns None if unknown or expired.
Sourcepub fn save(&self, session: &Session) -> Result<()>
pub fn save(&self, session: &Session) -> Result<()>
Persist a session’s data back to Redis, resetting the TTL.
Sourcepub fn purge_expired(&self)
pub fn purge_expired(&self)
No-op — Redis expiry removes sessions automatically.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for RedisSessionStore
impl RefUnwindSafe for RedisSessionStore
impl Send for RedisSessionStore
impl Sync for RedisSessionStore
impl Unpin for RedisSessionStore
impl UnsafeUnpin for RedisSessionStore
impl UnwindSafe for RedisSessionStore
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