cal-redis 0.1.80

Callable Redis Implementation
Documentation
use redis::{AsyncCommands, RedisError, aio::MultiplexedConnection};
use crate::cache::CallableCache;

/// Provides direct Redis operations while maintaining consistency with caching layer
pub struct RedisOps;

impl RedisOps {
    /// Get a raw Redis connection from CallableCache
    /// Use this for custom Redis operations not covered by the standard API
    pub fn get_connection(cache: &CallableCache) -> MultiplexedConnection {
        cache.remote_cache.connection.clone()
    }

    /// Generic key-value operations
    pub async fn set(con: &mut MultiplexedConnection, key: &str, value: &str) -> Result<(), RedisError> {
        con.set(key, value).await
    }

    pub async fn get(con: &mut MultiplexedConnection, key: &str) -> Result<Option<String>, RedisError> {
        con.get(key).await
    }

    pub async fn set_ex(con: &mut MultiplexedConnection, key: &str, value: &str, seconds: u64) -> Result<(), RedisError> {
        con.set_ex(key, value, seconds).await
    }

    pub async fn del(con: &mut MultiplexedConnection, key: &str) -> Result<(), RedisError> {
        con.del(key).await
    }

    /// Set operations
    pub async fn sadd(con: &mut MultiplexedConnection, key: &str, member: &str) -> Result<(), RedisError> {
        con.sadd(key, member).await
    }

    pub async fn srem(con: &mut MultiplexedConnection, key: &str, member: &str) -> Result<(), RedisError> {
        con.srem(key, member).await
    }

    pub async fn smembers(con: &mut MultiplexedConnection, key: &str) -> Result<Vec<String>, RedisError> {
        con.smembers(key).await
    }

    /// Hash operations
    pub async fn hset(con: &mut MultiplexedConnection, key: &str, field: &str, value: &str) -> Result<(), RedisError> {
        con.hset(key, field, value).await
    }

    pub async fn hget(con: &mut MultiplexedConnection, key: &str, field: &str) -> Result<Option<String>, RedisError> {
        con.hget(key, field).await
    }

    pub async fn hdel(con: &mut MultiplexedConnection, key: &str, field: &str) -> Result<(), RedisError> {
        con.hdel(key, field).await
    }

    /// Pub/Sub operations
    pub async fn publish(con: &mut MultiplexedConnection, channel: &str, message: &str) -> Result<(), RedisError> {
        con.publish(channel, message).await
    }
}