cal-redis 0.1.80

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

use crate::cache::CallableCache;
use crate::{get_ddi, get_ddis};
use cal_core::DDI;
use redis::RedisError;

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

                // Cache each DDI locally for future use
                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)
            }
        }
    }

    /// Retrieves a specific DDI for an account.
    ///
    /// # Arguments
    /// * `account_id` - Account ID
    /// * `ddi_id` - DDI ID to retrieve
    ///
    /// # Returns
    /// * `Result<Option<DDI>, RedisError>` - The DDI if found, None if not found, or a Redis error
    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);
        // Try local cache first
        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));
        }

        // If not in local cache, try Redis
        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);
                // Cache for future use
                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)
            }
        }
    }
}