pub struct Session { /* private fields */ }Expand description
会话实例 — 对齐 PHP think\Session
每个 Session 实例绑定一个 session_id,通过 SessionStore 后端
读写数据。实例本身不缓存数据,每次操作都直接访问后端(对齐 PHP 行为)。
§用法
use sz_rust_core::session::{Session, MemorySessionStore};
use serde_json::json;
let store = MemorySessionStore::new();
let session = Session::new("session-id-123", store);
session.set("user_id", json!(12345));
assert_eq!(session.get("user_id"), Some(json!(12345)));
assert!(session.has("user_id"));Implementations§
Source§impl Session
impl Session
Sourcepub fn new(
session_id: impl Into<String>,
store: impl SessionStore + 'static,
) -> Self
pub fn new( session_id: impl Into<String>, store: impl SessionStore + 'static, ) -> Self
从 Arc<dyn SessionStore> 创建会话(共享后端实例)
Sourcepub fn with_prefix(self, prefix: impl Into<String>) -> Self
pub fn with_prefix(self, prefix: impl Into<String>) -> Self
设置键前缀(对齐 PHP think\Session::prefix($prefix))
设置后,所有 set/get/delete/has 操作都会自动添加此前缀。
用于在同一 session_id 下实现命名空间隔离。
Sourcepub fn session_id(&self) -> &str
pub fn session_id(&self) -> &str
获取当前会话 ID
Sourcepub fn get_with_default(&self, name: &str, default: Value) -> Value
pub fn get_with_default(&self, name: &str, default: Value) -> Value
获取会话数据,键不存在时返回默认值
对齐 PHP Session::get($name, $default)。
Sourcepub fn delete(&self, name: &str) -> Option<Value>
pub fn delete(&self, name: &str) -> Option<Value>
删除会话数据(对齐 PHP Session::delete($name))
返回被删除的值(对齐 PHP 行为:删除不存在的键返回 null)。
Sourcepub fn clear(&self)
pub fn clear(&self)
清空当前会话的所有数据(对齐 PHP Session::clear())
注意:此操作会删除整个 session_id 对应的数据,包括 flash 数据。
Sourcepub fn flash(&self, name: &str, value: Value)
pub fn flash(&self, name: &str, value: Value)
设置 flash 数据(对齐 PHP Session::flash($name, $value))
Flash 数据在设置后,于下次调用 Session::clear_flash 时被清除。
通常由中间件在请求结束时调用 clear_flash。
§实现说明
使用 __flash__: 前缀标记 flash 数据,与普通数据隔离存储。
Sourcepub fn get_flash(&self, name: &str) -> Option<Value>
pub fn get_flash(&self, name: &str) -> Option<Value>
获取 flash 数据
与 Session::get 类似,但自动添加 flash 前缀。
Sourcepub fn clear_flash(&self)
pub fn clear_flash(&self)
清除所有 flash 数据(对齐 PHP think\session\Driver::clearFlashData())
应在请求结束时由中间件调用,以实现 flash 数据的“一次性“语义。
Sourcepub fn flush(&self)
pub fn flush(&self)
清空所有数据并销毁 session(对齐 PHP Session::flush())
与 Session::clear 的区别:flush 同时清除 flash 数据,
行为上等价于 clear(因为 clear 直接销毁整个 session)。
Auto Trait Implementations§
impl !RefUnwindSafe for Session
impl !UnwindSafe for Session
impl Freeze for Session
impl Send for Session
impl Sync for Session
impl Unpin for Session
impl UnsafeUnpin for Session
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> 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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.