1
0
Fork 0

Add --gc flag to cache-cache command and ability to GC vcs/repo caches, fixes #7834 (#10826)

pull/10835/head
Jordi Boggiano 2022-06-09 11:46:00 +02:00 committed by GitHub
parent 0fd845eeaf
commit 6186d0c1d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 3 deletions

View File

@ -926,6 +926,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

View File

@ -339,6 +339,25 @@ class Cache
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
*

View File

@ -15,6 +15,7 @@ namespace Composer\Command;
use Composer\Cache;
use Composer\Factory;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
@ -31,6 +32,9 @@ class ClearCacheCommand extends BaseCommand
->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
@ -55,6 +59,11 @@ EOT
);
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>");
@ -69,11 +78,26 @@ EOT
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;
}