Skip to content
Clone
f2e745a
156 lines 4.4 KB Rust
use std::num::NonZeroUsize;
use redis::{AsyncCommands, Cmd, FromRedisValue, ToRedisArgs, ToSingleRedisArg};
use crate::error::{CacheError, CacheResult};
use crate::kv::Cache;
impl Cache {
pub async fn lpush<V: ToRedisArgs + Send + Sync>(
&self,
key: &str,
value: V,
) -> CacheResult<usize> {
let mut conn = self.conn().clone();
conn.lpush(key, value).await.map_err(CacheError::from)
}
pub async fn lpush_many<V: ToRedisArgs + Send + Sync>(
&self,
key: &str,
values: Vec<V>,
) -> CacheResult<usize> {
let mut conn = self.conn().clone();
conn.lpush(key, values).await.map_err(CacheError::from)
}
pub async fn lpush_capped<V: ToRedisArgs + Send + Sync>(
&self,
key: &str,
value: V,
limit: usize,
) -> CacheResult<usize> {
let length = self.lpush(key, value).await?;
if limit == 0 {
self.delete(key).await?;
return Ok(0);
}
if length > limit {
self.ltrim(key, 0, limit as isize - 1).await?;
}
Ok(length.min(limit))
}
pub async fn rpush<V: ToRedisArgs + Send + Sync>(
&self,
key: &str,
value: V,
) -> CacheResult<usize> {
let mut conn = self.conn().clone();
conn.rpush(key, value).await.map_err(CacheError::from)
}
pub async fn rpush_many<V: ToRedisArgs + Send + Sync>(
&self,
key: &str,
values: Vec<V>,
) -> CacheResult<usize> {
let mut conn = self.conn().clone();
conn.rpush(key, values).await.map_err(CacheError::from)
}
pub async fn lpop<T: FromRedisValue + Send>(&self, key: &str) -> CacheResult<Option<T>> {
let mut conn = self.conn().clone();
conn.lpop(key, None).await.map_err(CacheError::from)
}
pub async fn lpop_many<T: FromRedisValue + Send>(
&self,
key: &str,
count: NonZeroUsize,
) -> CacheResult<Vec<T>> {
let mut conn = self.conn().clone();
conn.lpop(key, Some(count)).await.map_err(CacheError::from)
}
pub async fn rpop<T: FromRedisValue + Send>(&self, key: &str) -> CacheResult<Option<T>> {
let mut conn = self.conn().clone();
conn.rpop(key, None).await.map_err(CacheError::from)
}
pub async fn rpop_many<T: FromRedisValue + Send>(
&self,
key: &str,
count: NonZeroUsize,
) -> CacheResult<Vec<T>> {
let mut conn = self.conn().clone();
conn.rpop(key, Some(count)).await.map_err(CacheError::from)
}
pub async fn llen(&self, key: &str) -> CacheResult<usize> {
let mut conn = self.conn().clone();
conn.llen(key).await.map_err(CacheError::from)
}
pub async fn lrange<T: FromRedisValue + Send>(
&self,
key: &str,
start: isize,
stop: isize,
) -> CacheResult<Vec<T>> {
let mut conn = self.conn().clone();
conn.lrange(key, start, stop)
.await
.map_err(CacheError::from)
}
pub async fn ltrim(&self, key: &str, start: isize, stop: isize) -> CacheResult<()> {
let mut conn = self.conn().clone();
conn.ltrim(key, start, stop).await.map_err(CacheError::from)
}
pub async fn lindex<T: FromRedisValue + Send>(
&self,
key: &str,
index: isize,
) -> CacheResult<Option<T>> {
let mut conn = self.conn().clone();
conn.lindex(key, index).await.map_err(CacheError::from)
}
pub async fn lset<V: ToSingleRedisArg + Send + Sync>(
&self,
key: &str,
index: isize,
value: V,
) -> CacheResult<()> {
let mut conn = self.conn().clone();
conn.lset(key, index, value).await.map_err(CacheError::from)
}
pub async fn lrem<V: ToSingleRedisArg + Send + Sync>(
&self,
key: &str,
count: isize,
value: V,
) -> CacheResult<usize> {
let mut conn = self.conn().clone();
conn.lrem(key, count, value).await.map_err(CacheError::from)
}
pub async fn linsert<V: ToSingleRedisArg + Send + Sync>(
&self,
key: &str,
before: bool,
pivot: V,
value: V,
) -> CacheResult<i64> {
let mut cmd = Cmd::new();
cmd.arg("LINSERT")
.arg(key)
.arg(if before { "BEFORE" } else { "AFTER" })
.arg(pivot)
.arg(value);
self.exec(&cmd).await
}
}