Skip to content

Use the new cache plugin factory method #531

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
Mar 28, 2017
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
30 changes: 30 additions & 0 deletions doc/caching.md
Original file line number Diff line number Diff line change
@@ -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
<?php

// This file is generated by Composer
require_once 'vendor/autoload.php';

use Cache\Adapter\Redis\RedisCachePool;

$client = new \Redis();
$client->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).

4 changes: 2 additions & 2 deletions lib/Github/HttpClient/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down