Skip to content

Commit aaaaf52

Browse files
authored
Merge pull request #404 from Nyholm/cache
Use PSR6 cache
2 parents dc1836b + a1b8362 commit aaaaf52

File tree

10 files changed

+32
-508
lines changed

10 files changed

+32
-508
lines changed

README.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,20 @@ From `$client` object, you can access to all GitHub.
6969
// This file is generated by Composer
7070
require_once 'vendor/autoload.php';
7171

72+
use Cache\Adapter\Redis\RedisCachePool;
73+
74+
$client = new \Redis();
75+
$client->connect('127.0.0.1', 6379);
76+
// Create a PSR6 cache pool
77+
$pool = new RedisCachePool($client);
78+
7279
$client = new \Github\Client();
73-
$client->useCache();
74-
75-
// Or select directly which cache you want to use
76-
$client->useCache(
77-
// Built in one, or any cache implementing this interface:
78-
// Github\HttpClient\Cache\CacheInterface
79-
new \Github\HttpClient\Cache\FilesystemCache('/tmp/github-api-cache')
80-
);
80+
$client->addCache($pool);
81+
82+
// Do some request
83+
84+
// Stop using cache
85+
$client->removeCache();
8186
```
8287

8388
Using cache, the client will get cached responses if resources haven't changed since last time,

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@
1717
}
1818
],
1919
"require": {
20-
"php": "^5.5|^7.0",
20+
"php": "^5.5 || ^7.0",
21+
"psr/http-message": "^1.0",
22+
"psr/cache": "^1.0",
2123
"php-http/httplug": "^1.0",
2224
"php-http/discovery": "^1.0",
2325
"php-http/client-implementation": "^1.0",
24-
"php-http/client-common": "^1.1"
26+
"php-http/client-common": "^1.1",
27+
"php-http/cache-plugin": "^1.0"
2528
},
2629
"require-dev": {
2730
"phpunit/phpunit": "~4.0",
2831
"php-http/guzzle6-adapter": "~1.0",
2932
"guzzlehttp/psr7": "^1.2",
3033
"sllh/php-cs-fixer-styleci-bridge": "~1.3"
3134
},
32-
"suggest": {
33-
"knplabs/gaufrette": "Needed for optional Gaufrette cache"
34-
},
3535
"autoload": {
3636
"psr-4": { "Github\\": "lib/Github/" }
3737
},

lib/Github/Client.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
use Github\Api\ApiInterface;
66
use Github\Exception\InvalidArgumentException;
77
use Github\Exception\BadMethodCallException;
8-
use Github\HttpClient\Cache\CacheInterface;
98
use Github\HttpClient\Plugin\Authentication;
10-
use Github\HttpClient\Plugin\Cache;
119
use Github\HttpClient\Plugin\GithubExceptionThrower;
1210
use Github\HttpClient\Plugin\History;
1311
use Github\HttpClient\Plugin\PathPrepend;
@@ -19,6 +17,7 @@
1917
use Http\Discovery\MessageFactoryDiscovery;
2018
use Http\Discovery\UriFactoryDiscovery;
2119
use Http\Message\MessageFactory;
20+
use Psr\Cache\CacheItemPoolInterface;
2221

2322
/**
2423
* Simple yet very cool PHP GitHub client.
@@ -376,19 +375,21 @@ public function addHeaders(array $headers)
376375
}
377376

378377
/**
379-
* @param bool|CacheInterface $cache
378+
* Add a cache plugin to cache responses locally.
379+
* @param CacheItemPoolInterface $cache
380380
*/
381-
public function useCache($cache = true)
381+
public function addCache(CacheItemPoolInterface $cachePool)
382382
{
383-
$this->removePlugin(Cache::class);
384-
if ($cache !== false) {
385-
if ($cache instanceof CacheInterface) {
386-
$plugin = new Cache($cache);
387-
} else {
388-
$plugin = new Cache();
389-
}
390-
$this->addPlugin($plugin);
391-
}
383+
$this->removeCache();
384+
$this->addPlugin(new Plugin\CachePlugin($cachePool));
385+
}
386+
387+
/**
388+
* Remove the cache plugin
389+
*/
390+
public function removeCache()
391+
{
392+
$this->removePlugin(Plugin\CachePlugin::class);
392393
}
393394

394395
/**

lib/Github/HttpClient/Cache/CacheInterface.php

Lines changed: 0 additions & 51 deletions
This file was deleted.

lib/Github/HttpClient/Cache/FilesystemCache.php

Lines changed: 0 additions & 85 deletions
This file was deleted.

lib/Github/HttpClient/Cache/GaufretteCache.php

Lines changed: 0 additions & 71 deletions
This file was deleted.

lib/Github/HttpClient/Cache/ResponseSerializer.php

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)