cyfs_bdt/ndn/chunk/cache/raw_cache/
manager.rs1use std::{
2 sync::{Arc, RwLock}
3};
4use cyfs_base::*;
5use super::{
6 common::*,
7 mem::*
8};
9
10
11struct ManagerState {
12 total_mem: u64,
13}
14
15struct ManagerImpl {
16 local: DeviceId,
17 config: RawCacheConfig,
18 state: RwLock<ManagerState>
19}
20
21impl std::fmt::Display for RawCacheManager {
22 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23 write!(f, "RawCache{{local:{}}}", self.0.local)
24 }
25}
26
27#[derive(Clone)]
28pub struct RawCacheManager(Arc<ManagerImpl>);
29
30impl RawCacheManager {
31 pub fn new(local: DeviceId, config: RawCacheConfig) -> Self {
32 Self(Arc::new(ManagerImpl {
33 local,
34 config,
35 state: RwLock::new(ManagerState {
36 total_mem: 0
37 })
38 }))
39 }
40
41 pub fn config(&self) -> &RawCacheConfig {
42 &self.0.config
43 }
44
45 pub async fn alloc(&self, capacity: usize) -> Box<dyn RawCache> {
46 self.alloc_mem(capacity)
48 }
49
50 pub fn used_mem(&self) -> u64 {
51 self.0.state.read().unwrap().total_mem
52 }
53
54 pub fn alloc_mem(&self, capacity: usize) -> Box<dyn RawCache> {
55 info!("{} alloc raw cache {}", self, capacity);
56 self.0.state.write().unwrap().total_mem += capacity as u64;
57 MemCache::new(capacity, Some(self.clone())).clone_as_raw_cache()
58 }
59
60 pub(super) fn release_mem(&self, capacity: usize) {
61 info!("{} release raw cache {}", self, capacity);
62 self.0.state.write().unwrap().total_mem -= capacity as u64;
63 }
64}