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
use std::{
    sync::{Arc, RwLock}
};
use super::{
    common::*,
    mem::*
};


struct ManagerState {
    total_mem: u64, 
}

struct ManagerImpl {
    config: RawCacheConfig, 
    state: RwLock<ManagerState>
}

#[derive(Clone)]
pub struct RawCacheManager(Arc<ManagerImpl>);

impl RawCacheManager {
    pub fn new(config: RawCacheConfig) -> Self {
        Self(Arc::new(ManagerImpl {
            config, 
            state: RwLock::new(ManagerState {
                total_mem: 0
            })
        }))
    }

    pub fn config(&self) -> &RawCacheConfig {
        &self.0.config
    }

    pub async fn alloc(&self, capacity: usize) -> Box<dyn RawCache> {
        // FIXME: create file cache when outof config 
        self.alloc_mem(capacity)
    }

    pub fn alloc_mem(&self, capacity: usize) -> Box<dyn RawCache> {
        self.0.state.write().unwrap().total_mem += capacity as u64;
        MemCache::new(capacity, Some(self.clone())).clone_as_raw_cache()
    }

    pub(super) fn release_mem(&self, capacity: usize) {
        self.0.state.write().unwrap().total_mem -= capacity as u64;
    }
}