1 | <?php declare(strict_types=1); |
||
2 | |||
3 | namespace WyriHaximus\React\Cache; |
||
4 | |||
5 | use React\Cache\CacheInterface; |
||
6 | use React\Promise\PromiseInterface; |
||
7 | |||
8 | final class Msgpack implements CacheInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var CacheInterface |
||
12 | */ |
||
13 | private $cache; |
||
14 | |||
15 | /** |
||
16 | * @param CacheInterface $cache |
||
17 | */ |
||
18 | 10 | public function __construct(CacheInterface $cache) |
|
19 | { |
||
20 | 10 | $this->cache = $cache; |
|
21 | 10 | } |
|
22 | |||
23 | /** |
||
24 | * @param $key |
||
25 | * @param null $default |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
26 | * @return PromiseInterface |
||
27 | */ |
||
28 | 2 | public function get($key, $default = null) |
|
29 | { |
||
30 | return $this->cache->get($key, $default)->then(function ($result) use ($default) { |
||
31 | 2 | if ($result === null || $result === $default) { |
|
32 | 1 | return $result; |
|
33 | } |
||
34 | |||
35 | 1 | return msgpack_unpack($result); |
|
36 | 2 | }); |
|
37 | } |
||
38 | |||
39 | /** |
||
40 | * @param string $key |
||
41 | * @param mixed $value |
||
42 | * @param null $ttl |
||
0 ignored issues
–
show
|
|||
43 | * @return PromiseInterface |
||
44 | */ |
||
45 | 1 | public function set($key, $value, $ttl = null) |
|
46 | { |
||
47 | 1 | return $this->cache->set($key, msgpack_pack($value), $ttl); |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param string $key |
||
52 | * @return PromiseInterface |
||
53 | */ |
||
54 | 1 | public function delete($key) |
|
55 | { |
||
56 | 1 | return $this->cache->delete($key); |
|
57 | } |
||
58 | |||
59 | 2 | public function getMultiple(array $keys, $default = null) |
|
60 | { |
||
61 | return $this->cache->getMultiple($keys, $default)->then(function ($results) use ($default) { |
||
62 | 2 | foreach ($results as $key => $result) { |
|
63 | 2 | if ($result === null || $result === $default) { |
|
64 | 1 | continue; |
|
65 | } |
||
66 | |||
67 | 1 | $results[$key] = msgpack_unpack($result); |
|
68 | } |
||
69 | |||
70 | 2 | return $results; |
|
71 | 2 | }); |
|
72 | } |
||
73 | |||
74 | 1 | public function setMultiple(array $values, $ttl = null) |
|
75 | { |
||
76 | 1 | foreach ($values as $key => $value) { |
|
77 | 1 | $values[$key] = msgpack_pack($value); |
|
78 | } |
||
79 | |||
80 | 1 | return $this->cache->setMultiple($values, $ttl); |
|
81 | } |
||
82 | |||
83 | 1 | public function deleteMultiple(array $keys) |
|
84 | { |
||
85 | 1 | return $this->cache->deleteMultiple($keys); |
|
86 | } |
||
87 | |||
88 | 1 | public function clear() |
|
89 | { |
||
90 | 1 | return $this->cache->clear(); |
|
91 | } |
||
92 | |||
93 | 1 | public function has($key) |
|
94 | { |
||
95 | 1 | return $this->cache->has($key); |
|
96 | } |
||
97 | } |
||
98 |