diff --git a/composer.json b/composer.json index b9595471044..3e519ef5eeb 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "php-http/discovery": "^1.0", "php-http/client-implementation": "^1.0", "php-http/client-common": "^1.3", - "php-http/cache-plugin": "^1.2" + "php-http/cache-plugin": "^1.3" }, "require-dev": { "phpunit/phpunit": "^4.0 || ^5.5", diff --git a/doc/README.md b/doc/README.md index ea20c6a6897..20abcd6373a 100644 --- a/doc/README.md +++ b/doc/README.md @@ -48,4 +48,5 @@ Additional features: * [Request any Route](request_any_route.md) * [Customize `php-github-api`](customize.md) * [Running and writing tests](testing.md) +* [Response caching](caching.md) * [Request / Response info](request_response_info.md) diff --git a/doc/caching.md b/doc/caching.md new file mode 100644 index 00000000000..a95f92e9eea --- /dev/null +++ b/doc/caching.md @@ -0,0 +1,30 @@ +## Response caching +[Back to the navigation](README.md) + +This example uses the PSR6 cache pool [redis-adapter](https://github.com/php-cache/redis-adapter). See http://www.php-cache.com/ for alternatives. + +```php +connect('127.0.0.1', 6379); +// Create a PSR6 cache pool +$pool = new RedisCachePool($client); + +$client = new \Github\Client(); +$client->addCache($pool); + +// Do some request + +// Stop using cache +$client->removeCache(); +``` + +Using cache, the client will get cached responses if resources haven't changed since last time, +**without** reaching the `X-Rate-Limit` [imposed by github](http://developer.github.com/v3/#rate-limiting). + diff --git a/lib/Github/HttpClient/Builder.php b/lib/Github/HttpClient/Builder.php index 5b742285bd9..e42a5efe22e 100644 --- a/lib/Github/HttpClient/Builder.php +++ b/lib/Github/HttpClient/Builder.php @@ -176,12 +176,12 @@ public function addHeaderValue($header, $headerValue) /** * Add a cache plugin to cache responses locally. * - * @param CacheItemPoolInterface $cache + * @param CacheItemPoolInterface $cachePool * @param array $config */ public function addCache(CacheItemPoolInterface $cachePool, array $config = []) { - $this->cachePlugin = new Plugin\CachePlugin($cachePool, $this->streamFactory, $config); + $this->cachePlugin = Plugin\CachePlugin::clientCache($cachePool, $this->streamFactory, $config); $this->httpClientModified = true; }