Skip to main content

Session

Struct Session 

Source
pub struct Session { /* private fields */ }
Expand description

会话实例 — 对齐 PHP think\Session

每个 Session 实例绑定一个 session_id,通过 SessionStore 后端 读写数据。实例本身不缓存数据,每次操作都直接访问后端(对齐 PHP 行为)。

§用法

use sz_rust_state_facade::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

Source

pub fn new( session_id: impl Into<String>, store: impl SessionStore + 'static, ) -> Session

创建新的会话实例

§参数
  • session_id:会话唯一标识(通常由中间件从 Cookie 中提取或新生成)
  • store:存储后端(如 MemorySessionStore)
Source

pub fn with_shared_store( session_id: impl Into<String>, store: Arc<dyn SessionStore>, ) -> Session

Arc<dyn SessionStore> 创建会话(共享后端实例)

Source

pub fn with_prefix(self, prefix: impl Into<String>) -> Session

设置键前缀(对齐 PHP think\Session::prefix($prefix)

设置后,所有 set/get/delete/has 操作都会自动添加此前缀。 用于在同一 session_id 下实现命名空间隔离。

Source

pub fn session_id(&self) -> &str

获取当前会话 ID

Source

pub fn set(&self, name: &str, value: Value)

设置会话数据(对齐 PHP Session::set($name, $value)

§PHP 对齐
public function set(string $name, $value): void
Source

pub fn get(&self, name: &str) -> Option<Value>

获取会话数据(对齐 PHP Session::get($name = null, $default = null)

§返回
  • Some(value):键存在
  • None:键不存在
Source

pub fn get_with_default(&self, name: &str, default: Value) -> Value

获取会话数据,键不存在时返回默认值

对齐 PHP Session::get($name, $default)

Source

pub fn has(&self, name: &str) -> bool

检查会话数据是否存在(对齐 PHP Session::has($name)

Source

pub fn delete(&self, name: &str) -> Option<Value>

删除会话数据(对齐 PHP Session::delete($name)

返回被删除的值(对齐 PHP 行为:删除不存在的键返回 null)。

Source

pub fn clear(&self)

清空当前会话的所有数据(对齐 PHP Session::clear()

注意:此操作会删除整个 session_id 对应的数据,包括 flash 数据。

Source

pub fn flash(&self, name: &str, value: Value)

设置 flash 数据(对齐 PHP Session::flash($name, $value)

Flash 数据在设置后,于下次调用 Session::clear_flash 时被清除。 通常由中间件在请求结束时调用 clear_flash

§实现说明

使用 __flash__: 前缀标记 flash 数据,与普通数据隔离存储。

Source

pub fn get_flash(&self, name: &str) -> Option<Value>

获取 flash 数据

Session::get 类似,但自动添加 flash 前缀。

Source

pub fn clear_flash(&self)

清除所有 flash 数据(对齐 PHP think\session\Driver::clearFlashData()

应在请求结束时由中间件调用,以实现 flash 数据的“一次性“语义。

Source

pub fn flush(&self)

清空所有数据并销毁 session(对齐 PHP Session::flush()

Session::clear 的区别:flush 同时清除 flash 数据, 行为上等价于 clear(因为 clear 直接销毁整个 session)。

Source

pub fn all(&self) -> HashMap<String, Value>

获取当前会话的所有数据(不含 flash 数据)

对齐 PHP Session::all()

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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