Skip to main content

CacheSessionStore

Struct CacheSessionStore 

Source
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

Source

pub fn new(cache: Arc<dyn Cache>) -> Self

A store over cache, with two-hour sessions.

Source

pub fn with_lifetime(self, lifetime: ChronoDuration) -> Self

Sessions expiring after lifetime.

Source

pub fn cache(&self) -> &Arc<dyn Cache> ⓘ

The cache underneath, for sharing it.

Source

pub fn lifetime(&self) -> Duration

How long a session lives without being touched.

Trait Implementations§

Source§

impl Debug for CacheSessionStore

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl SessionStore for CacheSessionStore

Source§

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 name(&self) -> &str

A label for diagnostics — "memory", "database", "cache", "cookie".
Source§

fn read<'a>(&'a self, id: &'a str) -> BoxFuture<'a, Result<Option<SessionData>>>

The data for this id, or None if it is unknown or expired.
Source§

fn write<'a>( &'a self, id: &'a str, data: &'a SessionData, ) -> BoxFuture<'a, Result<()>>

Write the data back, refreshing its expiry.
Source§

fn destroy<'a>(&'a self, id: &'a str) -> BoxFuture<'a, Result<()>>

Remove a session.
Source§

fn is_client_side(&self) -> bool

Whether the session travels in the cookie rather than being stored here. Read more
Source§

fn encode(&self, id: &str, data: &SessionData) -> Result<String>

The cookie value carrying this session. Read more
Source§

fn decode(&self, value: &str) -> Result<(String, Option<SessionData>)>

Recover a session id — and, for a client-side store, its data — from a cookie value. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Event for T
where T: Send + Sync + 'static,

Source§

fn event_name() -> &'static str
where Self: Sized,

A human-readable name, used by wildcard listeners and log output. Always the Rust type path; see the trait docs.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more