pub trait SessionStore:
Send
+ Sync
+ 'static {
// Required methods
fn read(
&self,
id: &str,
lifetime: Duration,
) -> impl Future<Output = Result<Option<String>, Error>> + Send;
fn write(
&self,
id: &str,
payload: String,
lifetime: Duration,
identity: &Identity,
write: Write,
) -> impl Future<Output = Result<(), Error>> + Send;
fn destroy(
&self,
id: &str,
) -> impl Future<Output = Result<(), Error>> + Send;
// Provided methods
fn destroy_others(
&self,
user_id: &Value,
except: &str,
) -> impl Future<Output = Result<(), Error>> + Send { ... }
fn collect_garbage(
&self,
lifetime: Duration,
) -> impl Future<Output = Result<(), Error>> + Send { ... }
}Expand description
Keeps session payloads by session ID. Implement it to add a session driver of your own.
The built-in drivers are cookie, file, memory, database, and redis. A
package adds another driver, such as Memcached, by implementing this trait and registering a
named factory with Drivers::extend:
use std::time::Duration;
use laravel::session::{Identity, SessionStore, Write};
struct MemcachedStore { /* a client */ }
impl SessionStore for MemcachedStore {
async fn read(&self, id: &str, lifetime: Duration) -> laravel::Result<Option<String>> {
// get session:{id}...
}
async fn write(
&self,
id: &str,
payload: String,
lifetime: Duration,
_identity: &Identity,
write: Write,
) -> laravel::Result<()> {
// For Write::Existing, `replace` session:{id} so a destroyed session stays destroyed.
// Otherwise, `set` it. Memcached expires it after the lifetime, so there's no garbage
// to collect...
}
async fn destroy(&self, id: &str) -> laravel::Result<()> {
// delete session:{id}...
}
}Payloads arrive serialized, and encrypted when SESSION_ENCRYPT is on. IDs are 40 ASCII letters
and digits, so they’re safe to use in file names and keys.
Required Methods§
Sourcefn read(
&self,
id: &str,
lifetime: Duration,
) -> impl Future<Output = Result<Option<String>, Error>> + Send
fn read( &self, id: &str, lifetime: Duration, ) -> impl Future<Output = Result<Option<String>, Error>> + Send
The payload of the session id. Returns None when the session is missing or has been
idle longer than lifetime.
§Errors
Fails when the store can’t be read. A missing or expired session isn’t an error.
Sourcefn write(
&self,
id: &str,
payload: String,
lifetime: Duration,
identity: &Identity,
write: Write,
) -> impl Future<Output = Result<(), Error>> + Send
fn write( &self, id: &str, payload: String, lifetime: Duration, identity: &Identity, write: Write, ) -> impl Future<Output = Result<(), Error>> + Send
Store a session’s payload. Stores that expire data themselves may use lifetime. Stores
that keep who a session belongs to may use identity, which describes the request that
saves the session.
write is Write::Existing when read found the session at the start of
the request. Another request may have destroyed the session since then, such as by logging
out. If the store can write to a session only while it’s still stored, it should do so, as
the built-in stores do, so the destroyed session stays destroyed. For Write::New, such
as for a new or regenerated ID, the store creates the session or overwrites an expired copy.
§Errors
Fails when the store can’t be written.
Provided Methods§
Sourcefn destroy_others(
&self,
user_id: &Value,
except: &str,
) -> impl Future<Output = Result<(), Error>> + Send
fn destroy_others( &self, user_id: &Value, except: &str, ) -> impl Future<Output = Result<(), Error>> + Send
Remove every session of the user whose ID is user_id, as Identity::user_id gives it,
except the session except.
Laravel calls this method after writing the current session when a user changes or resets their password. The default does nothing, for stores that can’t find sessions by user. Those sessions stay stored, but they can’t log the user in, because they hold the old password’s fingerprint.
§Errors
Fails when the store can’t be written. The request carries on.
Sourcefn collect_garbage(
&self,
lifetime: Duration,
) -> impl Future<Output = Result<(), Error>> + Send
fn collect_garbage( &self, lifetime: Duration, ) -> impl Future<Output = Result<(), Error>> + Send
Remove sessions that have been idle longer than lifetime.
StartSession calls this method on the requests that the session
config’s lottery picks. The default does nothing, for stores that expire data themselves.
§Errors
Fails when the store can’t be cleaned. The request carries on.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".