redis增加泛型支持

This commit is contained in:
李运家 2024-11-06 10:26:20 +08:00
parent d66e45b7c3
commit 56c5728a74

View File

@ -1,6 +1,7 @@
use std::fmt::Display;
use deadpool_redis::{Config, Connection, Pool}; use deadpool_redis::{Config, Connection, Pool};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use redis::{Cmd, ExpireOption}; use redis::{Cmd, ExpireOption, FromRedisValue, ToRedisArgs};
use crate::{ use crate::{
config, config,
@ -25,7 +26,7 @@ impl Default for RedisConnManager {
} }
impl RedisConnManager { impl RedisConnManager {
pub async fn set(&self, key: &str, value: &str) -> ResResult<()> { pub async fn set<V: ToRedisArgs + Display>(&self, key: &str, value: &V) -> ResResult<()> {
let mut conn = self.get_conn().await?; let mut conn = self.get_conn().await?;
match Cmd::set(key, value).exec_async(&mut conn).await { match Cmd::set(key, value).exec_async(&mut conn).await {
Ok(_) => Ok(()), Ok(_) => Ok(()),
@ -36,7 +37,7 @@ impl RedisConnManager {
} }
} }
pub async fn setexp(&self, key: &str, value: &str, seconds: u64) -> ResResult<()> { pub async fn setexp<V: ToRedisArgs + Display>(&self, key: &str, value: &V, seconds: u64) -> ResResult<()> {
let mut conn = self.get_conn().await?; let mut conn = self.get_conn().await?;
match Cmd::set_ex(key, value, seconds).exec_async(&mut conn).await { match Cmd::set_ex(key, value, seconds).exec_async(&mut conn).await {
Ok(_) => Ok(()), Ok(_) => Ok(()),
@ -53,9 +54,9 @@ impl RedisConnManager {
} }
} }
pub async fn get(&self, key: &str) -> ResResult<String> { pub async fn get<V: FromRedisValue>(&self, key: &str) -> ResResult<V> {
let mut conn = self.get_conn().await?; let mut conn = self.get_conn().await?;
match Cmd::get(key).query_async::<String>(&mut conn).await { match Cmd::get(key).query_async::<V>(&mut conn).await {
Ok(result) => Ok(result), Ok(result) => Ok(result),
Err(err) => { Err(err) => {
tracing::error!("redis get key:{} error:{}", key, 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<V: ToRedisArgs + Display>(&self, key: &str, field: &V, value: &str) -> ResResult<()> {
let mut conn = self.get_conn().await?; let mut conn = self.get_conn().await?;
match Cmd::hset(key, field, value) match Cmd::hset(key, field, value)
.query_async::<i64>(&mut conn) .query_async::<i64>(&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<V: ToRedisArgs + Display>(&self, key: &str, field: &str, value: &V, expire: i64) -> ResResult<()> {
let mut conn = self.get_conn().await?; let mut conn = self.get_conn().await?;
match Cmd::hset(key, field, value) match Cmd::hset(key, field, value)
.query_async::<i64>(&mut conn) .query_async::<i64>(&mut conn)
@ -132,9 +133,9 @@ impl RedisConnManager {
} }
} }
pub async fn hget(&self, key: &str, field: &str) -> ResResult<String> { pub async fn hget<V: FromRedisValue>(&self, key: &str, field: &str) -> ResResult<V> {
let mut conn = self.get_conn().await?; let mut conn = self.get_conn().await?;
match Cmd::hget(key, field).query_async::<String>(&mut conn).await { match Cmd::hget(key, field).query_async::<V>(&mut conn).await {
Ok(result) => Ok(result), Ok(result) => Ok(result),
Err(err) => { Err(err) => {
tracing::error!("redis hget key:{} field:{} error:{}", key, field, 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<Vec<(String, String)>> { pub async fn hgetall<V: FromRedisValue>(&self, key: &str) -> ResResult<Vec<(V, V)>> {
let mut conn = self.get_conn().await?; let mut conn = self.get_conn().await?;
match Cmd::hgetall(key) match Cmd::hgetall(key)
.query_async::<Vec<(String, String)>>(&mut conn) .query_async::<Vec<(V, V)>>(&mut conn)
.await .await
{ {
Ok(result) => Ok(result), Ok(result) => Ok(result),
@ -168,9 +169,9 @@ impl RedisConnManager {
} }
} }
pub async fn hkeys(&self, key: &str) -> ResResult<Vec<String>> { pub async fn hkeys<V: FromRedisValue>(&self, key: &str) -> ResResult<Vec<V>> {
let mut conn = self.get_conn().await?; let mut conn = self.get_conn().await?;
match Cmd::hkeys(key).query_async::<Vec<String>>(&mut conn).await { match Cmd::hkeys(key).query_async::<Vec<V>>(&mut conn).await {
Ok(result) => Ok(result), Ok(result) => Ok(result),
Err(err) => { Err(err) => {
tracing::error!("redis hkeys key:{} error:{}", key, err); tracing::error!("redis hkeys key:{} error:{}", key, err);
@ -179,9 +180,9 @@ impl RedisConnManager {
} }
} }
pub async fn hvals(&self, key: &str) -> ResResult<Vec<String>> { pub async fn hvals<V: FromRedisValue>(&self, key: &str) -> ResResult<Vec<V>> {
let mut conn = self.get_conn().await?; let mut conn = self.get_conn().await?;
match Cmd::hvals(key).query_async::<Vec<String>>(&mut conn).await { match Cmd::hvals(key).query_async::<Vec<V>>(&mut conn).await {
Ok(result) => Ok(result), Ok(result) => Ok(result),
Err(err) => { Err(err) => {
tracing::error!("redis hvals key:{} error:{}", key, err); tracing::error!("redis hvals key:{} error:{}", key, err);