Skip to content

Commit

Permalink
feat: Implement --clean flag for forced cache clearing
Browse files Browse the repository at this point in the history
Introduce the `--clean` flag, enabling users
to explicitly command the program to purge the existing cache.

This enhancement ensures more control over cache management,
facilitating immediate cache clearance without disrupting the workflow.
  • Loading branch information
Miyoshi-Ryota committed Feb 19, 2024
1 parent 49607d3 commit e592f75
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
33 changes: 26 additions & 7 deletions src/lib.rs
Expand Up @@ -14,7 +14,12 @@ pub fn hash<T: Hash>(t: &T) -> u64 {

/// This function executes the given command and caches the result if the cache is expired or not exists.
/// If the cache is not expired, it just returns the cached result without execution command.
pub fn cache_aware_execute_command(command: &str, ttl: u64, cache_file: &PathBuf) -> String {
pub fn cache_aware_execute_command(
command: &str,
ttl: u64,
cache_file: &PathBuf,
does_force_renew_cache: bool,
) -> String {
if cache_file.exists() && cache_file.is_file() {
let metadata = fs::metadata(cache_file).expect("Unable to read metadata of cache file");
let created = metadata
Expand All @@ -24,7 +29,7 @@ pub fn cache_aware_execute_command(command: &str, ttl: u64, cache_file: &PathBuf
let elapsed = now
.duration_since(created)
.expect("Unable to calculate elapsed time");
if elapsed.as_secs() < ttl {
if elapsed.as_secs() < ttl && !does_force_renew_cache {
return String::from_utf8_lossy(
&fs::read(cache_file).expect("Unable to read cache file"),
)
Expand Down Expand Up @@ -94,7 +99,7 @@ mod tests {

let start = Instant::now(); // Start timing

let result = super::cache_aware_execute_command(command, ttl, &cache_file);
let result = super::cache_aware_execute_command(command, ttl, &cache_file, false);
assert_eq!(result, "not hello");

let duration = start.elapsed(); // Measure how long it took
Expand All @@ -116,7 +121,7 @@ mod tests {

let start = Instant::now(); // Start timing

let result = super::cache_aware_execute_command(command, ttl, &cache_file);
let result = super::cache_aware_execute_command(command, ttl, &cache_file, false);
assert_eq!(result, "hello\n");

let duration = start.elapsed(); // Measure how long it took
Expand All @@ -140,7 +145,7 @@ mod tests {
sleep(Duration::from_secs(1));
let start = Instant::now(); // Start timing

let result = super::cache_aware_execute_command(command, ttl, &cache_file);
let result = super::cache_aware_execute_command(command, ttl, &cache_file, false);
assert_eq!(result, "hello\n");

let duration = start.elapsed(); // Measure how long it took
Expand Down Expand Up @@ -170,17 +175,31 @@ mod tests {

sleep(Duration::from_secs(2));

let _ = super::cache_aware_execute_command(command, ttl, &cache_file);
let _ = super::cache_aware_execute_command(command, ttl, &cache_file, false);

let renewed_cache_file_created = fs::metadata(&cache_file)
.expect("Unable to read metadata of cache file")
.created()
.expect("Unable to read created date of cache file");


assert!(
renewed_cache_file_created > old_cache_file_created,
"Cache file is not renewed"
);
}

#[test]
fn force_renew_cache() {
let ctx = TestContext::new(&format!("{}{}", file!(), line!()));

let cache_file = ctx.cache_root_path.join("test_cache");
let _ = std::fs::write(&cache_file, "not hello").expect("Unable to write cache file");

let command = "sleep 1 && echo 'hello'";
let ttl = 60;

let result = super::cache_aware_execute_command(command, ttl, &cache_file, true);

assert!(result == "hello\n", "Cache is not renewed: {:?}", result);
}
}
11 changes: 9 additions & 2 deletions src/main.rs
Expand Up @@ -10,6 +10,11 @@ struct Args {
#[arg(short, long, default_value = "3600")]
ttl: u64,

/// Clean a cache and re-execute the command.
/// Of-cause the result will be cached again.
#[arg(short = 'c', long = "clean")]
force_renew_cache: bool,

/// Target cli command to cache.
/// This argument should be quoted if it contains spaces.
/// For example, 'sleep 10 && date'
Expand All @@ -19,7 +24,8 @@ struct Args {
fn main() {
let args = Args::parse();
let ttl = args.ttl;
let command = args.command;
let command: String = args.command;
let does_force_renew_cache = args.force_renew_cache;

let cache_root_dir = env::temp_dir().join("fclicache/caches");
if !cache_root_dir.exists() {
Expand All @@ -36,7 +42,8 @@ fn main() {
cache_aware_execute_command(
&command,
ttl,
&cache_root_dir.join(hash(&command).to_string())
&cache_root_dir.join(hash(&command).to_string()),
does_force_renew_cache,
)
);
}

0 comments on commit e592f75

Please sign in to comment.