From 764de14b5eb2980ba9d479865b075ef3bf858600 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 7 Jan 2016 14:56:48 +0000 Subject: [PATCH] Add caching to bitbucket API requests --- src/Composer/Repository/Vcs/GitBitbucketDriver.php | 12 ++++++++++++ src/Composer/Repository/Vcs/HgBitbucketDriver.php | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Composer/Repository/Vcs/GitBitbucketDriver.php b/src/Composer/Repository/Vcs/GitBitbucketDriver.php index 199fc48c8..0f0a57c47 100644 --- a/src/Composer/Repository/Vcs/GitBitbucketDriver.php +++ b/src/Composer/Repository/Vcs/GitBitbucketDriver.php @@ -12,6 +12,7 @@ namespace Composer\Repository\Vcs; +use Composer\Cache; use Composer\Config; use Composer\Json\JsonFile; use Composer\IO\IOInterface; @@ -21,6 +22,7 @@ use Composer\IO\IOInterface; */ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface { + protected $cache; protected $owner; protected $repository; protected $tags; @@ -37,6 +39,7 @@ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface $this->owner = $match[1]; $this->repository = $match[2]; $this->originUrl = 'bitbucket.org'; + $this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.$this->originUrl.'/'.$this->owner.'/'.$this->repository); } /** @@ -84,6 +87,10 @@ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface */ public function getComposerInformation($identifier) { + if (preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier)) { + $this->infoCache[$identifier] = JsonFile::parseJson($res); + } + if (!isset($this->infoCache[$identifier])) { $resource = $this->getScheme() . '://bitbucket.org/'.$this->owner.'/'.$this->repository.'/raw/'.$identifier.'/composer.json'; $composer = $this->getContents($resource); @@ -98,6 +105,11 @@ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface $changeset = JsonFile::parseJson($this->getContents($resource), $resource); $composer['time'] = $changeset['timestamp']; } + + if (preg_match('{[a-f0-9]{40}}i', $identifier)) { + $this->cache->write($identifier, json_encode($composer)); + } + $this->infoCache[$identifier] = $composer; } diff --git a/src/Composer/Repository/Vcs/HgBitbucketDriver.php b/src/Composer/Repository/Vcs/HgBitbucketDriver.php index 569b83521..3beeee440 100644 --- a/src/Composer/Repository/Vcs/HgBitbucketDriver.php +++ b/src/Composer/Repository/Vcs/HgBitbucketDriver.php @@ -12,6 +12,7 @@ namespace Composer\Repository\Vcs; +use Composer\Cache; use Composer\Config; use Composer\Json\JsonFile; use Composer\IO\IOInterface; @@ -21,6 +22,7 @@ use Composer\IO\IOInterface; */ class HgBitbucketDriver extends VcsDriver { + protected $cache; protected $owner; protected $repository; protected $tags; @@ -37,6 +39,7 @@ class HgBitbucketDriver extends VcsDriver $this->owner = $match[1]; $this->repository = $match[2]; $this->originUrl = 'bitbucket.org'; + $this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.$this->originUrl.'/'.$this->owner.'/'.$this->repository); } /** @@ -87,6 +90,10 @@ class HgBitbucketDriver extends VcsDriver */ public function getComposerInformation($identifier) { + if (preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier)) { + $this->infoCache[$identifier] = JsonFile::parseJson($res); + } + if (!isset($this->infoCache[$identifier])) { $resource = $this->getScheme() . '://bitbucket.org/api/1.0/repositories/'.$this->owner.'/'.$this->repository.'/src/'.$identifier.'/composer.json'; $repoData = JsonFile::parseJson($this->getContents($resource), $resource); @@ -107,6 +114,11 @@ class HgBitbucketDriver extends VcsDriver $changeset = JsonFile::parseJson($this->getContents($resource), $resource); $composer['time'] = $changeset['timestamp']; } + + if (preg_match('{[a-f0-9]{40}}i', $identifier)) { + $this->cache->write($identifier, json_encode($composer)); + } + $this->infoCache[$identifier] = $composer; }