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 | use function Koded\Stdlib\json_serialize; |
||||||||||||||||||
17 | |||||||||||||||||||
18 | /** |
||||||||||||||||||
19 | * RedisJsonClient uses the Redis PHP extension. |
||||||||||||||||||
20 | * |
||||||||||||||||||
21 | * It will create 2 entries in Redis |
||||||||||||||||||
22 | * - one as JSON cache item |
||||||||||||||||||
23 | * - and other as serialized PHP value |
||||||||||||||||||
24 | * |
||||||||||||||||||
25 | * The first is useful for other programming languages to use it, |
||||||||||||||||||
26 | * and the PHP serialized variant is useful only for PHP applications |
||||||||||||||||||
27 | * where the cached item is handled by PHP serialization. |
||||||||||||||||||
28 | * |
||||||||||||||||||
29 | */ |
||||||||||||||||||
30 | final class RedisJsonClient implements Cache |
||||||||||||||||||
31 | { |
||||||||||||||||||
32 | use ClientTrait, MultiplesTrait; |
||||||||||||||||||
33 | |||||||||||||||||||
34 | private string $suffix; |
||||||||||||||||||
35 | private int $options; |
||||||||||||||||||
36 | private Serializer $serializer; |
||||||||||||||||||
37 | |||||||||||||||||||
38 | 70 | public function __construct(\Redis $client, Serializer $serializer, int $options, int $ttl = null) |
|||||||||||||||||
39 | { |
||||||||||||||||||
40 | 70 | $this->suffix = '__' . $serializer->type() . '__'; |
|||||||||||||||||
41 | 70 | $this->serializer = $serializer; |
|||||||||||||||||
42 | 70 | $this->options = $options; |
|||||||||||||||||
43 | 70 | $this->client = $client; |
|||||||||||||||||
44 | 70 | $this->ttl = $ttl; |
|||||||||||||||||
45 | } |
||||||||||||||||||
46 | |||||||||||||||||||
47 | 46 | public function get(string $key, mixed $default = null): mixed |
|||||||||||||||||
48 | { |
||||||||||||||||||
49 | 46 | return $this->has($key) |
|||||||||||||||||
50 | 38 | ? $this->serializer->unserialize($this->client->get($key . $this->suffix)) |
|||||||||||||||||
51 | 46 | : $default; |
|||||||||||||||||
52 | } |
||||||||||||||||||
53 | |||||||||||||||||||
54 | 48 | public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool |
|||||||||||||||||
55 | { |
||||||||||||||||||
56 | 48 | verify_key($key); |
|||||||||||||||||
57 | 48 | $expiration = $this->secondsWithGlobalTtl($ttl); |
|||||||||||||||||
58 | 48 | if (null === $ttl && 0 === $expiration) { |
|||||||||||||||||
59 | 45 | return $this->client->set($key, json_serialize($value, $this->options)) |
|||||||||||||||||
60 | 45 | && $this->client->set($key . $this->suffix, $this->serializer->serialize($value)); |
|||||||||||||||||
61 | } |
||||||||||||||||||
62 | 4 | if ($expiration > 0) { |
|||||||||||||||||
63 | 2 | return $this->client->setex($key, $expiration, json_serialize($value, $this->options)) |
|||||||||||||||||
0 ignored issues
–
show
The method
setex() does not exist on Koded\Caching\Client\ShmopClient . Did you maybe mean set() ?
(
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
setex() does not exist on Koded\Caching\Client\FileClient . Did you maybe mean set() ?
(
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
setex() does not exist on Memcached . Did you maybe mean set() ?
(
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. ![]() |
|||||||||||||||||||
64 | 2 | && $this->client->setex($key . $this->suffix, $expiration, $this->serializer->serialize($value)); |
|||||||||||||||||
65 | } |
||||||||||||||||||
66 | 2 | $this->client->del([$key, $key . $this->suffix]); |
|||||||||||||||||
0 ignored issues
–
show
The method
del() does not exist on Koded\Caching\Client\MemoryClient .
(
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
del() 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. ![]() The method
del() 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
del() 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. ![]() |
|||||||||||||||||||
67 | 2 | return true; |
|||||||||||||||||
68 | } |
||||||||||||||||||
69 | |||||||||||||||||||
70 | 8 | public function delete(string $key): bool |
|||||||||||||||||
71 | { |
||||||||||||||||||
72 | 8 | if (false === $this->has($key)) { |
|||||||||||||||||
73 | 6 | return true; |
|||||||||||||||||
74 | } |
||||||||||||||||||
75 | 6 | return 2 === $this->client->del([$key, $key . $this->suffix]); |
|||||||||||||||||
76 | } |
||||||||||||||||||
77 | |||||||||||||||||||
78 | 70 | public function clear(): bool |
|||||||||||||||||
79 | { |
||||||||||||||||||
80 | 70 | return $this->client->flushDB(); |
|||||||||||||||||
0 ignored issues
–
show
The method
flushDB() does not exist on Memcached . Did you maybe mean flush() ?
(
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
flushDB() 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
flushDB() does not exist on Koded\Caching\Client\MemoryClient .
(
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
flushDB() 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. ![]() |
|||||||||||||||||||
81 | } |
||||||||||||||||||
82 | |||||||||||||||||||
83 | 51 | public function has(string $key): bool |
|||||||||||||||||
84 | { |
||||||||||||||||||
85 | 51 | verify_key($key); |
|||||||||||||||||
86 | 51 | 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. ![]() The method
exists() does not exist on Koded\Caching\Client\MemoryClient .
(
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. ![]() |
|||||||||||||||||||
87 | 51 | && (bool)$this->client->exists($key . $this->suffix); |
|||||||||||||||||
88 | } |
||||||||||||||||||
89 | } |
||||||||||||||||||
90 |
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.