caches: allow explicit in-memory caches

This commit is contained in:
Johannes Kirschbauer 2024-11-11 16:45:53 +01:00
parent bfc143a5ac
commit 08a5199bc3
No known key found for this signature in database
5 changed files with 34 additions and 4 deletions

View File

@ -597,6 +597,8 @@ pub struct Flags {
pub argv: Vec<String>,
pub subcommand: DenoSubcommand,
/// Flag
pub in_memory_cache: Option<bool>,
pub frozen_lockfile: Option<bool>,
pub ca_stores: Option<Vec<String>>,
pub ca_data: Option<CaData>,
@ -2821,6 +2823,7 @@ fn run_args(command: Command, top_level: bool) -> Command {
})
.arg(env_file_arg())
.arg(no_code_cache_arg())
.arg(in_memory_cache_arg())
}
fn run_subcommand() -> Command {
@ -3951,6 +3954,13 @@ fn no_clear_screen_arg() -> Arg {
.help_heading(FILE_WATCHING_HEADING)
}
fn in_memory_cache_arg() -> Arg {
Arg::new("in_memory_cache")
.long("in-memory-cache")
.help("Disable all filesystem caches and use in-memory cache only")
.action(ArgAction::SetTrue)
}
fn no_code_cache_arg() -> Arg {
Arg::new("no-code-cache")
.long("no-code-cache")

View File

@ -1685,6 +1685,10 @@ impl CliOptions {
self.flags.code_cache_enabled
}
pub fn in_memory_cache(&self) -> bool {
self.flags.in_memory_cache.unwrap_or(false)
}
pub fn watch_paths(&self) -> Vec<PathBuf> {
let mut full_paths = Vec::new();
if let DenoSubcommand::Run(RunFlags {

17
cli/cache/caches.rs vendored
View File

@ -16,6 +16,7 @@ use super::module_info::MODULE_INFO_CACHE_DB;
use super::node::NODE_ANALYSIS_CACHE_DB;
pub struct Caches {
in_memory: bool,
dir_provider: Arc<DenoDirProvider>,
fmt_incremental_cache_db: OnceCell<CacheDB>,
lint_incremental_cache_db: OnceCell<CacheDB>,
@ -27,8 +28,9 @@ pub struct Caches {
}
impl Caches {
pub fn new(dir: Arc<DenoDirProvider>) -> Self {
pub fn new(dir: Arc<DenoDirProvider>, in_memory: bool) -> Self {
Self {
in_memory,
dir_provider: dir,
fmt_incremental_cache_db: Default::default(),
lint_incremental_cache_db: Default::default(),
@ -41,13 +43,17 @@ impl Caches {
}
fn make_db(
&self,
cell: &OnceCell<CacheDB>,
config: &'static CacheDBConfiguration,
path: Option<PathBuf>,
) -> CacheDB {
cell
.get_or_init(|| {
if let Some(path) = path {
// Return in memory cache if in_memory is true
if self.in_memory {
CacheDB::in_memory(config, crate::version::DENO_VERSION_INFO.deno)
} else if let Some(path) = path {
CacheDB::from_path(
config,
path,
@ -62,6 +68,7 @@ impl Caches {
pub fn fmt_incremental_cache_db(&self) -> CacheDB {
Self::make_db(
self,
&self.fmt_incremental_cache_db,
&INCREMENTAL_CACHE_DB,
self
@ -74,6 +81,7 @@ impl Caches {
pub fn lint_incremental_cache_db(&self) -> CacheDB {
Self::make_db(
self,
&self.lint_incremental_cache_db,
&INCREMENTAL_CACHE_DB,
self
@ -86,6 +94,7 @@ impl Caches {
pub fn dep_analysis_db(&self) -> CacheDB {
Self::make_db(
self,
&self.dep_analysis_db,
&MODULE_INFO_CACHE_DB,
self
@ -98,6 +107,7 @@ impl Caches {
pub fn fast_check_db(&self) -> CacheDB {
Self::make_db(
self,
&self.fast_check_db,
&FAST_CHECK_CACHE_DB,
self
@ -110,6 +120,7 @@ impl Caches {
pub fn node_analysis_db(&self) -> CacheDB {
Self::make_db(
self,
&self.node_analysis_db,
&NODE_ANALYSIS_CACHE_DB,
self
@ -122,6 +133,7 @@ impl Caches {
pub fn type_checking_cache_db(&self) -> CacheDB {
Self::make_db(
self,
&self.type_checking_cache_db,
&TYPE_CHECK_CACHE_DB,
self
@ -134,6 +146,7 @@ impl Caches {
pub fn code_cache_db(&self) -> CacheDB {
Self::make_db(
self,
&self.code_cache_db,
&CODE_CACHE_DB,
self

View File

@ -265,7 +265,10 @@ impl CliFactory {
pub fn caches(&self) -> Result<&Arc<Caches>, AnyError> {
self.services.caches.get_or_try_init(|| {
let cli_options = self.cli_options()?;
let caches = Arc::new(Caches::new(self.deno_dir_provider()?.clone()));
let caches = Arc::new(Caches::new(
self.deno_dir_provider()?.clone(),
cli_options.in_memory_cache(),
));
// Warm up the caches we know we'll likely need based on the CLI mode
match cli_options.sub_command() {
DenoSubcommand::Run(_)

View File

@ -632,7 +632,7 @@ pub async fn run(data: StandaloneData) -> Result<i32, AnyError> {
unstable_detect_cjs: metadata.unstable_config.detect_cjs,
},
));
let cache_db = Caches::new(deno_dir_provider.clone());
let cache_db = Caches::new(deno_dir_provider.clone(), false);
let node_analysis_cache = NodeAnalysisCache::new(cache_db.node_analysis_db());
let cli_node_resolver = Arc::new(CliNodeResolver::new(
cjs_tracker.clone(),