Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
q3356564 committed Sep 14, 2024
1 parent d2004d2 commit 2ea39e4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
8 changes: 4 additions & 4 deletions core/src/services/redis/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,26 +341,26 @@ impl kv::Adapter for Adapter {
}

async fn get(&self, key: &str) -> Result<Option<Buffer>> {
let conn = self.conn().await?;
let mut conn = self.conn().await?;
let result = conn.get(key).await?;
Ok(result)
}

async fn set(&self, key: &str, value: Buffer) -> Result<()> {
let conn = self.conn().await?;
let mut conn = self.conn().await?;
let value = value.to_vec();
conn.set(key, value, self.default_ttl).await?;
Ok(())
}

async fn delete(&self, key: &str) -> Result<()> {
let conn = self.conn().await?;
let mut conn = self.conn().await?;
conn.delete(key).await?;
Ok(())
}

async fn append(&self, key: &str, value: &[u8]) -> Result<()> {
let conn = self.conn().await?;
let mut conn = self.conn().await?;
conn.append(key, value).await?;
Ok(())
}
Expand Down
53 changes: 31 additions & 22 deletions core/src/services/redis/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,42 @@ pub enum RedisConnection {
Cluster(ClusterConnection),
}
impl RedisConnection {
pub async fn get(&self, key: &str) -> crate::Result<Option<Buffer>> {
let result: Option<bytes::Bytes> = match self.clone() {
RedisConnection::Normal(mut conn) => conn.get(key).await.map_err(format_redis_error),
RedisConnection::Cluster(mut conn) => conn.get(key).await.map_err(format_redis_error),
pub async fn get(&mut self, key: &str) -> crate::Result<Option<Buffer>> {
let result: Option<bytes::Bytes> = match self {
RedisConnection::Normal(ref mut conn) => {
conn.get(key).await.map_err(format_redis_error)
}
RedisConnection::Cluster(ref mut conn) => {
conn.get(key).await.map_err(format_redis_error)
}
}?;
Ok(result.map(Buffer::from))
}

pub async fn set(&self, key: &str, value: Vec<u8>, ttl: Option<Duration>) -> crate::Result<()> {
pub async fn set(
&mut self,
key: &str,
value: Vec<u8>,
ttl: Option<Duration>,
) -> crate::Result<()> {
let value = value.to_vec();
if let Some(ttl) = ttl {
match self.clone() {
RedisConnection::Normal(mut conn) => conn
match self {
RedisConnection::Normal(ref mut conn) => conn
.set_ex(key, value, ttl.as_secs())
.await
.map_err(format_redis_error)?,
RedisConnection::Cluster(mut conn) => conn
RedisConnection::Cluster(ref mut conn) => conn
.set_ex(key, value, ttl.as_secs())
.await
.map_err(format_redis_error)?,
}
} else {
match self.clone() {
RedisConnection::Normal(mut conn) => {
match self {
RedisConnection::Normal(ref mut conn) => {
conn.set(key, value).await.map_err(format_redis_error)?
}
RedisConnection::Cluster(mut conn) => {
RedisConnection::Cluster(ref mut conn) => {
conn.set(key, value).await.map_err(format_redis_error)?
}
}
Expand All @@ -72,25 +81,25 @@ impl RedisConnection {
Ok(())
}

pub async fn delete(&self, key: &str) -> crate::Result<()> {
match self.clone() {
RedisConnection::Normal(mut conn) => {
pub async fn delete(&mut self, key: &str) -> crate::Result<()> {
match self {
RedisConnection::Normal(ref mut conn) => {
let _: () = conn.del(key).await.map_err(format_redis_error)?;
}
RedisConnection::Cluster(mut conn) => {
RedisConnection::Cluster(ref mut conn) => {
let _: () = conn.del(key).await.map_err(format_redis_error)?;
}
}

Ok(())
}

pub async fn append(&self, key: &str, value: &[u8]) -> crate::Result<()> {
match self.clone() {
RedisConnection::Normal(mut conn) => {
pub async fn append(&mut self, key: &str, value: &[u8]) -> crate::Result<()> {
match self {
RedisConnection::Normal(ref mut conn) => {
() = conn.append(key, value).await.map_err(format_redis_error)?;
}
RedisConnection::Cluster(mut conn) => {
RedisConnection::Cluster(ref mut conn) => {
() = conn.append(key, value).await.map_err(format_redis_error)?;
}
}
Expand Down Expand Up @@ -127,13 +136,13 @@ impl bb8::ManageConnection for RedisConnectionManager {
}

async fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Self::Error> {
let pong_value = match conn.clone() {
RedisConnection::Normal(mut conn) => conn
let pong_value = match conn {
RedisConnection::Normal(ref mut conn) => conn
.send_packed_command(&redis::cmd("PING"))
.await
.map_err(format_redis_error)?,

RedisConnection::Cluster(mut conn) => conn
RedisConnection::Cluster(ref mut conn) => conn
.req_packed_command(&redis::cmd("PING"))
.await
.map_err(format_redis_error)?,
Expand Down

0 comments on commit 2ea39e4

Please sign in to comment.