Skip to content

Commit

Permalink
Merge pull request #284 from JakubOnderka/redis-cluster-async
Browse files Browse the repository at this point in the history
Use async for Redis cluster
  • Loading branch information
icewind1991 authored Jun 21, 2023
2 parents c4b7540 + 633fbdc commit 3847d55
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
9 changes: 2 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
rust-version = "1.60"

[dependencies]
redis = { version = "0.23.0", features = ["tokio-comp", "aio", "cluster"] }
redis = { version = "0.23.0", default-features = false, features = ["tokio-comp", "aio", "cluster", "cluster-async"] }
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96"
thiserror = "1.0.40"
Expand Down
25 changes: 13 additions & 12 deletions src/redis.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::error::ConfigError;
use crate::Result;
use redis::aio::{Connection, PubSub};
use redis::cluster::{ClusterClient, ClusterConnection};
use redis::{AsyncCommands, Client, Commands, ConnectionInfo, RedisError};
use tokio::task::block_in_place;
use redis::cluster::ClusterClient;
use redis::cluster_async::ClusterConnection;
use redis::{AsyncCommands, Client, ConnectionInfo, RedisError};

pub struct Redis {
config: Vec<ConnectionInfo>,
Expand Down Expand Up @@ -32,8 +32,9 @@ impl Redis {
RedisConnection::Async(client)
}
config => {
let client =
block_in_place(|| ClusterClient::new(config.to_vec())?.get_connection())?;
let client = ClusterClient::new(config.to_vec())?
.get_async_connection()
.await?;
RedisConnection::Cluster(client)
}
};
Expand All @@ -47,32 +48,32 @@ pub enum RedisConnection {
}

impl RedisConnection {
pub async fn del(&mut self, key: &str) -> Result<(), redis::RedisError> {
pub async fn del(&mut self, key: &str) -> Result<(), RedisError> {
match self {
RedisConnection::Async(client) => {
client.del::<_, ()>(key).await?;
client.del(key).await?;
}
RedisConnection::Cluster(client) => {
block_in_place(|| client.del::<_, ()>(key))?;
client.del(key).await?;
}
}
Ok(())
}

pub async fn get(&mut self, key: &str) -> Result<String> {
Ok(match self {
RedisConnection::Async(client) => client.get::<_, String>(key).await?,
RedisConnection::Cluster(client) => block_in_place(|| client.get::<_, String>(key))?,
RedisConnection::Async(client) => client.get(key).await?,
RedisConnection::Cluster(client) => client.get(key).await?,
})
}

pub async fn set(&mut self, key: &str, value: &str) -> Result<()> {
match self {
RedisConnection::Async(client) => {
client.set::<_, _, ()>(key, value).await?;
client.set(key, value).await?;
}
RedisConnection::Cluster(client) => {
block_in_place(|| client.set::<_, _, ()>(key, value))?;
client.set(key, value).await?;
}
}
Ok(())
Expand Down

0 comments on commit 3847d55

Please sign in to comment.