use crate::error::BaseError;
use std::sync::OnceLock;
use yang_db::redis::{RedisClient, RedisConfig};
static GLOBAL_REDIS: OnceLock<RedisClient> = OnceLock::new();
pub struct GlobalRedis;
impl GlobalRedis {
pub async fn init(url: &str, config: RedisConfig) -> Result<(), BaseError> {
let client = RedisClient::connect_with_config(url, config)
.await
.map_err(|e| BaseError::RedisConnectionFailed(e.to_string()))?;
GLOBAL_REDIS
.set(client)
.map_err(|_| BaseError::RedisAlreadyInitialized)?;
log::info!("全局 Redis 已初始化");
Ok(())
}
pub fn client() -> Result<&'static RedisClient, BaseError> {
GLOBAL_REDIS.get().ok_or(BaseError::RedisNotInitialized)
}
pub async fn set(
key: impl Into<String>,
value: impl Into<String>,
expire_seconds: Option<i64>,
) -> Result<(), BaseError> {
let key_str = key.into();
let value_str = value.into();
if let Some(seconds) = expire_seconds {
Self::client()?
.setex(key_str, seconds, value_str)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
} else {
Self::client()?
.set(key_str, value_str)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
}
pub async fn get(key: impl Into<String>) -> Result<Option<String>, BaseError> {
Self::client()?
.get(key)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn del(keys: &[String]) -> Result<i64, BaseError> {
Self::client()?
.del(keys)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn exists(keys: &[String]) -> Result<i64, BaseError> {
Self::client()?
.exists(keys)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn expire(key: impl Into<String>, seconds: i64) -> Result<bool, BaseError> {
Self::client()?
.expire(key, seconds)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn ttl(key: impl Into<String>) -> Result<i64, BaseError> {
Self::client()?
.ttl(key)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn persist(key: impl Into<String>) -> Result<bool, BaseError> {
Self::client()?
.persist(key)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn keys(pattern: impl Into<String>) -> Result<Vec<String>, BaseError> {
Self::client()?
.keys(pattern)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn hset(
key: impl Into<String>,
field: impl Into<String>,
value: impl Into<String>,
) -> Result<bool, BaseError> {
Self::client()?
.hset(key, field, value)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn hget(
key: impl Into<String>,
field: impl Into<String>,
) -> Result<Option<String>, BaseError> {
Self::client()?
.hget(key, field)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn hdel(key: impl Into<String>, fields: &[String]) -> Result<i64, BaseError> {
Self::client()?
.hdel(key, fields)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn hgetall(key: impl Into<String>) -> Result<Vec<(String, String)>, BaseError> {
Self::client()?
.hgetall(key)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn hexists(
key: impl Into<String>,
field: impl Into<String>,
) -> Result<bool, BaseError> {
Self::client()?
.hexists(key, field)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn hlen(key: impl Into<String>) -> Result<i64, BaseError> {
Self::client()?
.hlen(key)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn lpush(key: impl Into<String>, values: &[String]) -> Result<i64, BaseError> {
Self::client()?
.lpush(key, values)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn rpush(key: impl Into<String>, values: &[String]) -> Result<i64, BaseError> {
Self::client()?
.rpush(key, values)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn lpop(key: impl Into<String>) -> Result<Option<String>, BaseError> {
Self::client()?
.lpop(key)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn rpop(key: impl Into<String>) -> Result<Option<String>, BaseError> {
Self::client()?
.rpop(key)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn llen(key: impl Into<String>) -> Result<i64, BaseError> {
Self::client()?
.llen(key)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn lrange(
key: impl Into<String>,
start: i64,
stop: i64,
) -> Result<Vec<String>, BaseError> {
Self::client()?
.lrange(key, start, stop)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn sadd(key: impl Into<String>, members: &[String]) -> Result<i64, BaseError> {
Self::client()?
.sadd(key, members)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn srem(key: impl Into<String>, members: &[String]) -> Result<i64, BaseError> {
Self::client()?
.srem(key, members)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn sismember(
key: impl Into<String>,
member: impl Into<String>,
) -> Result<bool, BaseError> {
Self::client()?
.sismember(key, member)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn smembers(key: impl Into<String>) -> Result<Vec<String>, BaseError> {
Self::client()?
.smembers(key)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn scard(key: impl Into<String>) -> Result<i64, BaseError> {
Self::client()?
.scard(key)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn zadd(key: impl Into<String>, members: &[(f64, String)]) -> Result<i64, BaseError> {
Self::client()?
.zadd(key, members)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn zrem(key: impl Into<String>, members: &[String]) -> Result<i64, BaseError> {
Self::client()?
.zrem(key, members)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn zcard(key: impl Into<String>) -> Result<i64, BaseError> {
Self::client()?
.zcard(key)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
pub async fn zrange(
key: impl Into<String>,
start: i64,
stop: i64,
) -> Result<Vec<String>, BaseError> {
Self::client()?
.zrange(key, start, stop)
.await
.map_err(|e| BaseError::RedisOperationFailed(e.to_string()))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_redis_not_initialized() {
let result = GlobalRedis::client();
assert!(result.is_err());
assert!(matches!(result, Err(BaseError::RedisNotInitialized)));
}
}