mod disable;
mod memory;
mod mixed;
pub mod persistent;
use std::{fmt::Debug, path::PathBuf, sync::Arc};
use rspack_cacheable::{cacheable, utils::PortablePath, with::As};
use rspack_fs::{IntermediateFileSystem, ReadableFileSystem};
use rspack_paths::Utf8PathBuf;
use self::{
disable::DisableCache, memory::MemoryCache, mixed::MixedCache, persistent::PersistentCache,
};
use crate::{CacheOptions, Compilation, CompilationLogging, CompilerOptions, MaxMemoryGenerations};
#[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, _compilation: &mut Compilation) {}
async fn after_build_module_graph(&mut self, _compilation: &Compilation) {}
async fn before_process_assets(&mut self, _compilation: &mut Compilation) {}
async fn after_process_assets(&mut self, _compilation: &Compilation) {}
fn store_hot_cache(&mut self, _compilation: &mut Compilation) {}
async fn close(&self) {}
}
pub fn create_cache(
compiler_path: &str,
compiler_option: Arc<CompilerOptions>,
input_filesystem: Arc<dyn ReadableFileSystem>,
intermediate_filesystem: Arc<dyn IntermediateFileSystem>,
compilation_logging: CompilationLogging,
) -> Box<dyn Cache> {
if compiler_option.experiments.new_cache.is_enabled() {
return Box::new(DisableCache);
}
match &compiler_option.cache {
CacheOptions::Disabled | CacheOptions::FileSystem(_) => Box::new(DisableCache),
CacheOptions::Memory { .. } => Box::<MemoryCache>::default(),
CacheOptions::Persistent(option) => {
let persistent = PersistentCache::new(
compiler_path,
option,
compiler_option.clone(),
input_filesystem,
intermediate_filesystem,
compilation_logging,
);
Box::new(MixedCache::new(persistent))
}
}
}
pub type BuildDepsOptions = Vec<PathBuf>;
#[cacheable]
#[derive(Debug, Clone, Hash)]
pub enum StorageOptions {
FileSystem {
#[cacheable(with=As<PortablePath>)]
directory: Utf8PathBuf,
},
}
#[derive(Debug, Clone)]
pub struct PersistentCacheOptions {
pub build_dependencies: BuildDepsOptions,
pub version: String,
pub storage: StorageOptions,
pub portable: bool,
pub readonly: bool,
pub max_age: u64,
pub max_memory_generations: MaxMemoryGenerations,
}