264cfbd173ea4414eaeeb36324333ab38a0f3c29
[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 * @var CacheItemPoolInterface
13 */
14 private $cache;
15
16 /**
17 * @var CacheItemInterface
18 */
19 private $item;
20
21 public function __construct(CacheItemPoolInterface $cache) {
22 $this->cache = $cache;
23 }
24
25 /**
26 * @param string $key
27 * @param Response|null $response
28 * @return bool
29 * @throws \Psr\Cache\InvalidArgumentException
30 */
31 public function fetch(string $key, Response &$response = null) : bool {
32 $this->item = $this->cache->getItem($key);
33 if ($this->item->isHit()) {
34 $response = $this->item->get();
35 return true;
36 }
37 return false;
38 }
39
40 public function store(string $key, Response $response) : bool {
41 $this->item->set($response);
42 return $this->cache->save($this->item);
43 }
44
45 /**
46 * @param string $key
47 * @throws \Psr\Cache\InvalidArgumentException
48 */
49 public function del(string $key) {
50 $this->cache->deleteItem($key);
51 }
52
53 public function clear() {
54 $this->cache->clear();
55 }
56 }