feat: 完善生产级 Rust Web 模板
This commit is contained in:
@@ -52,11 +52,7 @@ impl RedisClient {
|
||||
}
|
||||
|
||||
/// 使用 RedisKey 设置 JSON 值
|
||||
pub async fn set_key<T: Serialize>(
|
||||
&self,
|
||||
key: &RedisKey,
|
||||
value: &T,
|
||||
) -> redis::RedisResult<()> {
|
||||
pub async fn set_key<T: Serialize>(&self, key: &RedisKey, value: &T) -> redis::RedisResult<()> {
|
||||
let json = serde_json::to_string(value).map_err(|e| {
|
||||
redis::RedisError::from((
|
||||
redis::ErrorKind::TypeError,
|
||||
@@ -132,4 +128,40 @@ impl RedisClient {
|
||||
let mut c = self.conn.lock().await;
|
||||
c.expire(key.build(), seconds as i64).await
|
||||
}
|
||||
|
||||
pub async fn queue_push<T: Serialize>(&self, key: &str, value: &T) -> redis::RedisResult<()> {
|
||||
let json = serde_json::to_string(value).map_err(|e| {
|
||||
redis::RedisError::from((
|
||||
redis::ErrorKind::TypeError,
|
||||
"JSON serialization failed",
|
||||
e.to_string(),
|
||||
))
|
||||
})?;
|
||||
let mut connection = self.conn.lock().await;
|
||||
connection.lpush(key, json).await
|
||||
}
|
||||
|
||||
pub async fn queue_pop<T: for<'de> serde::Deserialize<'de>>(
|
||||
&self,
|
||||
key: &str,
|
||||
) -> redis::RedisResult<Option<T>> {
|
||||
let mut connection = self.conn.lock().await;
|
||||
let value: Option<String> = connection.rpop(key, None).await?;
|
||||
value
|
||||
.map(|json| {
|
||||
serde_json::from_str(&json).map_err(|e| {
|
||||
redis::RedisError::from((
|
||||
redis::ErrorKind::TypeError,
|
||||
"JSON deserialization failed",
|
||||
e.to_string(),
|
||||
))
|
||||
})
|
||||
})
|
||||
.transpose()
|
||||
}
|
||||
|
||||
pub async fn queue_len(&self, key: &str) -> redis::RedisResult<usize> {
|
||||
let mut connection = self.conn.lock().await;
|
||||
connection.llen(key).await
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user