sylvia_iot_broker/models/
cache_memory.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! Pure memory cache.

use std::{error::Error as StdError, sync::Arc};

use async_trait::async_trait;

use super::{
    device, device_route,
    memory::{
        device::{Cache as DeviceCache, Options as DeviceOptions},
        device_route::{Cache as DeviceRouteCache, Options as DeviceRouteOptions},
        network_route::{Cache as NetworkRouteCache, Options as NetworkRouteOptions},
    },
    network_route, Model,
};

/// Pure memory cache.
#[derive(Clone)]
pub struct Cache {
    device: Arc<DeviceCache>,
    device_route: Arc<DeviceRouteCache>,
    network_route: Arc<NetworkRouteCache>,
}

pub struct Options<'a> {
    pub device: &'a DeviceOptions,
    pub device_route: &'a DeviceRouteOptions,
    pub network_route: &'a NetworkRouteOptions,
}

impl Cache {
    /// Create an instance.
    pub fn new(opts: &Options, model: &Arc<dyn Model>) -> Self {
        Cache {
            device: Arc::new(DeviceCache::new(opts.device, model.clone())),
            device_route: Arc::new(DeviceRouteCache::new(opts.device_route, model.clone())),
            network_route: Arc::new(NetworkRouteCache::new(opts.network_route, model.clone())),
        }
    }
}

#[async_trait]
impl super::Cache for Cache {
    async fn close(&self) -> Result<(), Box<dyn StdError>> {
        let _ = self.device_route().clear().await;
        let _ = self.network_route().clear().await;
        Ok(())
    }

    fn device(&self) -> &dyn device::DeviceCache {
        self.device.as_ref()
    }

    fn device_route(&self) -> &dyn device_route::DeviceRouteCache {
        self.device_route.as_ref()
    }

    fn network_route(&self) -> &dyn network_route::NetworkRouteCache {
        self.network_route.as_ref()
    }
}