cal_redis/
redis_ops.rs

1use redis::{AsyncCommands, RedisError, aio::MultiplexedConnection};
2use crate::cache::CallableCache;
3
4/// Provides direct Redis operations while maintaining consistency with caching layer
5pub struct RedisOps;
6
7impl RedisOps {
8    /// Get a raw Redis connection from CallableCache
9    /// Use this for custom Redis operations not covered by the standard API
10    pub fn get_connection(cache: &CallableCache) -> MultiplexedConnection {
11        cache.remote_cache.connection.clone()
12    }
13
14    /// Generic key-value operations
15    pub async fn set(con: &mut MultiplexedConnection, key: &str, value: &str) -> Result<(), RedisError> {
16        con.set(key, value).await
17    }
18
19    pub async fn get(con: &mut MultiplexedConnection, key: &str) -> Result<Option<String>, RedisError> {
20        con.get(key).await
21    }
22
23    pub async fn set_ex(con: &mut MultiplexedConnection, key: &str, value: &str, seconds: u64) -> Result<(), RedisError> {
24        con.set_ex(key, value, seconds).await
25    }
26
27    pub async fn del(con: &mut MultiplexedConnection, key: &str) -> Result<(), RedisError> {
28        con.del(key).await
29    }
30
31    /// Set operations
32    pub async fn sadd(con: &mut MultiplexedConnection, key: &str, member: &str) -> Result<(), RedisError> {
33        con.sadd(key, member).await
34    }
35
36    pub async fn srem(con: &mut MultiplexedConnection, key: &str, member: &str) -> Result<(), RedisError> {
37        con.srem(key, member).await
38    }
39
40    pub async fn smembers(con: &mut MultiplexedConnection, key: &str) -> Result<Vec<String>, RedisError> {
41        con.smembers(key).await
42    }
43
44    /// Hash operations
45    pub async fn hset(con: &mut MultiplexedConnection, key: &str, field: &str, value: &str) -> Result<(), RedisError> {
46        con.hset(key, field, value).await
47    }
48
49    pub async fn hget(con: &mut MultiplexedConnection, key: &str, field: &str) -> Result<Option<String>, RedisError> {
50        con.hget(key, field).await
51    }
52
53    pub async fn hdel(con: &mut MultiplexedConnection, key: &str, field: &str) -> Result<(), RedisError> {
54        con.hdel(key, field).await
55    }
56
57    /// Pub/Sub operations
58    pub async fn publish(con: &mut MultiplexedConnection, channel: &str, message: &str) -> Result<(), RedisError> {
59        con.publish(channel, message).await
60    }
61}