X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fseekat;a=blobdiff_plain;f=examples%2Fcache.php;fp=examples%2Fcache.php;h=87c0aef7eef446c43b1024bc7b60a5b5b9667992;hp=0000000000000000000000000000000000000000;hb=2451d97f1cb7b97e445b4dd839835b8673a4d0fc;hpb=3958595e9ff27162ae918db1453ddecd4840d481 diff --git a/examples/cache.php b/examples/cache.php new file mode 100644 index 0000000..87c0aef --- /dev/null +++ b/examples/cache.php @@ -0,0 +1,45 @@ +#!/usr/bin/env php +pushHandler(new StreamHandler(STDERR, Logger::INFO)); + +$redis = new Redis; +$redis->connect("localhost"); +$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); +$cache = new class($redis) implements \seekat\API\Call\Cache\Service { + private $redis; + function __construct(Redis $redis) { + $this->redis = $redis; + } + function clear() { + return $this->redis->flushDB(); + } + function fetch(string $key, \http\Client\Response &$response = null): bool { + list($exists, $response) = $this->redis + ->multi() + ->exists($key) + ->get($key) + ->exec(); + return $exists; + } + function store(string $key, \http\Client\Response $response): bool { + return $this->redis->set($key, $response); + } +}; + +$api = new seekat\API([ + "Authorization" => "token ".getenv("GITHUB_TOKEN") +], null, null, $log, $cache); + +$api(function($api) use($cache) { + yield $api->users->m6w6(); + yield $api->users->m6w6(); + $cache->clear(); +});