26af08e
use std::collections::HashMap;
use redis::{AsyncCommands, FromRedisValue, ToRedisArgs, ToSingleRedisArg};
use crate::error::{CacheError, CacheResult};
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 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_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 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 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)
}
}