update to PHP-8.1
[m6w6/seekat] / lib / API / Call / Cache / Service / ItemPool.php
1 <?php
2
3 namespace seekat\API\Call\Cache\Service;
4
5 use http\Client\Response;
6 use Psr\Cache\CacheItemInterface;
7 use Psr\Cache\CacheItemPoolInterface;
8 use seekat\API\Call\Cache\Service;
9
10 final class ItemPool implements Service {
11 private ?CacheItemInterface $item;
12
13 public function __construct(private readonly CacheItemPoolInterface $cache) {
14 }
15
16 /**
17 * @param string $key
18 * @param Response|null $response
19 * @return bool
20 * @throws \Psr\Cache\InvalidArgumentException
21 */
22 public function fetch(string $key, Response &$response = null) : bool {
23 $this->item = $this->cache->getItem($key);
24 if ($this->item->isHit()) {
25 $response = $this->item->get();
26 return true;
27 }
28 return false;
29 }
30
31 public function store(string $key, Response $response) : bool {
32 $this->item->set($response);
33 return $this->cache->save($this->item);
34 }
35
36 /**
37 * @param string $key
38 * @throws \Psr\Cache\InvalidArgumentException
39 */
40 public function del(string $key) : void {
41 $this->cache->deleteItem($key);
42 }
43
44 public function clear() : void {
45 $this->cache->clear();
46 }
47 }