diff --git a/library/src/cache/redis_cache.rs b/library/src/cache/redis_cache.rs index 297d1d2..ac81075 100644 --- a/library/src/cache/redis_cache.rs +++ b/library/src/cache/redis_cache.rs @@ -1,6 +1,7 @@ +use std::fmt::Display; use deadpool_redis::{Config, Connection, Pool}; use lazy_static::lazy_static; -use redis::{Cmd, ExpireOption}; +use redis::{Cmd, ExpireOption, FromRedisValue, ToRedisArgs}; use crate::{ config, @@ -25,7 +26,7 @@ impl Default for RedisConnManager { } impl RedisConnManager { - pub async fn set(&self, key: &str, value: &str) -> ResResult<()> { + pub async fn set(&self, key: &str, value: &V) -> ResResult<()> { let mut conn = self.get_conn().await?; match Cmd::set(key, value).exec_async(&mut conn).await { Ok(_) => Ok(()), @@ -36,7 +37,7 @@ impl RedisConnManager { } } - pub async fn setexp(&self, key: &str, value: &str, seconds: u64) -> ResResult<()> { + pub async fn setexp(&self, key: &str, value: &V, seconds: u64) -> ResResult<()> { let mut conn = self.get_conn().await?; match Cmd::set_ex(key, value, seconds).exec_async(&mut conn).await { Ok(_) => Ok(()), @@ -53,9 +54,9 @@ impl RedisConnManager { } } - pub async fn get(&self, key: &str) -> ResResult { + pub async fn get(&self, key: &str) -> ResResult { let mut conn = self.get_conn().await?; - match Cmd::get(key).query_async::(&mut conn).await { + match Cmd::get(key).query_async::(&mut conn).await { Ok(result) => Ok(result), Err(err) => { tracing::error!("redis get key:{} error:{}", key, err); @@ -75,7 +76,7 @@ impl RedisConnManager { } } - pub async fn hset(&self, key: &str, field: &str, value: &str) -> ResResult<()> { + pub async fn hset(&self, key: &str, field: &V, value: &str) -> ResResult<()> { let mut conn = self.get_conn().await?; match Cmd::hset(key, field, value) .query_async::(&mut conn) @@ -95,7 +96,7 @@ impl RedisConnManager { } } - pub async fn hset_ex(&self, key: &str, field: &str, value: &str, expire: i64) -> ResResult<()> { + pub async fn hset_ex(&self, key: &str, field: &str, value: &V, expire: i64) -> ResResult<()> { let mut conn = self.get_conn().await?; match Cmd::hset(key, field, value) .query_async::(&mut conn) @@ -132,9 +133,9 @@ impl RedisConnManager { } } - pub async fn hget(&self, key: &str, field: &str) -> ResResult { + pub async fn hget(&self, key: &str, field: &str) -> ResResult { let mut conn = self.get_conn().await?; - match Cmd::hget(key, field).query_async::(&mut conn).await { + match Cmd::hget(key, field).query_async::(&mut conn).await { Ok(result) => Ok(result), Err(err) => { tracing::error!("redis hget key:{} field:{} error:{}", key, field, err); @@ -154,10 +155,10 @@ impl RedisConnManager { } } - pub async fn hgetall(&self, key: &str) -> ResResult> { + pub async fn hgetall(&self, key: &str) -> ResResult> { let mut conn = self.get_conn().await?; match Cmd::hgetall(key) - .query_async::>(&mut conn) + .query_async::>(&mut conn) .await { Ok(result) => Ok(result), @@ -168,9 +169,9 @@ impl RedisConnManager { } } - pub async fn hkeys(&self, key: &str) -> ResResult> { + pub async fn hkeys(&self, key: &str) -> ResResult> { let mut conn = self.get_conn().await?; - match Cmd::hkeys(key).query_async::>(&mut conn).await { + match Cmd::hkeys(key).query_async::>(&mut conn).await { Ok(result) => Ok(result), Err(err) => { tracing::error!("redis hkeys key:{} error:{}", key, err); @@ -179,9 +180,9 @@ impl RedisConnManager { } } - pub async fn hvals(&self, key: &str) -> ResResult> { + pub async fn hvals(&self, key: &str) -> ResResult> { let mut conn = self.get_conn().await?; - match Cmd::hvals(key).query_async::>(&mut conn).await { + match Cmd::hvals(key).query_async::>(&mut conn).await { Ok(result) => Ok(result), Err(err) => { tracing::error!("redis hvals key:{} error:{}", key, err);