Skip to content

Update CachedHttpClient.php #265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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