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

Add --gc flag to clear-cache command and ability to GC vcs/repo caches #10826

Merged
merged 1 commit into from Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/03-cli.md
Expand Up @@ -902,6 +902,10 @@ performance.

Deletes all content from Composer's cache directories.

### Options

* **--gc:** Only run garbage collection, not a full cache clear

## licenses

Lists the name, version and license of every package installed. Use
Expand Down
19 changes: 19 additions & 0 deletions src/Composer/Cache.php
Expand Up @@ -339,6 +339,25 @@ public function gc(int $ttl, int $maxSize)
return false;
}

public function gcVcsCache(int $ttl): bool
{
if ($this->isEnabled()) {
$expire = new \DateTime();
$expire->modify('-'.$ttl.' seconds');

$finder = Finder::create()->in($this->root)->directories()->depth(0)->date('until '.$expire->format('Y-m-d H:i:s'));
foreach ($finder as $file) {
$this->filesystem->removeDirectory($file->getPathname());
}

self::$cacheCollected = true;

return true;
}

return false;
}

/**
* @param string $file
*
Expand Down
30 changes: 27 additions & 3 deletions src/Composer/Command/ClearCacheCommand.php
Expand Up @@ -15,6 +15,7 @@
use Composer\Cache;
use Composer\Factory;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand All @@ -31,6 +32,9 @@ protected function configure(): void
->setName('clear-cache')
->setAliases(array('clearcache', 'cc'))
->setDescription('Clears composer\'s internal package cache.')
->setDefinition(array(
new InputOption('gc', null, InputOption::VALUE_NONE, 'Only run garbage collection, not a full cache clear'),
))
->setHelp(
<<<EOT
The <info>clear-cache</info> deletes all cached packages from composer's
Expand All @@ -55,6 +59,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
);

foreach ($cachePaths as $key => $cachePath) {
// only individual dirs get garbage collected
if ($key === 'cache-dir' && $input->getOption('gc')) {
continue;
}

$cachePath = realpath($cachePath);
if (!$cachePath) {
$io->writeError("<info>Cache directory does not exist ($key): $cachePath</info>");
Expand All @@ -69,11 +78,26 @@ protected function execute(InputInterface $input, OutputInterface $output): int
continue;
}

$io->writeError("<info>Clearing cache ($key): $cachePath</info>");
$cache->clear();
if ($input->getOption('gc')) {
$io->writeError("<info>Garbage-collecting cache ($key): $cachePath</info>");
if ($key === 'cache-files-dir') {
$cache->gc($config->get('cache-files-ttl'), $config->get('cache-files-maxsize'));
} elseif ($key === 'cache-repo-dir') {
$cache->gc($config->get('cache-ttl'), 1024*1024*1024 /* 1GB, this should almost never clear anything that is not outdated */);
} elseif ($key === 'cache-vcs-dir') {
$cache->gcVcsCache($config->get('cache-ttl'));
}
} else {
$io->writeError("<info>Clearing cache ($key): $cachePath</info>");
$cache->clear();
}
}

$io->writeError('<info>All caches cleared.</info>');
if ($input->getOption('gc')) {
$io->writeError('<info>All caches garbage-collected.</info>');
} else {
$io->writeError('<info>All caches cleared.</info>');
}

return 0;
}
Expand Down