cal_redis/cache/
queue.rs

1// File: cal-redis/src/cache/queue.rs
2
3use super::CallableCache;
4use cal_core::QueueGroup;
5use redis::{AsyncCommands, RedisError};
6
7impl CallableCache {
8    /// Store a QueueGroup
9    pub async fn queue_group_set(&self, queue: &QueueGroup) -> Result<(), RedisError> {
10        println!("[CallableCache::queue_group_set] Setting queue group - Account: {}, Name: {}, Type: {}",
11                 queue.account_id, queue.name, queue.get_type());
12        let key = crate::constants::QueueKeys::queue(&queue.account_id, &queue.name);
13        let json = serde_json::to_string(queue)
14            .map_err(super::helpers::serde_to_redis_error)?;
15
16        let mut con = self.redis_connection();
17        con.set(&key, json).await?;
18
19        // Track in active queues
20        con.sadd(&crate::constants::QueueKeys::active(&queue.account_id), &queue.name).await?;
21
22        // Store metadata
23        let meta_key = crate::constants::QueueKeys::metadata(&queue.account_id);
24        let meta = serde_json::json!({
25            "name": queue.name,
26            "type": queue.get_type(),
27            "device_id": queue.device_id,
28            "entry_count": queue.entries.len()
29        });
30        con.hset(&meta_key, &queue.name, meta.to_string()).await?;
31
32        Ok(())
33    }
34
35    /// Get a QueueGroup
36    pub async fn queue_group_get(&self, account_id: &str, queue_name: &str) -> Result<Option<QueueGroup>, RedisError> {
37        println!("[CallableCache::queue_group_get] Getting queue group - Account: {}, Name: {}", account_id, queue_name);
38        let key = crate::constants::QueueKeys::queue(account_id, queue_name);
39
40        let mut con = self.redis_connection();
41        match con.get::<_, Option<String>>(&key).await? {
42            Some(json) => {
43                let queue: QueueGroup = serde_json::from_str(&json)
44                    .map_err(super::helpers::serde_to_redis_error)?;
45                // Validate account_id
46                if queue.account_id != account_id {
47                    println!("[CallableCache::queue_group_get] Account ID mismatch! Expected: {}, Got: {}",
48                             account_id, queue.account_id);
49                    return Ok(None);
50                }
51                println!("[CallableCache::queue_group_get] Found queue group: {} entries", queue.entries.len());
52                Ok(Some(queue))
53            }
54            None => {
55                println!("[CallableCache::queue_group_get] Queue group not found");
56                Ok(None)
57            }
58        }
59    }
60
61    /// Delete a QueueGroup
62    pub async fn queue_group_delete(&self, account_id: &str, queue_name: &str) -> Result<(), RedisError> {
63        println!("[CallableCache::queue_group_delete] Deleting queue group - Account: {}, Name: {}", account_id, queue_name);
64        let key = crate::constants::QueueKeys::queue(account_id, queue_name);
65        let mut con = self.redis_connection();
66
67        // Remove queue data
68        con.del(&key).await?;
69
70        // Remove from active set
71        con.srem(&crate::constants::QueueKeys::active(account_id), queue_name).await?;
72
73        // Remove metadata
74        let meta_key = crate::constants::QueueKeys::metadata(account_id);
75        con.hdel(&meta_key, queue_name).await?;
76
77        Ok(())
78    }
79
80    /// Get all active queue names for an account
81    pub async fn queue_group_list(&self, account_id: &str) -> Result<Vec<String>, RedisError> {
82        println!("[CallableCache::queue_group_list] Getting all queue names for account: {}", account_id);
83        let mut con = self.redis_connection();
84        let names: Vec<String> = con.smembers(&crate::constants::QueueKeys::active(account_id)).await?;
85        println!("[CallableCache::queue_group_list] Found {} queue names", names.len());
86        Ok(names)
87    }
88
89    /// Get all QueueGroups for an account
90    pub async fn queue_groups_get_all(&self, account_id: &str) -> Result<Vec<QueueGroup>, RedisError> {
91        println!("[CallableCache::queue_groups_get_all] Getting all queue groups for account: {}", account_id);
92        let queue_names = self.queue_group_list(account_id).await?;
93        let mut queues = Vec::new();
94
95        for name in queue_names {
96            if let Some(queue) = self.queue_group_get(account_id, &name).await? {
97                queues.push(queue);
98            }
99        }
100
101        println!("[CallableCache::queue_groups_get_all] Returning {} queue groups", queues.len());
102        Ok(queues)
103    }
104}