use std::any::TypeId;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use trait_kit::core::{AsyncAutoBuilder, ModuleMeta};
use trait_kit::kit::AsyncKit;
use super::settings_module::SettingsModule;
use super::CacheComponents;
use super::ModuleBuildError;
use crate::infrastructure::oxcache::ConcurrencyController;
pub struct CacheModule;
impl ModuleMeta for CacheModule {
const NAME: &'static str = "cache";
fn dependencies() -> &'static [(&'static str, TypeId)] {
static DEPS: [(&str, TypeId); 1] = [(SettingsModule::NAME, TypeId::of::<SettingsModule>())];
&DEPS
}
}
impl AsyncAutoBuilder for CacheModule {
type Capability = CacheComponents;
type Error = ModuleBuildError;
fn build<'a>(
kit: &'a AsyncKit,
) -> Pin<Box<dyn Future<Output = Result<Self::Capability, Self::Error>> + Send + 'a>> {
Box::pin(async move {
let settings = kit.require::<SettingsModule>()?;
let search_cache = crate::infrastructure::oxcache::create_cache(&settings.cache)
.await
.map_err(|e| ModuleBuildError::CacheInit(e.to_string()))?;
let max_permits = std::cmp::max(1, settings.concurrency.default_team_limit as usize);
let concurrency_controller = Arc::new(ConcurrencyController::new(max_permits));
Ok(CacheComponents {
search_cache,
concurrency_controller,
})
})
}
}