mod disable;
mod memory;
pub mod persistent;
use std::{fmt::Debug, sync::Arc};
use rspack_fs::{IntermediateFileSystem, ReadableFileSystem};
use self::{disable::DisableCache, memory::MemoryCache, persistent::PersistentCache};
use crate::{
Compilation, CompilerOptions, ExperimentCacheOptions,
compilation::build_module_graph::BuildModuleGraphArtifact,
};
#[async_trait::async_trait]
pub trait Cache: Debug + Send + Sync {
async fn before_compile(&mut self, _compilation: &mut Compilation) -> bool {
false
}
async fn after_compile(&mut self, _compilation: &Compilation) {}
async fn before_build_module_graph(&mut self, _make_artifact: &mut BuildModuleGraphArtifact) {}
async fn after_build_module_graph(&mut self, _make_artifact: &BuildModuleGraphArtifact) {}
}
pub fn new_cache(
compiler_path: &str,
compiler_option: Arc<CompilerOptions>,
input_filesystem: Arc<dyn ReadableFileSystem>,
intermediate_filesystem: Arc<dyn IntermediateFileSystem>,
) -> Box<dyn Cache> {
match &compiler_option.experiments.cache {
ExperimentCacheOptions::Disabled => Box::new(DisableCache),
ExperimentCacheOptions::Memory => Box::new(MemoryCache),
ExperimentCacheOptions::Persistent(option) => Box::new(PersistentCache::new(
compiler_path,
option,
compiler_option.clone(),
input_filesystem,
intermediate_filesystem,
)),
}
}