cal_redis/
redis_cache.rs

1use crate::user::insert_user;
2use crate::{get_asset, get_assets, get_ddi, get_ddis, get_device, get_devices, get_hash, get_hook, get_hooks, get_region_by_id, get_region_by_ident, get_regions, get_str, get_trunk, get_trunks, insert_account, insert_account_idents,
3            insert_proxies, insert_proxy, insert_region, insert_region_idents};
4use cal_core::device::device::DeviceStruct;
5use cal_core::{Account, Asset, Hook, Proxy, Region, Trunk, User, DDI};
6use redis::aio::MultiplexedConnection;
7use redis::RedisError;
8
9#[derive(Clone)]
10pub struct RedisCache {
11    pub connection: MultiplexedConnection,
12}
13
14impl RedisCache {
15
16    /// Inserts a proxy into Redis.
17    ///
18    /// # Arguments
19    /// * `value` - The proxy to insert
20    ///
21    /// # Returns
22    /// * `Result<RedisCache, RedisError>` - Self for method chaining or a Redis error
23    pub async fn insert_proxy(self, value: Proxy) -> Result<RedisCache, RedisError> {
24        insert_proxy(self.connection.clone(), value).await?;
25        Ok(self)
26    }
27
28    /// Inserts proxies into Redis.
29    ///
30    /// # Arguments
31    /// * `value` - The proxies Vec to insert
32    ///
33    /// # Returns
34    /// * `Result<RedisCache, RedisError>` - Self for method chaining or a Redis error
35    pub async fn insert_proxies(self, value: Vec<Proxy>) -> Result<RedisCache, RedisError> {
36        insert_proxies(self.connection.clone(), value).await?;
37        Ok(self)
38    }
39
40
41    /// Inserts a region into Redis.
42    ///
43    /// # Arguments
44    /// * `value` - The region to insert
45    ///
46    /// # Returns
47    /// * `Result<RedisCache, RedisError>` - Self for method chaining or a Redis error
48    pub async fn insert_region(self, value: Region) -> Result<RedisCache, RedisError> {
49        insert_region(self.connection.clone(), value).await?;
50        Ok(self)
51    }
52
53    /// Inserts region identifiers into Redis.
54    ///
55    /// # Arguments
56    /// * `items` - Pairs of (identifier, region_id) to insert
57    ///
58    /// # Returns
59    /// * `Result<RedisCache, RedisError>` - Self for method chaining or a Redis error
60    pub async fn insert_region_idents(self, items: Vec<(String, String)>) -> Result<RedisCache, RedisError> {
61        insert_region_idents(self.connection.clone(), &items).await?;
62        Ok(self)
63    }
64
65    /// Inserts an account into Redis.
66    ///
67    /// # Arguments
68    /// * `value` - The account to insert
69    ///
70    /// # Returns
71    /// * `Result<RedisCache, RedisError>` - Self for method chaining or a Redis error
72    pub async fn insert_account(self, value: Account) -> Result<RedisCache, RedisError> {
73        insert_account(self.connection.clone(), value).await?;
74        Ok(self)
75    }
76
77    /// Inserts account identifiers into Redis.
78    ///
79    /// # Arguments
80    /// * `items` - Pairs of (identifier, account_id) to insert
81    ///
82    /// # Returns
83    /// * `Result<RedisCache, RedisError>` - Self for method chaining or a Redis error
84    pub async fn insert_account_idents(self, items: Vec<(String, String)>) -> Result<RedisCache, RedisError> {
85        insert_account_idents(self.connection.clone(), &items).await?;
86        Ok(self)
87    }
88
89    /// Retrieves all devices for an account.
90    ///
91    /// # Arguments
92    /// * `account_id` - Account ID
93    ///
94    /// # Returns
95    /// * `Result<Vec<DeviceStruct>, RedisError>` - List of devices or a Redis error
96    pub async fn get_devices(self, account_id: String) -> Result<Vec<DeviceStruct>, RedisError> {
97        get_devices(self.connection.clone(), &account_id).await
98    }
99
100    /// Retrieves a specific device for an account.
101    ///
102    /// # Arguments
103    /// * `account_id` - Account ID
104    /// * `device_id` - Device ID to retrieve
105    ///
106    /// # Returns
107    /// * `Result<Option<DeviceStruct>, RedisError>` - The device if found, None if not found, or a Redis error
108    pub async fn get_device(self, account_id: String, device_id: String) -> Result<Option<DeviceStruct>, RedisError> {
109        get_device(self.connection.clone(), &account_id, &device_id).await
110    }
111
112    /// Retrieves all trunks for an account.
113    ///
114    /// # Arguments
115    /// * `account_id` - Account ID
116    ///
117    /// # Returns
118    /// * `Result<Vec<Trunk>, RedisError>` - List of trunks or a Redis error
119    pub async fn get_trunks(self, account_id: String) -> Result<Vec<Trunk>, RedisError> {
120        get_trunks(self.connection.clone(), &account_id).await
121    }
122
123    /// Retrieves a specific trunk for an account.
124    ///
125    /// # Arguments
126    /// * `account_id` - Account ID
127    /// * `trunk_id` - Trunk ID to retrieve
128    ///
129    /// # Returns
130    /// * `Result<Option<Trunk>, RedisError>` - The trunk if found, None if not found, or a Redis error
131    pub async fn get_trunk(self, account_id: String, trunk_id: String) -> Result<Option<Trunk>, RedisError> {
132        get_trunk(self.connection.clone(), &account_id, &trunk_id).await
133    }
134
135    /// Retrieves all DDIs for an account.
136    ///
137    /// # Arguments
138    /// * `account_id` - Account ID
139    ///
140    /// # Returns
141    /// * `Result<Vec<DDI>, RedisError>` - List of DDIs or a Redis error
142    pub async fn get_ddis(self, account_id: String) -> Result<Vec<DDI>, RedisError> {
143        get_ddis(self.connection.clone(), &account_id).await
144    }
145
146    /// Retrieves a specific DDI for an account.
147    ///
148    /// # Arguments
149    /// * `account_id` - Account ID
150    /// * `ddi_id` - DDI ID to retrieve
151    ///
152    /// # Returns
153    /// * `Result<Option<DDI>, RedisError>` - The DDI if found, None if not found, or a Redis error
154    pub async fn get_ddi(self, account_id: String, ddi_id: String) -> Result<Option<DDI>, RedisError> {
155        get_ddi(self.connection.clone(), &account_id, &ddi_id).await
156    }
157
158    /// Retrieves all hooks for an account.
159    ///
160    /// # Arguments
161    /// * `account_id` - Account ID
162    ///
163    /// # Returns
164    /// * `Result<Vec<Hook>, RedisError>` - List of hooks or a Redis error
165    pub async fn get_hooks(self, account_id: String) -> Result<Vec<Hook>, RedisError> {
166        get_hooks(self.connection.clone(), &account_id).await
167    }
168
169    /// Retrieves a specific hook for an account.
170    ///
171    /// # Arguments
172    /// * `account_id` - Account ID
173    /// * `hook_id` - Hook ID to retrieve
174    ///
175    /// # Returns
176    /// * `Result<Option<Hook>, RedisError>` - The hook if found, None if not found, or a Redis error
177    pub async fn get_hook(self, account_id: String, hook_id: String) -> Result<Option<Hook>, RedisError> {
178        get_hook(self.connection.clone(), &account_id, &hook_id).await
179    }
180
181    /// Retrieves all assets for an account.
182    ///
183    /// # Arguments
184    /// * `account_id` - Account ID
185    ///
186    /// # Returns
187    /// * `Result<Vec<Asset>, RedisError>` - List of assets or a Redis error
188    pub async fn get_assets(self, account_id: String) -> Result<Vec<Asset>, RedisError> {
189        get_assets(self.connection.clone(), &account_id).await
190    }
191
192    /// Retrieves a specific asset for an account.
193    ///
194    /// # Arguments
195    /// * `account_id` - Account ID
196    /// * `asset_id` - Asset ID to retrieve
197    ///
198    /// # Returns
199    /// * `Result<Option<Asset>, RedisError>` - The asset if found, None if not found, or a Redis error
200    pub async fn get_asset(self, account_id: String, asset_id: String) -> Result<Option<Asset>, RedisError> {
201        get_asset(self.connection.clone(), &account_id, &asset_id).await
202    }
203
204    /// Retrieves a region by its identifier.
205    ///
206    /// # Arguments
207    /// * `key` - Region identifier (ID, name, etc.)
208    ///
209    /// # Returns
210    /// * `Result<Option<Region>, RedisError>` - Region if found, None if not found, or a Redis error
211    pub async fn get_region_by_ident(self, key: &str) -> Result<Option<Region>, RedisError> {
212        get_region_by_ident(self.connection.clone(), key).await
213    }
214
215    /// Retrieves a region by its ID.
216    ///
217    /// # Arguments
218    /// * `id` - Region ID
219    ///
220    /// # Returns
221    /// * `Result<Option<Region>, RedisError>` - Region if found, None if not found, or a Redis error
222    pub async fn get_region_by_id(self, id: &str) -> Result<Option<Region>, RedisError> {
223        get_region_by_id(self.connection.clone(), id).await
224    }
225
226    /// Retrieves all regions.
227    ///
228    /// # Returns
229    /// * `Result<HashMap<String, Region>, RedisError>` - Map of region IDs to regions
230    pub async fn get_regions(self) -> Result<Vec<Region>, RedisError> {
231        get_regions(self.connection.clone()).await
232    }
233
234    /// Retrieves a string value from Redis.
235    ///
236    /// # Arguments
237    /// * `key` - Redis key
238    ///
239    /// # Returns
240    /// * `Result<Option<String>, RedisError>` - String value if found, None if not found, or a Redis error
241    pub async fn get_str(self, key: &str) -> Result<Option<String>, RedisError> {
242        get_str(self.connection.clone(), key).await
243    }
244
245    /// Retrieves a field from a Redis hash.
246    ///
247    /// # Arguments
248    /// * `key` - Redis hash key
249    /// * `field` - Field to retrieve
250    ///
251    /// # Returns
252    /// * `Result<Option<String>, RedisError>` - Field value if found, None if not found, or a Redis error
253    pub async fn get_hash(self, key: &str, field: &str) -> Result<Option<String>, RedisError> {
254        get_hash(self.connection.clone(), key, field).await
255    }
256
257    pub async fn insert_user(self, value: User) -> Result<RedisCache, RedisError> {
258        insert_user(self.connection.clone(), value).await?;
259        Ok(self)
260    }
261}