From ef3797cdd686a854e8a6dea262aa795535843b0b Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 4 May 2020 21:58:33 +0200 Subject: [PATCH] Cache successful requests to make sure subsequent loadPackages calls do not do the same requests for nothing --- src/Composer/Repository/ComposerRepository.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Composer/Repository/ComposerRepository.php b/src/Composer/Repository/ComposerRepository.php index 5f68de635..34584e2db 100644 --- a/src/Composer/Repository/ComposerRepository.php +++ b/src/Composer/Repository/ComposerRepository.php @@ -70,6 +70,14 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito private $hasPartialPackages; private $partialPackagesByName; + /** + * TODO v3 should make this private once we can drop PHP 5.3 support + * @private + * @var array list of package names which are fresh and can be loaded from the cache directly in case loadPackage is called several times + * useful for v2 metadata repositories with lazy providers + */ + public $freshMetadataUrls = array(); + /** * TODO v3 should make this private once we can drop PHP 5.3 support * @private @@ -1150,6 +1158,11 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito return new Promise(function ($resolve, $reject) { $resolve(array('packages' => array())); }); } + if (isset($this->freshMetadataUrls[$filename]) && $lastModifiedTime) { + // make it look like we got a 304 response + return new Promise(function ($resolve, $reject) { $resolve(true); }); + } + $httpDownloader = $this->httpDownloader; if ($this->eventDispatcher) { $preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $this->httpDownloader, $filename); @@ -1173,6 +1186,7 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito $json = $response->getBody(); if ($json === '' && $response->getStatusCode() === 304) { + $repo->freshMetadataUrls[$filename] = true; return true; } @@ -1186,6 +1200,7 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito $json = JsonFile::encode($data, JsonFile::JSON_UNESCAPED_SLASHES | JsonFile::JSON_UNESCAPED_UNICODE); } $cache->write($cacheKey, $json); + $repo->freshMetadataUrls[$filename] = true; return $data; };