use crate::cache::CallableCache;
use crate::{get_device, get_device_by_ident, get_devices};
use cal_core::device::device::DeviceStruct;
use redis::RedisError;
use std::sync::Arc;
impl CallableCache {
pub async fn get_device_by_ident(
self,
account_id: &str,
ident: &str,
) -> Result<Option<Arc<DeviceStruct>>, RedisError> {
println!("[CallableCache::get_device_by_ident] Looking up device - Account: {}, Ident: {}", account_id, ident);
let key = format!("{}:{}", account_id, ident);
if let Some(device_id) = self.local_cache.device_idents.get(&key) {
println!("[CallableCache::get_device_by_ident] Found device_id in local cache: {}", device_id);
if let Some(device) = self.local_cache.devices.get(device_id.as_str()) {
println!("[CallableCache::get_device_by_ident] Found device in local cache");
self.local_cache.devices.insert(device_id, device.clone());
return Ok(Some(device));
}
println!("[CallableCache::get_device_by_ident] Device not in local cache, fetching from Redis");
match get_device(
self.remote_cache.connection.clone(),
account_id,
device_id.as_str(),
)
.await?
{
Some(device) => {
println!("[CallableCache::get_device_by_ident] Found device in Redis: {}", device.name);
self.local_cache
.devices
.insert(device_id, Arc::new(device.clone()));
self.local_cache
.device_idents
.insert(ident.to_string(), device.id.clone());
Ok(Some(Arc::new(device)))
}
None => {
println!("[CallableCache::get_device_by_ident] Device not found in Redis");
Ok(None)
}
}
} else {
println!("[CallableCache::get_device_by_ident] Device ident not in local cache, checking Redis");
match get_device_by_ident(self.remote_cache.connection.clone(), account_id, ident)
.await?
{
Some(device) => {
println!("[CallableCache::get_device_by_ident] Found device in Redis by ident: {}", device.name);
self.local_cache
.devices
.insert(device.id.clone(), Arc::new(device.clone()));
self.local_cache
.device_idents
.insert(device.id.clone().to_string(), device.id.clone());
Ok(Some(Arc::new(device)))
}
None => {
println!("[CallableCache::get_device_by_ident] No device found for ident");
Ok(None)
}
}
}
}
pub async fn get_devices(self, account_id: &str) -> Result<Arc<Vec<DeviceStruct>>, RedisError> {
println!("[CallableCache::get_devices] Getting all devices for account: {}", account_id);
let devices = get_devices(self.remote_cache.connection.clone(), account_id).await?;
println!("[CallableCache::get_devices] Retrieved {} devices from Redis", devices.len());
for device in &devices {
let cache_key = format!("{}:{}", account_id, device.id);
println!("[CallableCache::get_devices] Caching device: {} with key: {}", device.name, cache_key);
self.local_cache
.devices
.insert(cache_key, Arc::new(device.clone()));
}
Ok(Arc::new(devices))
}
pub async fn get_device(
self,
account_id: &str,
device_id: &str,
) -> Result<Option<Arc<DeviceStruct>>, RedisError> {
println!("[CallableCache::get_device] Getting device - Account: {}, Device ID: {}", account_id, device_id);
let cache_key = format!("{}:{}", account_id, device_id);
if let Some(device) = self.local_cache.devices.get(&cache_key) {
println!("[CallableCache::get_device] Found device in local cache");
return Ok(Some(device));
}
println!("[CallableCache::get_device] Device not in local cache, fetching from Redis");
match get_device(self.remote_cache.connection.clone(), account_id, device_id).await? {
Some(device) => {
println!("[CallableCache::get_device] Found device in Redis: {}", device.name);
self.local_cache
.devices
.insert(cache_key, Arc::new(device.clone()));
Ok(Some(Arc::new(device)))
}
None => {
println!("[CallableCache::get_device] Device not found in Redis");
Ok(None)
}
}
}
}