update to PHP-8.1
[m6w6/seekat] / examples / cache.php
1 #!/usr/bin/env php
2 <?php
3
4 require_once __DIR__ . "/../vendor/autoload.php";
5
6 $redis = new Redis;
7 $redis->connect("localhost");
8 $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
9 $cache = new class($redis) implements \seekat\API\Call\Cache\Service {
10 function __construct(private readonly Redis $redis) {
11 }
12 function clear() : void {
13 $this->redis->flushDB();
14 }
15 function fetch(string $key, \http\Client\Response &$response = null): bool {
16 list($exists, $response) = $this->redis
17 ->multi()
18 ->exists($key)
19 ->get($key)
20 ->exec();
21 return $exists;
22 }
23 function store(string $key, \http\Client\Response $response): bool {
24 return $this->redis->set($key, $response);
25 }
26 function del(string $key) : void {
27 $this->redis->del($key);
28 }
29 };
30
31 $log_level = "INFO";
32
33 $api = include "examples.inc";
34
35 $api(function($api) use($cache) {
36 yield $api->users->m6w6();
37 yield $api->users->m6w6();
38 //$cache->clear();
39 });