1 | <?php |
||||||||
2 | /* |
||||||||
3 | * This file is part of the Koded package. |
||||||||
4 | * |
||||||||
5 | * (c) Mihail Binev <[email protected]> |
||||||||
6 | * |
||||||||
7 | * Please view the LICENSE distributed with this source code |
||||||||
8 | * for the full copyright and license information. |
||||||||
9 | */ |
||||||||
10 | |||||||||
11 | namespace Koded\Caching\Client; |
||||||||
12 | |||||||||
13 | use Koded\Caching\Cache; |
||||||||
14 | use Koded\Stdlib\Serializer; |
||||||||
15 | use function Koded\Caching\verify_key; |
||||||||
16 | |||||||||
17 | /** |
||||||||
18 | * Class RedisClient uses the Redis PHP extension. |
||||||||
19 | * |
||||||||
20 | */ |
||||||||
21 | final class RedisClient implements Cache |
||||||||
22 | { |
||||||||
23 | use ClientTrait, MultiplesTrait; |
||||||||
24 | |||||||||
25 | private Serializer $serializer; |
||||||||
26 | |||||||||
27 | 81 | public function __construct(\Redis $client, Serializer $serializer, int $ttl = null) |
|||||||
28 | { |
||||||||
29 | 81 | $this->client = $client; |
|||||||
30 | 81 | $this->serializer = $serializer; |
|||||||
31 | 81 | $this->ttl = $ttl; |
|||||||
32 | } |
||||||||
33 | |||||||||
34 | 50 | public function get(string $key, mixed $default = null): mixed |
|||||||
35 | { |
||||||||
36 | 50 | return $this->has($key) |
|||||||
37 | 41 | ? $this->serializer->unserialize($this->client->get($key)) |
|||||||
38 | 50 | : $default; |
|||||||
39 | } |
||||||||
40 | |||||||||
41 | 55 | public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool |
|||||||
42 | { |
||||||||
43 | 55 | verify_key($key); |
|||||||
44 | 55 | $expiration = $this->secondsWithGlobalTtl($ttl); |
|||||||
45 | 55 | if (null === $ttl && 0 === $expiration) { |
|||||||
46 | 51 | return $this->client->set($key, $this->serializer->serialize($value)); |
|||||||
47 | } |
||||||||
48 | 5 | if ($expiration > 0) { |
|||||||
49 | 2 | return $this->client->setex($key, $expiration, $this->serializer->serialize($value)); |
|||||||
50 | } |
||||||||
51 | 3 | $this->client->del($key); |
|||||||
52 | 3 | return true; |
|||||||
53 | } |
||||||||
54 | |||||||||
55 | 11 | public function delete(string $key): bool |
|||||||
56 | { |
||||||||
57 | 11 | if (false === $this->has($key)) { |
|||||||
58 | 8 | return true; |
|||||||
59 | } |
||||||||
60 | 8 | return 1 === $this->client->del($key); |
|||||||
61 | } |
||||||||
62 | |||||||||
63 | 80 | public function clear(): bool |
|||||||
64 | { |
||||||||
65 | 80 | return $this->client->flushDB(); |
|||||||
66 | } |
||||||||
67 | |||||||||
68 | 59 | public function has(string $key): bool |
|||||||
69 | { |
||||||||
70 | 59 | verify_key($key); |
|||||||
71 | 59 | return (bool)$this->client->exists($key); |
|||||||
0 ignored issues
–
show
The method
exists() does not exist on Koded\Caching\Client\ShmopClient .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() The method
exists() does not exist on Koded\Caching\Client\FileClient .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() The method
exists() does not exist on Memcached .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||||
72 | } |
||||||||
73 | } |
||||||||
74 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.