Crates.io | redis_pool |
lib.rs | redis_pool |
version | 0.6.0 |
source | src |
created_at | 2023-09-07 15:30:54.564383 |
updated_at | 2024-09-16 00:14:15.63559 |
description | Library to Provide a redis client and cluster client pools. |
homepage | |
repository | https://github.com/AscendingCreations/RedisPool |
max_upload_size | |
id | 966339 |
size | 51,901 |
This project is licensed under either Apache License, Version 2.0, zlib License, or MIT License, at your option.
If you need help with this library or have suggestions please go to our Discord Group
RedisPool uses tokio
runtime.
# Cargo.toml
[dependencies]
redis_pool = "0.6.0"
cluster
: Enables Redis Cluster Client and connections.
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.acquire().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()
}
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
.