Skip to content

Commit

Permalink
fix: add target and other configs to cache key logic
Browse files Browse the repository at this point in the history
Related to #2139
  • Loading branch information
jdx committed May 19, 2024
1 parent 70d4e92 commit 77e2516
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 26 deletions.
26 changes: 23 additions & 3 deletions src/cache.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -32,7 +34,12 @@ impl<T> CacheManager<T>
where
T: Serialize + DeserializeOwned,
{
pub fn new(cache_file_path: PathBuf) -> Self {
pub fn new(cache_file_path: impl AsRef<Path>) -> 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()),
Expand Down Expand Up @@ -136,6 +143,19 @@ where
}
}

static KEY: Lazy<String> = 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::<Vec<_>>();
hash_to_str(&parts).chars().take(5).collect()
});

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion src/forge/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion src/forge/go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
6 changes: 4 additions & 2 deletions src/forge/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/forge/pipx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/forge/ubi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/core/java.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions src/plugins/core/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/plugins/external_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/external_plugin_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand Down

0 comments on commit 77e2516

Please sign in to comment.