#redis #pool #redis-cluster #tokio

redis_pool

Library to Provide a redis client and cluster client pools

5 releases (3 breaking)

new 0.4.0 Apr 12, 2024
0.3.0 Dec 21, 2023
0.2.1 Sep 18, 2023
0.1.1 Sep 7, 2023

#879 in Database interfaces

Download history 57/week @ 2023-12-19 22/week @ 2023-12-26 75/week @ 2024-01-02 66/week @ 2024-01-09 34/week @ 2024-01-16 32/week @ 2024-01-23 111/week @ 2024-01-30 63/week @ 2024-02-06 134/week @ 2024-02-13 27/week @ 2024-02-20 162/week @ 2024-02-27 101/week @ 2024-03-05 67/week @ 2024-03-12 73/week @ 2024-03-19 154/week @ 2024-03-26 68/week @ 2024-04-02

382 downloads per month
Used in 2 crates

MIT/Apache

16KB
279 lines

RedisPool

Library to Provide an Redis pool.

crates.io docs.rs Minimum Rust Version

License

This project is licensed under either Apache License, Version 2.0, zlib License, or MIT License, at your option.

Help

If you need help with this library or have suggestions please go to our Discord Group

Install

RedisPool uses tokio runtime.

# Cargo.toml
[dependencies]
redis_pool = "0.4.0"

Cargo Feature Flags

cluster: Enables Redis Cluster Client and connections.

Example

use redis_pool::{RedisPool, SingleRedisPool};
use axum::{Router, routing::get, extract::State};
use tokio::net::TcpListener;

#[tokio::main]
async fn main() {
    let redis_url = "redis://default:YourSecretPassWord@127.0.0.1:6379/0";
    let client = redis::Client::open(redis_url).expect("Error while testing the connection");
    let pool = RedisPool::from(client);

    // build our application with some routes
    let app = Router::new()
        .route("/test", get(test_pool))
        .with_state(pool);

    // run it
    let listener = TcpListener::bind("127.0.0.1:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

async fn test_pool(State(pool): State<SingleRedisPool>) -> String {
    let mut connection = pool.aquire().await.unwrap();
    let _: () = redis::pipe()
            .set(0, "Hello")
            .ignore()
            .query_async(&mut connection)
            .await
            .unwrap();

    redis::cmd("GET").arg(0).query_async(&mut connection).await.unwrap()
}

Running Tests

Docker must be installed because this library utilizes testcontainers to spin up redis intances. Additionally, the images contained in the docker directory need to be built and accessible in your local registry; this can be accomplished by running ./docker/build.sh.

Dependencies

~7–18MB
~238K SLoC