Skip to content

Commit

Permalink
Merge pull request #265 from jrean/patch-2
Browse files Browse the repository at this point in the history
Update CachedHttpClient.php
  • Loading branch information
pilot committed May 5, 2015
2 parents 048231c + ec371af commit b53ff0c
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/Github/HttpClient/CachedHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ class CachedHttpClient extends HttpClient
*/
private $lastCachedResponse;

/**
* Identifier used for the cache file(s).
* $path + encoded query parameter(s) if they exist.
*
* @var string
*/
private $id;

/**
* @return CacheInterface
*/
Expand Down Expand Up @@ -54,13 +62,13 @@ public function request($path, $body = null, $httpMethod = 'GET', array $headers
$response = parent::request($path, $body, $httpMethod, $headers, $options);

if (304 == $response->getStatusCode()) {
$cacheResponse = $this->getCache()->get($path);
$cacheResponse = $this->getCache()->get($this->id);
$this->lastCachedResponse = $cacheResponse;

return $cacheResponse;
}

$this->getCache()->set($path, $response);
$this->getCache()->set($this->id, $response);

return $response;
}
Expand All @@ -73,8 +81,14 @@ public function request($path, $body = null, $httpMethod = 'GET', array $headers
protected function createRequest($httpMethod, $path, $body = null, array $headers = array(), array $options = array())
{
$request = parent::createRequest($httpMethod, $path, $body, $headers, $options);

$this->id = $path;

if (array_key_exists('query', $options) && !empty($options['query'])) {
$this->id .= '?' . $request->getQuery();
}

if ($modifiedAt = $this->getCache()->getModifiedSince($path)) {
if ($modifiedAt = $this->getCache()->getModifiedSince($this->id)) {
$modifiedAt = new \DateTime('@'.$modifiedAt);
$modifiedAt->setTimezone(new \DateTimeZone('GMT'));

Expand All @@ -83,7 +97,7 @@ protected function createRequest($httpMethod, $path, $body = null, array $header
sprintf('%s GMT', $modifiedAt->format('l, d-M-y H:i:s'))
);
}
if ($etag = $this->getCache()->getETag($path)) {
if ($etag = $this->getCache()->getETag($this->id)) {
$request->addHeader(
'If-None-Match',
$etag
Expand Down

0 comments on commit b53ff0c

Please sign in to comment.