Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: cache flush safety & cache flush command #118

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

chr15k
Copy link
Contributor

@chr15k chr15k commented Jan 16, 2025

What:

  • Bug Fix
  • New Feature

Description:

The issue is if you provide a custom cache directory, the flush method will attempt to delete everything from it, potentially even unrelated files. One solution would be to namespace the cache, but in this case I've kept it simple and just added a default prefix peck_* to the md5 cache filename.

Note

The only potential issue with this approach is having stale pre-existing cache. So I've added a prefix option to the flush command.

To achieve the existing flush behaviour (clear all regardless of prefix), you can provide an empty string as the prefix option (bin/peck cache:clear --prefix=)

Changes:

  • A new cache:clear command clears cached files in the default or user-specified directory.
  • Cached files now use a default peck_* prefix, ensuring only relevant files are deleted in custom directories.
  • Cache version/validation logic is decoupled from Cache::default(), improving maintainability.

Usage:

# default dir
bin/peck cache:clear

# custom dir / prefix
bin/peck cache:clear --directory=/tmp/.peck.cache --prefix=peck_

Copy link
Contributor

@benjam-es benjam-es left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR makes sense and the code looks good to me

Copy link
Collaborator

@c0nst4ntin c0nst4ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chr15k Good Contribution! I really like the number of test you added for the feature.

I left some change requests and quite a few questions to make sure I understand everything the way you intended. 

It would be incredible, if you could also extend the README to document the new command. That way everybody could imideatly see the command and use it.

Before this, Nuno was unsure about adding new commands. This however makes sense to me. The final decision lies with him.

I will mark this as a draft for now. Please mark it as "ready for review" once you looked at the feedback and implemented some of the changes.

composer.json Outdated Show resolved Hide resolved
src/Console/Commands/Cache/ClearCommand.php Outdated Show resolved Hide resolved

try {
if (is_string($directory) && ! is_dir($directory)) {
throw new Exception('The specified cache directory does not exist.');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is the only exception that can be thrown, I would prefer to remove the try-catch and instead directly have an early return and $output->writeline()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good spot, it is the only exception in this case; I've removed it. Thanks.

}

match (is_string($directory)) {
true => Cache::create($directory, $prefix)->flush(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This expects the $directory to be absolute correct, right? I think everywhere else we use a relative path. Feel free to correct me on this.

Either way, you could maybe explain how the path needs to be passed in the configure method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeh good point, perhaps we can ensure the custom directory is always inside {project_root}/.peck.cache/?

So if you pass a custom dir, it's {project_root}/.peck.cache/{directory} ?

And the config method can state something like: 'The cache directory path (relative to project root)'


$commandTester = new CommandTester($command);

$commandTester->execute([]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit unsure about this, as this "interacts with"/changes the files outside the tests directory.
But I guess it is the only way to test this.

$output = $commandTester->getDisplay();

expect(trim($output))->toContain('Clearing cache...');
expect(trim($output))->toContain('Cache successfully cleared!');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should also check if the files where deleted? Or do we leave that to the unit test of the Cache class?

$commandTester = new CommandTester($command);

if (! is_dir('/tmp/.peck.cache')) {
@mkdir('/tmp/.peck.cache');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you check if this directory was created?

If it was not created there is a permission problem. I am unsure how the test behaves in that scenario, when we ignore the errors.

'--prefix' => 'pecker_',
]);

expect(count(glob('/tmp/peck_custom/*')))->toBe(2);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would also pass if the --prefix was ignored and the default was used (as the number of files for both prefixes are the same). If possible also assert the file names.

@c0nst4ntin c0nst4ntin marked this pull request as draft January 18, 2025 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants