cal_redis/cache/
asset.rs

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