pub struct RedisSessionStore { /* private fields */ }
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
impl RedisSessionStore
Sourcepub fn builder(connection_string: impl Into<String>) -> RedisSessionStoreBuilder
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.
Sourcepub fn builder_pooled(pool: impl Into<Pool>) -> RedisSessionStoreBuilder
Available on crate feature redis-pool
only.
pub fn builder_pooled(pool: impl Into<Pool>) -> RedisSessionStoreBuilder
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.
Sourcepub async fn new(
connection_string: impl Into<String>,
) -> Result<RedisSessionStore, Error>
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.
Sourcepub async fn new_pooled(pool: impl Into<Pool>) -> Result<RedisSessionStore>
Available on crate feature redis-pool
only.
pub async fn new_pooled(pool: impl Into<Pool>) -> Result<RedisSessionStore>
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
impl Clone for RedisSessionStore
Source§fn clone(&self) -> RedisSessionStore
fn clone(&self) -> RedisSessionStore
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more