use super::CallableCache;
use crate::{get_trunk, get_trunks};
use cal_core::Trunk;
use redis::RedisError;
impl CallableCache {
pub async fn get_trunks(self, account_id: &str) -> Result<Vec<Trunk>, RedisError> {
println!("[CallableCache::get_trunks] Getting all trunks for account: {}", account_id);
let trunks = get_trunks(self.remote_cache.connection.clone(), account_id).await?;
println!("[CallableCache::get_trunks] Retrieved {} trunks from Redis", trunks.len());
for trunk in &trunks {
let cache_key = format!("{}:{}", account_id, trunk.id);
println!("[CallableCache::get_trunks] Caching trunk: {} with key: {}", trunk.ip, cache_key);
self.local_cache.trunks.insert(cache_key, trunk.clone());
}
Ok(trunks)
}
pub async fn get_trunk(
self,
account_id: &str,
trunk_id: &str,
) -> Result<Option<Trunk>, RedisError> {
println!("[CallableCache::get_trunk] Getting trunk - Account: {}, Trunk ID: {}", account_id, trunk_id);
let cache_key = format!("{}:{}", account_id, trunk_id);
if let Some(trunk) = self.local_cache.trunks.get(&cache_key) {
println!("[CallableCache::get_trunk] Found trunk in local cache");
return Ok(Some(trunk));
}
println!("[CallableCache::get_trunk] Trunk not in local cache, fetching from Redis");
match get_trunk(self.remote_cache.connection.clone(), account_id, trunk_id).await? {
Some(trunk) => {
println!("[CallableCache::get_trunk] Found trunk in Redis: {}", trunk.ip);
self.local_cache.trunks.insert(cache_key, trunk.clone());
Ok(Some(trunk))
}
None => {
println!("[CallableCache::get_trunk] Trunk not found in Redis");
Ok(None)
}
}
}
}