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 moreSource§impl SessionStore for RedisSessionStore
impl SessionStore for RedisSessionStore
Source§async fn load(
&self,
session_key: &SessionKey,
) -> Result<Option<SessionState>, LoadError>
async fn load( &self, session_key: &SessionKey, ) -> Result<Option<SessionState>, LoadError>
Source§async fn save(
&self,
session_state: SessionState,
ttl: &Duration,
) -> Result<SessionKey, SaveError>
async fn save( &self, session_state: SessionState, ttl: &Duration, ) -> Result<SessionKey, SaveError>
Source§async fn update(
&self,
session_key: SessionKey,
session_state: SessionState,
ttl: &Duration,
) -> Result<SessionKey, UpdateError>
async fn update( &self, session_key: SessionKey, session_state: SessionState, ttl: &Duration, ) -> Result<SessionKey, UpdateError>
Source§async fn update_ttl(
&self,
session_key: &SessionKey,
ttl: &Duration,
) -> Result<()>
async fn update_ttl( &self, session_key: &SessionKey, ttl: &Duration, ) -> Result<()>
Auto Trait Implementations§
impl Freeze for RedisSessionStore
impl !RefUnwindSafe for RedisSessionStore
impl Send for RedisSessionStore
impl Sync for RedisSessionStore
impl Unpin for RedisSessionStore
impl !UnwindSafe for RedisSessionStore
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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