use std::borrow::Cow; use std::sync::Arc; use crate::cache::config::DEFAULT_LFS_NAME; use crate::cache::stats::CacheStats; use crate::cache::{CacheConfig, CacheStore}; use crate::error::BabyError; pub struct CachePools { store: Arc, lfs: Option>, } impl CachePools { pub async fn open(cfg: CacheConfig) -> Result, BabyError> { let store = CacheStore::open(cfg.clone()).await?; let lfs = match &cfg.lfs { Some(lfs_cfg) => { let lfs_cfg = CacheConfig { memory_capacity_bytes: lfs_cfg.memory_capacity_bytes, disk_capacity_bytes: lfs_cfg.disk_capacity_bytes, disk_dir: cfg.disk_dir.join("lfs"), name: Cow::Owned(DEFAULT_LFS_NAME.to_owned()), lfs: None, }; Some(CacheStore::open(lfs_cfg).await?) } None => None, }; Ok(Arc::new(Self { store, lfs })) } pub async fn close(&self) -> Result<(), BabyError> { self.store.close().await?; if let Some(lfs) = &self.lfs { lfs.close().await?; } Ok(()) } /// Drop all entries in the main object/ref store. The LFS store is /// content-addressed by LFS oid and unaffected by git object/ref churn, so /// it is left intact. pub async fn clear(&self) -> Result<(), BabyError> { self.store.clear().await } pub fn store(&self) -> &Arc { &self.store } pub fn lfs(&self) -> Option<&Arc> { self.lfs.as_ref() } pub fn stats(&self) -> CacheStats { let s = self.store.statistics(); CacheStats { name: self.store.config().name.to_string(), disk_write_bytes: s.disk_write_bytes() as u64, disk_read_bytes: s.disk_read_bytes() as u64, disk_write_ios: s.disk_write_ios() as u64, disk_read_ios: s.disk_read_ios() as u64, } } }