cal_redis/cache/
ddi.rs

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