pub struct Session(/* private fields */);Expand description
The high-level interface you use to read and modify session data.
Session is an extractor, it can also be obtained with
UserSession::get_session from an HttpRequest, WebRequest or
RequestHead. All handles for the same request share the same state.
Without session middleware, the state is not persisted between requests.
Once the session is purged, further handler mutations
through set, remove, clear,
or renew are ignored.
use ntex_session::Session;
use ntex::web::*;
use serde_json::error::Error as JsonError;
fn index(session: Session) -> Result<&'static str, JsonError> {
// access session data
if let Some(count) = session.get::<i32>("counter")? {
session.set("counter", count + 1)?;
} else {
session.set("counter", 1)?;
}
Ok("Welcome!")
}Implementations§
Source§impl Session
impl Session
Sourcepub fn get<T: DeserializeOwned>(
&self,
key: &str,
) -> Result<Option<T>, JsonError>
pub fn get<T: DeserializeOwned>( &self, key: &str, ) -> Result<Option<T>, JsonError>
Get a value from the session.
Returns Ok(None) if the key is not set.
§Errors
Returns an error if the stored JSON cannot be deserialized into T.
Sourcepub fn set<T: Serialize>(&self, key: &str, value: T) -> Result<(), JsonError>
pub fn set<T: Serialize>(&self, key: &str, value: T) -> Result<(), JsonError>
Set a value in the session.
Marks the session changed before serializing. Ignored if the session is purged, in which case the value is not serialized.
§Errors
Returns an error if the value cannot be serialized. The existing value is preserved, but the session remains marked changed.
Sourcepub fn remove(&self, key: &str)
pub fn remove(&self, key: &str)
Remove a value from the session.
Ignored if the session is purged.
Sourcepub fn renew(&self)
pub fn renew(&self)
Marks the session for reissue while preserving its existing state.
The backend chooses the renewal mechanism; CookieSession rewrites
the cookie without rotating its signing or encryption key. Ignored if
the session is purged.
Sourcepub fn set_session<St>(
data: impl Iterator<Item = (String, String)>,
req: &WebRequest<St>,
)
pub fn set_session<St>( data: impl Iterator<Item = (String, String)>, req: &WebRequest<St>, )
Load session state into the request.
Used by session backends before calling the inner service. Extends the
current state, overwriting matching keys without changing the status.
Values must be JSON encoded, as returned by Session::get_changes;
this method does not validate them.
Sourcepub fn get_changes(
res: &mut WebResponse,
) -> (SessionStatus, Option<impl Iterator<Item = (String, String)> + use<>>)
pub fn get_changes( res: &mut WebResponse, ) -> (SessionStatus, Option<impl Iterator<Item = (String, String)> + use<>>)
Drains the state from the response’s request and returns its current status.
Used by session backends after the inner service returns. The state is
moved out of all shared handles, but the status is not reset. Returns
None for the state only if no session was created; otherwise returns
Some, even for empty or unchanged state.
Trait Implementations§
Source§impl<St> FromRequest<St> for Session
Extractor implementation for Session type.
impl<St> FromRequest<St> for Session
Extractor implementation for Session type.
use ntex_session::Session;
use serde_json::error::Error as JsonError;
fn index(session: Session) -> Result<&'static str, JsonError> {
// access session data
if let Some(count) = session.get::<i32>("counter")? {
session.set("counter", count + 1)?;
} else {
session.set("counter", 1)?;
}
Ok("Welcome!")
}