cal-redis 0.1.80

Callable Redis Implementation
Documentation
// File: cal-redis/src/cache/hook.rs

use super::CallableCache;
use crate::{get_hook, get_hooks};
use cal_core::Hook;
use redis::RedisError;

impl CallableCache {
    /// Retrieves all hooks for an account.
    ///
    /// # Arguments
    /// * `account_id` - Account ID
    ///
    /// # Returns
    /// * `Result<Vec<Hook>, RedisError>` - List of hooks or a Redis error
    pub async fn get_hooks(self, account_id: &str) -> Result<Vec<Hook>, RedisError> {
        println!("[CallableCache::get_hooks] Getting all hooks for account: {}", account_id);
        // Get from Redis and update local cache
        let hooks = get_hooks(self.remote_cache.connection.clone(), account_id).await?;
        println!("[CallableCache::get_hooks] Retrieved {} hooks from Redis", hooks.len());

        // Cache each hook locally for future use
        for hook in &hooks {
            let cache_key = format!("{}:{}", account_id, hook.id);
            println!("[CallableCache::get_hooks] Caching hook: {} with key: {}", hook.url, cache_key);
            self.local_cache.hooks.insert(cache_key, hook.clone());
        }

        Ok(hooks)
    }

    /// Retrieves a specific hook for an account.
    ///
    /// # Arguments
    /// * `account_id` - Account ID
    /// * `hook_id` - Hook ID to retrieve
    ///
    /// # Returns
    /// * `Result<Option<Hook>, RedisError>` - The hook if found, None if not found, or a Redis error
    pub async fn get_hook(
        self,
        account_id: &str,
        hook_id: &str,
    ) -> Result<Option<Hook>, RedisError> {
        println!("[CallableCache::get_hook] Getting hook - Account: {}, Hook ID: {}", account_id, hook_id);
        // Try local cache first
        let cache_key = format!("{}:{}", account_id, hook_id);
        if let Some(hook) = self.local_cache.hooks.get(&cache_key) {
            println!("[CallableCache::get_hook] Found hook in local cache");
            return Ok(Some(hook));
        }

        // If not in local cache, try Redis
        println!("[CallableCache::get_hook] Hook not in local cache, fetching from Redis");
        match get_hook(self.remote_cache.connection.clone(), account_id, hook_id).await? {
            Some(hook) => {
                println!("[CallableCache::get_hook] Found hook in Redis: {}", hook.url);
                // Cache for future use
                self.local_cache.hooks.insert(cache_key, hook.clone());
                Ok(Some(hook))
            }
            None => {
                println!("[CallableCache::get_hook] Hook not found in Redis");
                Ok(None)
            }
        }
    }
}