use super::CallableCache;
use crate::{get_hook, get_hooks};
use cal_core::Hook;
use redis::RedisError;
impl CallableCache {
pub async fn get_hooks(self, account_id: &str) -> Result<Vec<Hook>, RedisError> {
println!("[CallableCache::get_hooks] Getting all hooks for account: {}", account_id);
let hooks = get_hooks(self.remote_cache.connection.clone(), account_id).await?;
println!("[CallableCache::get_hooks] Retrieved {} hooks from Redis", hooks.len());
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)
}
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);
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));
}
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);
self.local_cache.hooks.insert(cache_key, hook.clone());
Ok(Some(hook))
}
None => {
println!("[CallableCache::get_hook] Hook not found in Redis");
Ok(None)
}
}
}
}