pub struct RedisClient { /* private fields */ }Expand description
Redis 客户端
提供 Redis 数据库操作的统一接口,支持连接池管理
Implementations§
Source§impl RedisClient
impl RedisClient
Sourcepub async fn connect(url: impl Into<String>) -> Result<Self>
pub async fn connect(url: impl Into<String>) -> Result<Self>
连接到 Redis 服务器
§参数
url: Redis 连接 URL,格式为redis://host:port或redis://host:port/db
§返回
Ok(RedisClient): 连接成功Err(DbError): 连接失败
§示例
use yang_db::RedisClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = RedisClient::connect("redis://127.0.0.1:6379").await?;
Ok(())
}Sourcepub async fn connect_with_config(
url: impl Into<String>,
config: RedisConfig,
) -> Result<Self>
pub async fn connect_with_config( url: impl Into<String>, config: RedisConfig, ) -> Result<Self>
使用自定义配置连接到 Redis 服务器
§参数
url: Redis 连接 URLconfig: Redis 配置
§返回
Ok(RedisClient): 连接成功Err(DbError): 连接失败
§示例
use yang_db::{RedisClient, RedisConfig};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = RedisConfig::new(20, 10, 15, true);
let client = RedisClient::connect_with_config("redis://127.0.0.1:6379", config).await?;
Ok(())
}Sourcepub async fn execute(&self, cmd: &Cmd) -> Result<RedisValue>
pub async fn execute(&self, cmd: &Cmd) -> Result<RedisValue>
执行 Redis 命令
§参数
cmd: Redis 命令
§返回
Ok(RedisValue): 命令执行成功Err(DbError): 命令执行失败
§示例
use yang_db::RedisClient;
use redis::cmd;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = RedisClient::connect("redis://127.0.0.1:6379").await?;
let mut cmd = cmd("SET");
cmd.arg("key").arg("value");
let result = client.execute(&cmd).await?;
Ok(())
}Sourcepub async fn setex(
&self,
key: impl Into<String>,
seconds: i64,
value: impl Into<String>,
) -> Result<()>
pub async fn setex( &self, key: impl Into<String>, seconds: i64, value: impl Into<String>, ) -> Result<()>
Sourcepub async fn setnx(
&self,
key: impl Into<String>,
value: impl Into<String>,
) -> Result<bool>
pub async fn setnx( &self, key: impl Into<String>, value: impl Into<String>, ) -> Result<bool>
Sourcepub async fn getset(
&self,
key: impl Into<String>,
value: impl Into<String>,
) -> Result<Option<String>>
pub async fn getset( &self, key: impl Into<String>, value: impl Into<String>, ) -> Result<Option<String>>
Sourcepub async fn append(
&self,
key: impl Into<String>,
value: impl Into<String>,
) -> Result<i64>
pub async fn append( &self, key: impl Into<String>, value: impl Into<String>, ) -> Result<i64>
Sourcepub async fn hset(
&self,
key: impl Into<String>,
field: impl Into<String>,
value: impl Into<String>,
) -> Result<bool>
pub async fn hset( &self, key: impl Into<String>, field: impl Into<String>, value: impl Into<String>, ) -> Result<bool>
Sourcepub async fn hget(
&self,
key: impl Into<String>,
field: impl Into<String>,
) -> Result<Option<String>>
pub async fn hget( &self, key: impl Into<String>, field: impl Into<String>, ) -> Result<Option<String>>
Sourcepub async fn hexists(
&self,
key: impl Into<String>,
field: impl Into<String>,
) -> Result<bool>
pub async fn hexists( &self, key: impl Into<String>, field: impl Into<String>, ) -> Result<bool>
Sourcepub async fn hmset(
&self,
key: impl Into<String>,
fields: &[(String, String)],
) -> Result<()>
pub async fn hmset( &self, key: impl Into<String>, fields: &[(String, String)], ) -> Result<()>
HMSET - 批量设置哈希表的多个字段
Sourcepub async fn hmget(
&self,
key: impl Into<String>,
fields: &[String],
) -> Result<Vec<Option<String>>>
pub async fn hmget( &self, key: impl Into<String>, fields: &[String], ) -> Result<Vec<Option<String>>>
Sourcepub async fn hincrby(
&self,
key: impl Into<String>,
field: impl Into<String>,
increment: i64,
) -> Result<i64>
pub async fn hincrby( &self, key: impl Into<String>, field: impl Into<String>, increment: i64, ) -> Result<i64>
HINCRBY - 将哈希表字段的整数值增加指定数量
Sourcepub async fn hincrbyfloat(
&self,
key: impl Into<String>,
field: impl Into<String>,
increment: f64,
) -> Result<f64>
pub async fn hincrbyfloat( &self, key: impl Into<String>, field: impl Into<String>, increment: f64, ) -> Result<f64>
HINCRBYFLOAT - 将哈希表字段的浮点数值增加指定数量
Sourcepub async fn lrange(
&self,
key: impl Into<String>,
start: i64,
stop: i64,
) -> Result<Vec<String>>
pub async fn lrange( &self, key: impl Into<String>, start: i64, stop: i64, ) -> Result<Vec<String>>
Sourcepub async fn lset(
&self,
key: impl Into<String>,
index: i64,
value: impl Into<String>,
) -> Result<()>
pub async fn lset( &self, key: impl Into<String>, index: i64, value: impl Into<String>, ) -> Result<()>
LSET - 设置列表指定索引的元素值
Sourcepub async fn ltrim(
&self,
key: impl Into<String>,
start: i64,
stop: i64,
) -> Result<()>
pub async fn ltrim( &self, key: impl Into<String>, start: i64, stop: i64, ) -> Result<()>
LTRIM - 修剪列表,仅保留指定范围内的元素
Sourcepub async fn smembers(&self, key: impl Into<String>) -> Result<Vec<String>>
pub async fn smembers(&self, key: impl Into<String>) -> Result<Vec<String>>
SMEMBERS - 获取集合的所有成员
Sourcepub async fn sismember(
&self,
key: impl Into<String>,
member: impl Into<String>,
) -> Result<bool>
pub async fn sismember( &self, key: impl Into<String>, member: impl Into<String>, ) -> Result<bool>
Sourcepub async fn zscore(
&self,
key: impl Into<String>,
member: impl Into<String>,
) -> Result<Option<f64>>
pub async fn zscore( &self, key: impl Into<String>, member: impl Into<String>, ) -> Result<Option<f64>>
Sourcepub async fn zrange(
&self,
key: impl Into<String>,
start: i64,
stop: i64,
) -> Result<Vec<String>>
pub async fn zrange( &self, key: impl Into<String>, start: i64, stop: i64, ) -> Result<Vec<String>>
Sourcepub async fn zrangebyscore(
&self,
key: impl Into<String>,
min: f64,
max: f64,
) -> Result<Vec<String>>
pub async fn zrangebyscore( &self, key: impl Into<String>, min: f64, max: f64, ) -> Result<Vec<String>>
Sourcepub async fn zcount(
&self,
key: impl Into<String>,
min: f64,
max: f64,
) -> Result<i64>
pub async fn zcount( &self, key: impl Into<String>, min: f64, max: f64, ) -> Result<i64>
ZCOUNT - 计算分数范围内的成员数量
Sourcepub async fn zincrby(
&self,
key: impl Into<String>,
increment: f64,
member: impl Into<String>,
) -> Result<f64>
pub async fn zincrby( &self, key: impl Into<String>, increment: f64, member: impl Into<String>, ) -> Result<f64>
Trait Implementations§
Source§impl Clone for RedisClient
impl Clone for RedisClient
Source§fn clone(&self) -> RedisClient
fn clone(&self) -> RedisClient
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for RedisClient
impl !RefUnwindSafe for RedisClient
impl Send for RedisClient
impl Sync for RedisClient
impl Unpin for RedisClient
impl UnsafeUnpin for RedisClient
impl !UnwindSafe for RedisClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more