Skip to content
Clone
f2e745a
164 lines 4.9 KB Rust
use std::collections::HashMap;
use redis::{AsyncCommands, Cmd, FromRedisValue, ToRedisArgs, ToSingleRedisArg};
use serde::Serialize;
use serde::de::DeserializeOwned;
use crate::error::{CacheError, CacheResult};
use crate::json::{decode, encode};
use crate::kv::Cache;
impl Cache {
pub async fn hget<T: FromRedisValue + Send>(
&self,
key: &str,
field: &str,
) -> CacheResult<Option<T>> {
let mut conn = self.conn().clone();
conn.hget(key, field).await.map_err(CacheError::from)
}
pub async fn hget_bytes(&self, key: &str, field: &str) -> CacheResult<Option<Vec<u8>>> {
self.hget::<Vec<u8>>(key, field).await
}
pub async fn hget_json<T: DeserializeOwned>(
&self,
key: &str,
field: &str,
) -> CacheResult<Option<T>> {
match self.hget_bytes(key, field).await? {
Some(bytes) => Ok(Some(decode(key, &bytes)?)),
None => Ok(None),
}
}
pub async fn hmget<T: FromRedisValue + Send, F: ToRedisArgs + Send + Sync>(
&self,
key: &str,
fields: F,
) -> CacheResult<Vec<Option<T>>> {
let mut conn = self.conn().clone();
conn.hmget(key, fields).await.map_err(CacheError::from)
}
pub async fn hset<F: ToSingleRedisArg + Send + Sync, V: ToSingleRedisArg + Send + Sync>(
&self,
key: &str,
field: F,
value: V,
) -> CacheResult<usize> {
let mut conn = self.conn().clone();
conn.hset(key, field, value).await.map_err(CacheError::from)
}
pub async fn hset_json<F: ToSingleRedisArg + Send + Sync, V: Serialize>(
&self,
key: &str,
field: F,
value: &V,
) -> CacheResult<usize> {
self.hset(key, field, encode(key, value)?).await
}
pub async fn hset_nx<V: ToRedisArgs + Send + Sync>(
&self,
key: &str,
field: &str,
value: V,
) -> CacheResult<bool> {
let mut cmd = Cmd::new();
cmd.arg("HSETNX").arg(key).arg(field).arg(value);
self.exec(&cmd).await
}
pub async fn hset_multiple<
F: ToSingleRedisArg + Send + Sync,
V: ToSingleRedisArg + Send + Sync,
>(
&self,
key: &str,
items: &[(F, V)],
) -> CacheResult<()> {
let mut conn = self.conn().clone();
conn.hset_multiple(key, items)
.await
.map_err(CacheError::from)
}
pub async fn hset_multiple_json<F: ToRedisArgs + Send + Sync, V: Serialize>(
&self,
key: &str,
items: &[(F, V)],
) -> CacheResult<()> {
let mut encoded = Vec::with_capacity(items.len());
for (field, value) in items {
encoded.push((field, encode(key, value)?));
}
let mut conn = self.conn().clone();
conn.hset_multiple(key, &encoded)
.await
.map_err(CacheError::from)
}
pub async fn hgetall<T: FromRedisValue + Send>(
&self,
key: &str,
) -> CacheResult<HashMap<String, T>> {
let mut conn = self.conn().clone();
conn.hgetall(key).await.map_err(CacheError::from)
}
pub async fn hgetall_bytes(&self, key: &str) -> CacheResult<HashMap<String, Vec<u8>>> {
self.hgetall::<Vec<u8>>(key).await
}
pub async fn hgetall_json<T: DeserializeOwned>(
&self,
key: &str,
) -> CacheResult<HashMap<String, T>> {
let raw = self.hgetall_bytes(key).await?;
let mut decoded = HashMap::with_capacity(raw.len());
for (field, bytes) in raw {
let value = decode(&field, &bytes)?;
decoded.insert(field, value);
}
Ok(decoded)
}
pub async fn hdel<F: ToRedisArgs + Send + Sync>(
&self,
key: &str,
fields: F,
) -> CacheResult<usize> {
let mut conn = self.conn().clone();
conn.hdel(key, fields).await.map_err(CacheError::from)
}
pub async fn hexists(&self, key: &str, field: &str) -> CacheResult<bool> {
let mut conn = self.conn().clone();
conn.hexists(key, field).await.map_err(CacheError::from)
}
pub async fn hlen(&self, key: &str) -> CacheResult<usize> {
let mut conn = self.conn().clone();
conn.hlen(key).await.map_err(CacheError::from)
}
pub async fn hkeys<T: FromRedisValue + Send>(&self, key: &str) -> CacheResult<Vec<T>> {
let mut conn = self.conn().clone();
conn.hkeys(key).await.map_err(CacheError::from)
}
pub async fn hvals<T: FromRedisValue + Send>(&self, key: &str) -> CacheResult<Vec<T>> {
let mut conn = self.conn().clone();
conn.hvals(key).await.map_err(CacheError::from)
}
pub async fn hincr(&self, key: &str, field: &str, delta: i64) -> CacheResult<i64> {
let mut conn = self.conn().clone();
conn.hincr(key, field, delta)
.await
.map_err(CacheError::from)
}
}