1
//! Pure memory cache.
2

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

            
5
use async_trait::async_trait;
6

            
7
use super::{
8
    device, device_route,
9
    memory::{
10
        device::{Cache as DeviceCache, Options as DeviceOptions},
11
        device_route::{Cache as DeviceRouteCache, Options as DeviceRouteOptions},
12
        network_route::{Cache as NetworkRouteCache, Options as NetworkRouteOptions},
13
    },
14
    network_route, Model,
15
};
16

            
17
/// Pure memory cache.
18
#[derive(Clone)]
19
pub struct Cache {
20
    device: Arc<DeviceCache>,
21
    device_route: Arc<DeviceRouteCache>,
22
    network_route: Arc<NetworkRouteCache>,
23
}
24

            
25
pub struct Options<'a> {
26
    pub device: &'a DeviceOptions,
27
    pub device_route: &'a DeviceRouteOptions,
28
    pub network_route: &'a NetworkRouteOptions,
29
}
30

            
31
impl Cache {
32
    /// Create an instance.
33
10
    pub fn new(opts: &Options, model: &Arc<dyn Model>) -> Self {
34
10
        Cache {
35
10
            device: Arc::new(DeviceCache::new(opts.device, model.clone())),
36
10
            device_route: Arc::new(DeviceRouteCache::new(opts.device_route, model.clone())),
37
10
            network_route: Arc::new(NetworkRouteCache::new(opts.network_route, model.clone())),
38
10
        }
39
10
    }
40
}
41

            
42
#[async_trait]
43
impl super::Cache for Cache {
44
2
    async fn close(&self) -> Result<(), Box<dyn StdError>> {
45
2
        let _ = self.device_route().clear().await;
46
2
        let _ = self.network_route().clear().await;
47
2
        Ok(())
48
4
    }
49

            
50
16530
    fn device(&self) -> &dyn device::DeviceCache {
51
16530
        self.device.as_ref()
52
16530
    }
53

            
54
67914
    fn device_route(&self) -> &dyn device_route::DeviceRouteCache {
55
67914
        self.device_route.as_ref()
56
67914
    }
57

            
58
83
    fn network_route(&self) -> &dyn network_route::NetworkRouteCache {
59
83
        self.network_route.as_ref()
60
83
    }
61
}