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( &self, key: &str, value: V, ) -> CacheResult { let mut conn = self.conn().clone(); conn.lpush(key, value).await.map_err(CacheError::from) } pub async fn lpush_many( &self, key: &str, values: Vec, ) -> CacheResult { let mut conn = self.conn().clone(); conn.lpush(key, values).await.map_err(CacheError::from) } pub async fn lpush_capped( &self, key: &str, value: V, limit: usize, ) -> CacheResult { 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( &self, key: &str, value: V, ) -> CacheResult { let mut conn = self.conn().clone(); conn.rpush(key, value).await.map_err(CacheError::from) } pub async fn rpush_many( &self, key: &str, values: Vec, ) -> CacheResult { let mut conn = self.conn().clone(); conn.rpush(key, values).await.map_err(CacheError::from) } pub async fn lpop(&self, key: &str) -> CacheResult> { let mut conn = self.conn().clone(); conn.lpop(key, None).await.map_err(CacheError::from) } pub async fn lpop_many( &self, key: &str, count: NonZeroUsize, ) -> CacheResult> { let mut conn = self.conn().clone(); conn.lpop(key, Some(count)).await.map_err(CacheError::from) } pub async fn rpop(&self, key: &str) -> CacheResult> { let mut conn = self.conn().clone(); conn.rpop(key, None).await.map_err(CacheError::from) } pub async fn rpop_many( &self, key: &str, count: NonZeroUsize, ) -> CacheResult> { let mut conn = self.conn().clone(); conn.rpop(key, Some(count)).await.map_err(CacheError::from) } pub async fn llen(&self, key: &str) -> CacheResult { let mut conn = self.conn().clone(); conn.llen(key).await.map_err(CacheError::from) } pub async fn lrange( &self, key: &str, start: isize, stop: isize, ) -> CacheResult> { 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( &self, key: &str, index: isize, ) -> CacheResult> { let mut conn = self.conn().clone(); conn.lindex(key, index).await.map_err(CacheError::from) } pub async fn lset( &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( &self, key: &str, count: isize, value: V, ) -> CacheResult { let mut conn = self.conn().clone(); conn.lrem(key, count, value).await.map_err(CacheError::from) } pub async fn linsert( &self, key: &str, before: bool, pivot: V, value: V, ) -> CacheResult { let mut cmd = Cmd::new(); cmd.arg("LINSERT") .arg(key) .arg(if before { "BEFORE" } else { "AFTER" }) .arg(pivot) .arg(value); self.exec(&cmd).await } }