diff --git a/src/cache.rs b/src/cache.rs index 67e3d3d24..d1fe42433 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -1,19 +1,21 @@ use std::cmp::min; use std::fs::File; use std::io::{Read, Write}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::time::Duration; use eyre::Result; use flate2::read::ZlibDecoder; use flate2::write::ZlibEncoder; use flate2::Compression; -use once_cell::sync::OnceCell; +use once_cell::sync::{Lazy, OnceCell}; use serde::de::DeserializeOwned; use serde::Serialize; +use crate::build_time::built_info; use crate::file; use crate::file::{display_path, modified_duration}; +use crate::hash::hash_to_str; use crate::rand::random_string; #[derive(Debug, Clone)] @@ -32,7 +34,12 @@ impl CacheManager where T: Serialize + DeserializeOwned, { - pub fn new(cache_file_path: PathBuf) -> Self { + pub fn new(cache_file_path: impl AsRef) -> Self { + // "replace $KEY in path with key() + let cache_file_path = regex!(r#"\$KEY"#) + .replace_all(cache_file_path.as_ref().to_str().unwrap(), &*KEY) + .to_string() + .into(); Self { cache_file_path, cache: Box::new(OnceCell::new()), @@ -136,6 +143,19 @@ where } } +static KEY: Lazy = Lazy::new(|| { + let parts = vec![ + built_info::FEATURES_STR, + //built_info::PKG_VERSION, # TODO: put this in when we autoclean cache (#2139) + built_info::PROFILE, + built_info::RUSTC_VERSION, + built_info::TARGET, + ] + .into_iter() + .collect::>(); + hash_to_str(&parts).chars().take(5).collect() +}); + #[cfg(test)] mod tests { use super::*; diff --git a/src/forge/cargo.rs b/src/forge/cargo.rs index c8e803e91..f74837552 100644 --- a/src/forge/cargo.rs +++ b/src/forge/cargo.rs @@ -81,7 +81,7 @@ impl CargoForge { let fa = ForgeArg::new(ForgeType::Cargo, &name); Self { remote_version_cache: CacheManager::new( - fa.cache_path.join("remote_versions.msgpack.z"), + fa.cache_path.join("remote_versions-$KEY.msgpack.z"), ), fa, } diff --git a/src/forge/go.rs b/src/forge/go.rs index 21587f1e2..4d1e4ab52 100644 --- a/src/forge/go.rs +++ b/src/forge/go.rs @@ -87,7 +87,7 @@ impl GoForge { let fa = ForgeArg::new(ForgeType::Go, &name); Self { remote_version_cache: CacheManager::new( - fa.cache_path.join("remote_versions.msgpack.z"), + fa.cache_path.join("remote_versions-$KEY.msgpack.z"), ), fa, } diff --git a/src/forge/npm.rs b/src/forge/npm.rs index 1a1df8b52..f49b1b133 100644 --- a/src/forge/npm.rs +++ b/src/forge/npm.rs @@ -80,9 +80,11 @@ impl NPMForge { let fa = ForgeArg::new(ForgeType::Npm, &name); Self { remote_version_cache: CacheManager::new( - fa.cache_path.join("remote_versions.msgpack.z"), + fa.cache_path.join("remote_versions-$KEY.msgpack.z"), + ), + latest_version_cache: CacheManager::new( + fa.cache_path.join("latest_version-$KEY.msgpack.z"), ), - latest_version_cache: CacheManager::new(fa.cache_path.join("latest_version.msgpack.z")), fa, } } diff --git a/src/forge/pipx.rs b/src/forge/pipx.rs index a56bd85ed..6e6b85489 100644 --- a/src/forge/pipx.rs +++ b/src/forge/pipx.rs @@ -95,9 +95,11 @@ impl PIPXForge { let fa = ForgeArg::new(ForgeType::Pipx, &name); Self { remote_version_cache: CacheManager::new( - fa.cache_path.join("remote_versions.msgpack.z"), + fa.cache_path.join("remote_versions-$KEY.msgpack.z"), + ), + latest_version_cache: CacheManager::new( + fa.cache_path.join("latest_version-$KEY.msgpack.z"), ), - latest_version_cache: CacheManager::new(fa.cache_path.join("latest_version.msgpack.z")), fa, } } diff --git a/src/forge/ubi.rs b/src/forge/ubi.rs index 176ce1e2f..f44dc658e 100644 --- a/src/forge/ubi.rs +++ b/src/forge/ubi.rs @@ -82,7 +82,7 @@ impl UbiForge { let fa = ForgeArg::new(ForgeType::Ubi, &name); Self { remote_version_cache: CacheManager::new( - fa.cache_path.join("remote_versions.msgpack.z"), + fa.cache_path.join("remote_versions-$KEY.msgpack.z"), ), fa, } diff --git a/src/plugins/core/java.rs b/src/plugins/core/java.rs index e380229d1..5383dfaa2 100644 --- a/src/plugins/core/java.rs +++ b/src/plugins/core/java.rs @@ -35,9 +35,9 @@ impl JavaPlugin { pub fn new() -> Self { let core = CorePlugin::new("java"); let java_metadata_ga_cache_filename = - format!("java_metadata_ga_{}_{}.msgpack.z", os(), arch()); + format!("java_metadata_ga_{}_{}-$KEY.msgpack.z", os(), arch()); let java_metadata_ea_cache_filename = - format!("java_metadata_ea_{}_{}.msgpack.z", os(), arch()); + format!("java_metadata_ea_{}_{}-$KEY.msgpack.z", os(), arch()); Self { java_metadata_ea_cache: CacheManager::new( core.fa.cache_path.join(java_metadata_ea_cache_filename), diff --git a/src/plugins/core/mod.rs b/src/plugins/core/mod.rs index abae2bdeb..b2e9e9de1 100644 --- a/src/plugins/core/mod.rs +++ b/src/plugins/core/mod.rs @@ -65,7 +65,7 @@ impl CorePlugin { Self { name, remote_version_cache: CacheManager::new( - fa.cache_path.join("remote_versions.msgpack.z"), + fa.cache_path.join("remote_versions-$KEY.msgpack.z"), ) .with_fresh_duration(*env::MISE_FETCH_REMOTE_VERSIONS_CACHE), fa, diff --git a/src/plugins/core/python.rs b/src/plugins/core/python.rs index 63624a61a..71fdd0827 100644 --- a/src/plugins/core/python.rs +++ b/src/plugins/core/python.rs @@ -28,8 +28,10 @@ impl PythonPlugin { pub fn new() -> Self { let core = CorePlugin::new("python"); Self { - precompiled_cache: CacheManager::new(core.fa.cache_path.join("precompiled.msgpack.z")) - .with_fresh_duration(*env::MISE_FETCH_REMOTE_VERSIONS_CACHE), + precompiled_cache: CacheManager::new( + core.fa.cache_path.join("precompiled-$KEY.msgpack.z"), + ) + .with_fresh_duration(*env::MISE_FETCH_REMOTE_VERSIONS_CACHE), core, } } diff --git a/src/plugins/external_plugin.rs b/src/plugins/external_plugin.rs index a943a3ad7..2eb64285a 100644 --- a/src/plugins/external_plugin.rs +++ b/src/plugins/external_plugin.rs @@ -65,20 +65,22 @@ impl ExternalPlugin { script_man: build_script_man(&name, &plugin_path), cache: ExternalPluginCache::default(), remote_version_cache: CacheManager::new( - fa.cache_path.join("remote_versions.msgpack.z"), + fa.cache_path.join("remote_versions-$KEY.msgpack.z"), ) .with_fresh_duration(*env::MISE_FETCH_REMOTE_VERSIONS_CACHE) .with_fresh_file(plugin_path.clone()) .with_fresh_file(plugin_path.join("bin/list-all")), - latest_stable_cache: CacheManager::new(fa.cache_path.join("latest_stable.msgpack.z")) - .with_fresh_duration(*env::MISE_FETCH_REMOTE_VERSIONS_CACHE) - .with_fresh_file(plugin_path.clone()) - .with_fresh_file(plugin_path.join("bin/latest-stable")), - alias_cache: CacheManager::new(fa.cache_path.join("aliases.msgpack.z")) + latest_stable_cache: CacheManager::new( + fa.cache_path.join("latest_stable-$KEY.msgpack.z"), + ) + .with_fresh_duration(*env::MISE_FETCH_REMOTE_VERSIONS_CACHE) + .with_fresh_file(plugin_path.clone()) + .with_fresh_file(plugin_path.join("bin/latest-stable")), + alias_cache: CacheManager::new(fa.cache_path.join("aliases-$KEY.msgpack.z")) .with_fresh_file(plugin_path.clone()) .with_fresh_file(plugin_path.join("bin/list-aliases")), legacy_filename_cache: CacheManager::new( - fa.cache_path.join("legacy_filenames.msgpack.z"), + fa.cache_path.join("legacy_filenames-$KEY.msgpack.z"), ) .with_fresh_file(plugin_path.clone()) .with_fresh_file(plugin_path.join("bin/list-legacy-filenames")), diff --git a/src/plugins/external_plugin_cache.rs b/src/plugins/external_plugin_cache.rs index 27b368815..d15254fd9 100644 --- a/src/plugins/external_plugin_cache.rs +++ b/src/plugins/external_plugin_cache.rs @@ -33,10 +33,10 @@ impl ExternalPluginCache { Some(key) => { let config = Config::get(); let key = render_cache_key(&config, tv, key); - let filename = format!("{}.msgpack.z", key); + let filename = format!("{}-$KEY.msgpack.z", key); tv.cache_path().join("list_bin_paths").join(filename) } - None => tv.cache_path().join("list_bin_paths.msgpack.z"), + None => tv.cache_path().join("list_bin_paths-$KEY.msgpack.z"), }; CacheManager::new(list_bin_paths_filename) .with_fresh_file(dirs::DATA.to_path_buf()) @@ -61,10 +61,10 @@ impl ExternalPluginCache { let exec_env_filename = match &plugin.toml.exec_env.cache_key { Some(key) => { let key = render_cache_key(config, tv, key); - let filename = format!("{}.msgpack.z", key); + let filename = format!("{}-$KEY.msgpack.z", key); tv.cache_path().join("exec_env").join(filename) } - None => tv.cache_path().join("exec_env.msgpack.z"), + None => tv.cache_path().join("exec_env-$KEY.msgpack.z"), }; CacheManager::new(exec_env_filename) .with_fresh_file(dirs::DATA.to_path_buf())