pub struct RedisSessionStore { /* private fields */ }
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 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();

Implementation notes

RedisSessionStore leverages redis-rs as Redis client.

Implementations§

A fluent API to configure RedisSessionStore. It takes as input the only required input to create a new instance of RedisSessionStore - a connection string for Redis.

Create 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.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Loads the session state associated to a session key.
Persist the session state for a newly created session. Read more
Updates the session state associated to a pre-existing session key.
Updates the TTL of the session state associated to a pre-existing session key.
Deletes a session from the store.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more