actix-web uses redis connection pool

Viewed 374

I am using redis in actix-web 4

actix-web = "4"
redis = { version = "0.21", features = ["r2d2", "cluster", "connection-manager", "tokio-comp", "tokio-native-tls-comp"] }

I created redis client in main


#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let redis_client = redis::Client::open("redis://127.0.0.1:6379/").expect("err");
    let serve = HttpServer::new(move || {
        App::new()
            .app_data(web::Data::new(redis_client.clone()))
    });
}

Then get this redis client in the route

pub async fn list(
    query: web::Query<ListRequest>,
    redis_client: web::Data<redis::Client>,
) -> HttpResult {
    let mut con = redis_client.get_tokio_connection().await.map_err(hje)?;
    let _: () = con.set("my_key", 42).await.map_err(hje)?;
}

The above code works fine, but I would like to know how to use r2d2 to create a pool of redis connections instead of a single connection

I found in the documentation that r2d2 can be supported, but I don't know how to use it, can you help me?

1 Answers
Related