pub struct CacheSessionStore { /* private fields */ }Expand description
Sessions in a cache.
The usual production choice, because it gets the two things a session needs from infrastructure you already have: shared between instances, so a load balancer can send a user anywhere, and expiring by itself, so nothing has to sweep old rows.
Works over any Cache — including a
sharded Redis Cluster (rainier_cache::RedisCache), where each session lands
on the node that owns its slot. Every operation here touches exactly one key,
which is what makes that safe.
use rainier_cache::MemoryCache;
use rainier_session::CacheSessionStore;
use std::sync::Arc;
let store = CacheSessionStore::new(Arc::new(MemoryCache::new()));§A cache can evict
This is the trade against a database store. A cache under memory pressure evicts whatever it likes, and an evicted session logs somebody out mid-checkout.
For Redis, maxmemory-policy volatile-lru at least confines eviction to keys
with a TTL; allkeys-lru will take sessions ahead of things you would rather
lose. If being logged out is genuinely unacceptable, the database store is
the one that does not evict.
Implementations§
Source§impl CacheSessionStore
impl CacheSessionStore
Sourcepub fn with_lifetime(self, lifetime: ChronoDuration) -> Self
pub fn with_lifetime(self, lifetime: ChronoDuration) -> Self
Sessions expiring after lifetime.
Trait Implementations§
Source§impl Debug for CacheSessionStore
impl Debug for CacheSessionStore
Source§impl SessionStore for CacheSessionStore
impl SessionStore for CacheSessionStore
Source§fn gc(&self) -> BoxFuture<'_, Result<u64>>
fn gc(&self) -> BoxFuture<'_, Result<u64>>
Nothing to do: the cache expires its own keys.
Returns zero rather than pretending, and deliberately does not
delegate to Cache::flush — that would empty the whole cache, which is
not what “collect expired sessions” means.
Source§fn read<'a>(&'a self, id: &'a str) -> BoxFuture<'a, Result<Option<SessionData>>>
fn read<'a>(&'a self, id: &'a str) -> BoxFuture<'a, Result<Option<SessionData>>>
None if it is unknown or expired.