use crate::cache::CallableCache;
use crate::{get_ddi, get_ddis};
use cal_core::DDI;
use redis::RedisError;
impl CallableCache {
pub async fn get_ddis(self, account_id: &str) -> Result<Vec<DDI>, RedisError> {
println!("[CallableCache::get_ddis] Getting all DDIs for account: {}", account_id);
match get_ddis(self.remote_cache.connection.clone(), account_id).await {
Ok(ddis) => {
println!("[CallableCache::get_ddis] Retrieved {} DDIs from Redis", ddis.len());
for ddi in &ddis {
let cache_key = format!("{}:{}", account_id, ddi.id);
println!("[CallableCache::get_ddis] Caching DDI: {} (name: {}) with key: {}", ddi.id, ddi.name, cache_key);
self.local_cache.ddis.insert(cache_key, ddi.clone());
}
Ok(ddis)
}
Err(e) => {
println!("[CallableCache::get_ddis] Error retrieving DDIs from Redis: {:?}", e);
Err(e)
}
}
}
pub async fn get_ddi(self, account_id: &str, ddi_id: &str) -> Result<Option<DDI>, RedisError> {
println!("[CallableCache::get_ddi] Getting DDI - Account: {}, DDI ID: {}", account_id, ddi_id);
let cache_key = format!("{}:{}", account_id, ddi_id);
if let Some(ddi) = self.local_cache.ddis.get(&cache_key) {
println!("[CallableCache::get_ddi] Found DDI in local cache: {}", ddi.name);
return Ok(Some(ddi));
}
println!("[CallableCache::get_ddi] DDI not in local cache, fetching from Redis");
match get_ddi(self.remote_cache.connection.clone(), account_id, ddi_id).await {
Ok(Some(ddi)) => {
println!("[CallableCache::get_ddi] Found DDI in Redis: {} (name: {})", ddi.id, ddi.name);
self.local_cache.ddis.insert(cache_key, ddi.clone());
Ok(Some(ddi))
}
Ok(None) => {
println!("[CallableCache::get_ddi] DDI not found in Redis");
Ok(None)
}
Err(e) => {
println!("[CallableCache::get_ddi] Error retrieving DDI from Redis: {:?}", e);
Err(e)
}
}
}
}