cal-redis 0.1.80

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

use super::CallableCache;
use crate::{get_asset, get_assets};
use cal_core::Asset;
use redis::RedisError;

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

        // Cache each asset locally for future use
        for asset in &assets {
            let cache_key = format!("{}:{}", account_id, asset.id);
            println!("[CallableCache::get_assets] Caching asset: {} with key: {}", asset.id, cache_key);
            self.local_cache.assets.insert(cache_key, asset.clone());
        }

        Ok(assets)
    }

    /// Retrieves a specific asset for an account.
    ///
    /// # Arguments
    /// * `account_id` - Account ID
    /// * `asset_id` - Asset ID to retrieve
    ///
    /// # Returns
    /// * `Result<Option<Asset>, RedisError>` - The asset if found, None if not found, or a Redis error
    pub async fn get_asset(
        self,
        account_id: &str,
        asset_id: &str,
    ) -> Result<Option<Asset>, RedisError> {
        println!("[CallableCache::get_asset] Getting asset - Account: {}, Asset ID: {}", account_id, asset_id);
        // Try local cache first
        let cache_key = format!("{}:{}", account_id, asset_id);
        if let Some(asset) = self.local_cache.assets.get(&cache_key) {
            println!("[CallableCache::get_asset] Found asset in local cache");
            return Ok(Some(asset));
        }

        // If not in local cache, try Redis
        println!("[CallableCache::get_asset] Asset not in local cache, fetching from Redis");
        match get_asset(self.remote_cache.connection.clone(), account_id, asset_id).await? {
            Some(asset) => {
                println!("[CallableCache::get_asset] Found asset in Redis: {}", asset.id);
                // Cache for future use
                self.local_cache.assets.insert(cache_key, asset.clone());
                Ok(Some(asset))
            }
            None => {
                println!("[CallableCache::get_asset] Asset not found in Redis");
                Ok(None)
            }
        }
    }
}