Skip to content

Commit

Permalink
Add --gc flag to cache-cache command and ability to GC vcs/repo caches,
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek authored and emahorvat52 committed Jan 18, 2023
1 parent 5eb954e commit 929971c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
4 changes: 4 additions & 0 deletions doc/03-cli.md
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions src/Composer/Cache.php
Expand Up @@ -342,6 +342,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

0 comments on commit 929971c

Please sign in to comment.