63f12de84fc9d7971c0265a62e9c1c3f8c3bf2e2
[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 {
12 /**
13 * @var CacheItemPoolInterface
14 */
15 private $cache;
16
17 /**
18 * @var CacheItemInterface
19 */
20 private $item;
21
22 public function __construct(CacheItemPoolInterface $cache) {
23 $this->cache = $cache;
24 }
25
26 public function fetch(string $key, Response &$response = null) : bool {
27 $this->item = $this->cache->getItem($key);
28 if ($this->item->isHit()) {
29 $response = $this->item->get();
30 return true;
31 }
32 return false;
33 }
34
35 public function store(string $key, Response $response) : bool {
36 $this->item->set($response);
37 return $this->cache->save($this->item);
38 }
39
40 public function clear() {
41 $this->cache->clear();
42 }
43 }