cal_redis/cache/
hook.rs

1// File: cal-redis/src/cache/hook.rs
2
3use super::CallableCache;
4use crate::{get_hook, get_hooks};
5use cal_core::Hook;
6use redis::RedisError;
7
8impl CallableCache {
9    /// Retrieves all hooks for an account.
10    ///
11    /// # Arguments
12    /// * `account_id` - Account ID
13    ///
14    /// # Returns
15    /// * `Result<Vec<Hook>, RedisError>` - List of hooks or a Redis error
16    pub async fn get_hooks(self, account_id: &str) -> Result<Vec<Hook>, RedisError> {
17        println!("[CallableCache::get_hooks] Getting all hooks for account: {}", account_id);
18        // Get from Redis and update local cache
19        let hooks = get_hooks(self.remote_cache.connection.clone(), account_id).await?;
20        println!("[CallableCache::get_hooks] Retrieved {} hooks from Redis", hooks.len());
21
22        // Cache each hook locally for future use
23        for hook in &hooks {
24            let cache_key = format!("{}:{}", account_id, hook.id);
25            println!("[CallableCache::get_hooks] Caching hook: {} with key: {}", hook.url, cache_key);
26            self.local_cache.hooks.insert(cache_key, hook.clone());
27        }
28
29        Ok(hooks)
30    }
31
32    /// Retrieves a specific hook for an account.
33    ///
34    /// # Arguments
35    /// * `account_id` - Account ID
36    /// * `hook_id` - Hook ID to retrieve
37    ///
38    /// # Returns
39    /// * `Result<Option<Hook>, RedisError>` - The hook if found, None if not found, or a Redis error
40    pub async fn get_hook(
41        self,
42        account_id: &str,
43        hook_id: &str,
44    ) -> Result<Option<Hook>, RedisError> {
45        println!("[CallableCache::get_hook] Getting hook - Account: {}, Hook ID: {}", account_id, hook_id);
46        // Try local cache first
47        let cache_key = format!("{}:{}", account_id, hook_id);
48        if let Some(hook) = self.local_cache.hooks.get(&cache_key) {
49            println!("[CallableCache::get_hook] Found hook in local cache");
50            return Ok(Some(hook));
51        }
52
53        // If not in local cache, try Redis
54        println!("[CallableCache::get_hook] Hook not in local cache, fetching from Redis");
55        match get_hook(self.remote_cache.connection.clone(), account_id, hook_id).await? {
56            Some(hook) => {
57                println!("[CallableCache::get_hook] Found hook in Redis: {}", hook.url);
58                // Cache for future use
59                self.local_cache.hooks.insert(cache_key, hook.clone());
60                Ok(Some(hook))
61            }
62            None => {
63                println!("[CallableCache::get_hook] Hook not found in Redis");
64                Ok(None)
65            }
66        }
67    }
68}