Skip to content

Commit dffdc2a

Browse files
committed
Simplify cache setup for CachedHttpClient
1 parent 35c32b3 commit dffdc2a

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

README.markdown

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,17 @@ $client = new Github\Client(
7171
);
7272

7373
// Or select directly which cache you want to use
74-
$client = new Github\Client(
75-
new Github\HttpClient\CachedHttpClient(
76-
new Github\HttpClient\Cache\FilesystemCache('/tmp/github-api-cache')
77-
)
74+
$client = new Github\HttpClient\CachedHttpClient();
75+
$client->setCache(
76+
// Built in one, or any cache implementing this interface:
77+
// Github\HttpClient\Cache\CacheInterface
78+
new Github\HttpClient\Cache\FilesystemCache('/tmp/github-api-cache')
7879
);
80+
81+
$client = new Github\Client($client);
7982
```
8083

81-
Using cache, the client will get cached responses if resources haven't changed since last time,
84+
Using cache, the client will get cached responses if resources haven't changed since last time,
8285
**without** reaching the `X-Rate-Limit` [imposed by github](http://developer.github.com/v3/#rate-limiting).
8386

8487

lib/Github/HttpClient/CachedHttpClient.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Github\HttpClient;
44

5-
use Buzz\Client\ClientInterface;
6-
75
use Github\HttpClient\Cache\CacheInterface;
86
use Github\HttpClient\Cache\FilesystemCache;
97

@@ -22,15 +20,23 @@ class CachedHttpClient extends HttpClient
2220
protected $cache;
2321

2422
/**
25-
* @param array $options
26-
* @param null|ClientInterface $client
27-
* @param null|CacheInterface $cache
23+
* @return CacheInterface
2824
*/
29-
public function __construct(array $options = array(), ClientInterface $client = null, CacheInterface $cache = null)
25+
public function getCache()
3026
{
31-
parent::__construct($options, $client);
27+
if (null === $this->cache) {
28+
$this->cache = new FilesystemCache($this->options['cache_dir'] ?: sys_get_temp_dir().DIRECTORY_SEPARATOR.'php-github-api-cache');
29+
}
30+
31+
return $this->cache;
32+
}
3233

33-
$this->cache = $cache ?: new FilesystemCache($this->options['cache_dir'] ?: sys_get_temp_dir().DIRECTORY_SEPARATOR.'php-github-api-cache');
34+
/**
35+
* @param $cache CacheInterface
36+
*/
37+
public function setCache(CacheInterface $cache)
38+
{
39+
$this->cache = $cache;
3440
}
3541

3642
/**
@@ -42,10 +48,10 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
4248

4349
$key = trim($this->options['base_url'].$path, '/');
4450
if ($response->isNotModified()) {
45-
return $this->cache->get($key);
51+
return $this->getCache()->get($key);
4652
}
4753

48-
$this->cache->set($key, $response);
54+
$this->getCache()->set($key, $response);
4955

5056
return $response;
5157
}
@@ -58,7 +64,7 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
5864
protected function createRequest($httpMethod, $url)
5965
{
6066
$request = parent::createRequest($httpMethod, $url);
61-
$request->addHeader(sprintf('If-Modified-Since: %s', date('r', $this->cache->getModifiedSince($url))));
67+
$request->addHeader(sprintf('If-Modified-Since: %s', date('r', $this->getCache()->getModifiedSince($url))));
6268

6369
return $request;
6470
}

test/Github/Tests/HttpClient/CachedHttpClientTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public function shouldCacheResponseAtFirstTime()
1616

1717
$httpClient = new TestCachedHttpClient(
1818
array('base_url' => ''),
19-
$this->getMock('Buzz\Client\ClientInterface', array('setTimeout', 'setVerifyPeer', 'send')),
20-
$cache
19+
$this->getMock('Buzz\Client\ClientInterface', array('setTimeout', 'setVerifyPeer', 'send'))
2120
);
21+
$httpClient->setCache($cache);
2222

2323
$cache->expects($this->once())->method('set')->with('test', new Response);
2424

@@ -40,9 +40,9 @@ public function shouldGetCachedResponseWhileResourceNotModified()
4040

4141
$httpClient = new TestCachedHttpClient(
4242
array('base_url' => ''),
43-
$client,
44-
$cache
43+
$client
4544
);
45+
$httpClient->setCache($cache);
4646
$httpClient->fakeResponse = $response;
4747

4848
$cache->expects($this->once())->method('get')->with('test');
@@ -65,9 +65,9 @@ public function shouldRenewCacheWhenResourceHasChanged()
6565

6666
$httpClient = new TestCachedHttpClient(
6767
array('base_url' => ''),
68-
$client,
69-
$cache
68+
$client
7069
);
70+
$httpClient->setCache($cache);
7171
$httpClient->fakeResponse = $response;
7272

7373
$cache->expects($this->once())->method('set')->with('test', $response);

0 commit comments

Comments
 (0)