797fe60fb6fa184f281e3568d276bf937b213dc0
[m6w6/seekat] / examples / cache.php
1 #!/usr/bin/env php
2 <?php
3
4 require __DIR__."/../vendor/autoload.php";
5
6 use Monolog\{
7 Handler\StreamHandler, Logger
8 };
9
10 $log = new Logger("seekat");
11 $log->pushHandler(new StreamHandler(STDERR, Logger::INFO));
12
13 $redis = new Redis;
14 $redis->connect("localhost");
15 $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
16 $cache = new class($redis) implements \seekat\API\Call\Cache\Service {
17 private $redis;
18 function __construct(Redis $redis) {
19 $this->redis = $redis;
20 }
21 function clear() {
22 return $this->redis->flushDB();
23 }
24 function fetch(string $key, \http\Client\Response &$response = null): bool {
25 list($exists, $response) = $this->redis
26 ->multi()
27 ->exists($key)
28 ->get($key)
29 ->exec();
30 return $exists;
31 }
32 function store(string $key, \http\Client\Response $response): bool {
33 return $this->redis->set($key, $response);
34 }
35 };
36
37 $api = new seekat\API(seekat\API\Future\react(), [
38 "Authorization" => "token ".getenv("GITHUB_TOKEN")
39 ], null, null, $log, $cache);
40
41 $api(function($api) use($cache) {
42 yield $api->users->m6w6();
43 yield $api->users->m6w6();
44 $cache->clear();
45 });