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.