Skip to content

Commit b213498

Browse files
committed
Merge pull request KnpLabs#101 from KnpLabs/feature/etag
use ETag to decrease rate limit hits
2 parents acd9db4 + 9e22b44 commit b213498

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

lib/Github/HttpClient/Cache/CacheInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,27 @@
1111
*/
1212
interface CacheInterface
1313
{
14+
/**
15+
* @param string $id The id of the cached resource
16+
*
17+
* @return bool if present
18+
*/
19+
public function has($id);
20+
1421
/**
1522
* @param string $id The id of the cached resource
1623
*
1724
* @return null|integer The modified since timestamp
1825
*/
1926
public function getModifiedSince($id);
2027

28+
/**
29+
* @param string $id The id of the cached resource
30+
*
31+
* @return null|string The ETag value
32+
*/
33+
public function getETag($id);
34+
2135
/**
2236
* @param string $id The id of the cached resource
2337
*

lib/Github/HttpClient/Cache/FilesystemCache.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,34 @@ public function set($id, Response $response)
4343
if (false === @file_put_contents($this->getPath($id), serialize($response))) {
4444
throw new \InvalidArgumentException(sprintf('Cannot put content in file "%s"', $this->getPath($id)));
4545
}
46+
if (false === @file_put_contents($this->getPath($id).'.etag', $response->getHeader('ETag'))) {
47+
throw new \InvalidArgumentException(sprintf('Cannot put content in file "%s"', $this->getPath($id).'.etag'));
48+
}
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public function has($id)
55+
{
56+
return file_exists($this->getPath($id));
4657
}
4758

4859
/**
4960
* {@inheritdoc}
5061
*/
5162
public function getModifiedSince($id)
5263
{
53-
if (file_exists($this->getPath($id))) {
64+
if ($this->has($id)) {
5465
return filemtime($this->getPath($id));
5566
}
67+
}
5668

57-
return null;
69+
public function getETag($id)
70+
{
71+
if (file_exists($this->getPath($id).'.etag')) {
72+
return file_get_contents($this->getPath($id).'.etag');
73+
}
5874
}
5975

6076
/**

lib/Github/HttpClient/Cache/GaufretteCache.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ public function get($id)
4141
public function set($id, Response $response)
4242
{
4343
$this->filesystem->write($id, serialize($response), true);
44+
$this->filesystem->write($id.'.etag', $response->getHeader('ETag'), true);
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public function has($id)
51+
{
52+
$this->filesystem->has($id);
4453
}
4554

4655
/**
@@ -52,4 +61,11 @@ public function getModifiedSince($id)
5261
return $this->filesystem->mtime($id);
5362
}
5463
}
64+
65+
public function getETag($id)
66+
{
67+
if ($this->filesystem->has($id)) {
68+
return $this->filesystem->read($id.'.etag');
69+
}
70+
}
5571
}

lib/Github/HttpClient/CachedHttpClient.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,11 @@ public function request($path, $body = null, $httpMethod = 'GET', array $headers
4646
{
4747
$response = parent::request($path, $body, $httpMethod, $headers, $options);
4848

49-
$key = trim($this->options['base_url'].$path, '/');
5049
if (304 == $response->getStatusCode()) {
51-
return $this->getCache()->get($key);
50+
return $this->getCache()->get($path);
5251
}
5352

54-
$this->getCache()->set($key, $response);
53+
$this->getCache()->set($path, $response);
5554

5655
return $response;
5756
}
@@ -74,6 +73,12 @@ protected function createRequest($httpMethod, $path, $body = null, array $header
7473
sprintf('%s GMT', $modifiedAt->format('l, d-M-y H:i:s'))
7574
);
7675
}
76+
if ($etag = $this->getCache()->getETag($path)) {
77+
$request->addHeader(
78+
'If-None-Match',
79+
$etag
80+
);
81+
}
7782

7883
return $request;
7984
}

0 commit comments

Comments
 (0)