cal-redis 0.1.80

Callable Redis Implementation
Documentation
use cal_core::device::device::DeviceStruct;
use cal_core::{AccountLite, Asset, FlowState, Hook, Proxy, Region, Trunk, User, UserLite, DDI};
use moka::sync::Cache;
use std::sync::Arc;
use std::time::Duration;


/// In-memory cache for storing frequently accessed data.
///
/// Uses Moka cache for efficient memory usage with TTL-based expiration.
/// Now supports shared references via `Arc` for complex types like `FlowState`.
#[derive(Clone)]
pub struct LocalCache {
    /// Cache for proxy objects
    pub proxies: Cache<String, Arc<Proxy>>,
    /// Cache for flow state objects (shared via Arc, longer TTL)
    pub flow_state: Cache<String, Arc<FlowState>>,
    /// Cache for region objects
    pub regions: Cache<String, Arc<Region>>,
    /// Cache for region identifier mappings
    pub region_idents: Cache<String, String>,
    /// Cache for account objects
    pub accounts: Cache<String, Arc<AccountLite>>,
    /// Cache for account identifier mappings
    pub account_idents: Cache<String, String>,
    /// Cache for device objects
    pub devices: Cache<String, Arc<DeviceStruct>>,
    /// Cache for trunk objects
    pub trunks: Cache<String, Trunk>,
    /// Cache for DDI objects
    pub ddis: Cache<String, DDI>,
    /// Cache for hook objects
    pub hooks: Cache<String, Hook>,
    /// Cache for asset objects
    pub assets: Cache<String, Asset>,
    /// Cache for trunk_ddi identifier mappings
    pub trunk_ddi_idents: Cache<String, String>,
    /// Cache for ddi_device identifier mappings
    pub device_idents: Cache<String, String>,
    /// Cache for user objects
    pub users: Cache<String, Arc<User>>,
    /// Cache for user identifier mappings
    pub user_idents: Cache<String, String>,
}

/// Helper function to build a cache with specific capacity and TTL.
fn create_cache<T: Clone + Send + Sync + 'static>(capacity_mb: u64, ttl_secs: u64) -> Cache<String, T> {
    Cache::builder()
        .max_capacity(capacity_mb * 1024 * 1024)
        .time_to_live(Duration::from_secs(ttl_secs))
        .build()
}

/// Builds the full local cache with default parameters.
/// Includes Arc support for `FlowState`.
pub fn build_local_cache() -> LocalCache {
    LocalCache {
        flow_state: create_cache::<Arc<FlowState>>(50, 60 * 60 * 4),
        regions: create_cache::<Arc<Region>>(5, 60),
        accounts: create_cache::<Arc<AccountLite>>(50, 60),
        devices: create_cache::<Arc<DeviceStruct>>(20, 60),
        trunks: create_cache::<Trunk>(10, 60),
        ddis: create_cache::<DDI>(10, 60),
        hooks: create_cache::<Hook>(10, 60),
        assets: create_cache::<Asset>(10, 60),
        proxies: create_cache::<Arc<Proxy>>(10, 60 * 60),
        region_idents: create_cache::<String>(5, 60),
        account_idents: create_cache::<String>(5, 60),
        trunk_ddi_idents: create_cache::<String>(10, 60),
        device_idents: create_cache::<String>(10, 60),
        users: create_cache::<Arc<User>>(20, 60),
        user_idents: create_cache::<String>(5, 60),
    }
}

/// Builds and stores region identifier mappings in the local cache.
///
/// # Arguments
/// * `reg` - Region to store locally
///
/// # Returns
/// * `()` - This function doesn't fail
pub fn build_region_identifiers_locally(local_cache: LocalCache, reg: Arc<Region>) {
    // Store the region in local cache
    local_cache.regions.insert(reg.id.to_string(), reg.clone());

    // Add primary identifier
    local_cache.region_idents.insert(reg.id.to_string(), reg.id.to_string());

    // Add alternative identifiers
    for vs in &reg.voice_servers {
        local_cache.region_idents.insert(vs.id.to_string(), reg.id.to_string());
        local_cache.region_idents.insert(vs.public_ip.to_string(), reg.id.to_string());
        local_cache.region_idents.insert(vs.private_ip.to_string(), reg.id.to_string());
    }
}

/// Populates the local account cache with account identifiers.
///
/// # Arguments
/// * `acc` - Account to cache locally
///
/// # Returns
/// * `()` - This function doesn't fail
pub fn build_account_identifiers_locally(local_cache: LocalCache, acc: &AccountLite) {

    println!("Building account identifiers for {}", acc.id);

    // Store the account in local cache
    local_cache.accounts.insert(acc.id.clone(), Arc::new(acc.clone()));

    // Add primary identifier to local cache
    local_cache.account_idents.insert(acc.id.clone(), acc.id.clone());

    // Add domain-based identifiers to local cache
    local_cache.account_idents.insert(
        format!("{}.connect.callable.io", acc.domain),
        acc.id.clone(),
    );

    // Add teams identifiers to local cache
    if !acc.teams.domain.is_empty() {
        local_cache.account_idents.insert(
            format!("{}.teams.callable.io", acc.teams.domain),
            acc.id.clone(),
        );
    }
}



// Add this function to local_cache.rs
pub fn build_user_identifiers_locally(local_cache: &LocalCache, user: &User) {
    println!("Building user identifiers for {}", user.id);

    // Store the user in local cache
    local_cache.users.insert(user.id.clone(), Arc::new(user.clone()));

    // Add primary identifier to local cache
    local_cache.user_idents.insert(user.id.clone(), user.id.clone());

    // Add email identifier to local cache
    local_cache.user_idents.insert(user.email.clone(), user.id.clone());
}