Struct RedisSessionStore

Source
pub struct RedisSessionStore { /* private fields */ }
Available on crate feature redis-session only.
Expand description

Use Redis as session storage backend.

use actix_web::{web, App, HttpServer, HttpResponse, Error};
use actix_session::{SessionMiddleware, storage::RedisSessionStore};
use actix_web::cookie::Key;

// The secret key would usually be read from a configuration file/environment variables.
fn get_secret_key() -> Key {
    // [...]
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let secret_key = get_secret_key();
    let redis_connection_string = "redis://127.0.0.1:6379";
    let store = RedisSessionStore::new(redis_connection_string).await.unwrap();

    HttpServer::new(move ||
            App::new()
            .wrap(SessionMiddleware::new(
                store.clone(),
                secret_key.clone()
            ))
            .default_service(web::to(|| HttpResponse::Ok())))
        .bind(("127.0.0.1", 8080))?
        .run()
        .await
}

§TLS support

Add the redis-rs-tls-session or redis-rs-tls-session-rustls feature flag to enable TLS support. You can then establish a TLS connection to Redis using the rediss:// URL scheme:

use actix_session::{storage::RedisSessionStore};

let redis_connection_string = "rediss://127.0.0.1:6379";
let store = RedisSessionStore::new(redis_connection_string).await.unwrap();

§Pooled Redis Connections

When the redis-pool crate feature is enabled, a pre-existing pool from deadpool_redis can be provided.

use actix_session::storage::RedisSessionStore;
use deadpool_redis::{Config, Runtime};

let redis_cfg = Config::from_url("redis://127.0.0.1:6379");
let redis_pool = redis_cfg.create_pool(Some(Runtime::Tokio1)).unwrap();

let store = RedisSessionStore::new_pooled(redis_pool);

§Implementation notes

RedisSessionStore leverages the redis crate as the underlying Redis client.

Implementations§

Source§

impl RedisSessionStore

Source

pub fn builder(connection_string: impl Into<String>) -> RedisSessionStoreBuilder

Returns a fluent API builder to configure RedisSessionStore.

It takes as input the only required input to create a new instance of RedisSessionStore

  • a connection string for Redis.
Source

pub fn builder_pooled(pool: impl Into<Pool>) -> RedisSessionStoreBuilder

Available on crate feature redis-pool only.

Returns a fluent API builder to configure RedisSessionStore.

It takes as input the only required input to create a new instance of RedisSessionStore

  • a pool object for Redis.
Source

pub async fn new( connection_string: impl Into<String>, ) -> Result<RedisSessionStore, Error>

Creates a new instance of RedisSessionStore using the default configuration.

It takes as input the only required input to create a new instance of RedisSessionStore

  • a connection string for Redis.
Source

pub async fn new_pooled(pool: impl Into<Pool>) -> Result<RedisSessionStore>

Available on crate feature redis-pool only.

Creates a new instance of RedisSessionStore using the default configuration.

It takes as input the only required input to create a new instance of RedisSessionStore

  • a pool object for Redis.

Trait Implementations§

Source§

impl Clone for RedisSessionStore

Source§

fn clone(&self) -> RedisSessionStore

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl SessionStore for RedisSessionStore

Source§

async fn load( &self, session_key: &SessionKey, ) -> Result<Option<HashMap<String, String>>, LoadError>

Loads the session state associated to a session key.
Source§

async fn save( &self, session_state: HashMap<String, String>, ttl: &Duration, ) -> Result<SessionKey, SaveError>

Persist the session state for a newly created session. Read more
Source§

async fn update( &self, session_key: SessionKey, session_state: HashMap<String, String>, ttl: &Duration, ) -> Result<SessionKey, UpdateError>

Updates the session state associated to a pre-existing session key.
Source§

async fn update_ttl( &self, session_key: &SessionKey, ttl: &Duration, ) -> Result<()>

Updates the TTL of the session state associated to a pre-existing session key.
Source§

async fn delete(&self, session_key: &SessionKey) -> Result<(), Error>

Deletes a session from the store.

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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