فهرست منبع

Add caching to bitbucket API requests

Jordi Boggiano 9 سال پیش
والد
کامیت
764de14b5e
2فایلهای تغییر یافته به همراه24 افزوده شده و 0 حذف شده
  1. 12 0
      src/Composer/Repository/Vcs/GitBitbucketDriver.php
  2. 12 0
      src/Composer/Repository/Vcs/HgBitbucketDriver.php

+ 12 - 0
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;
         }
 

+ 12 - 0
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;
         }