|
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 Predis package. |
|
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
|
|
|
final class PredisJsonClient implements Cache |
|
30
|
|
|
{ |
|
31
|
|
|
use ClientTrait, MultiplesTrait; |
|
32
|
|
|
|
|
33
|
|
|
private string $suffix; |
|
34
|
|
|
private int $options; |
|
35
|
|
|
private Serializer $serializer; |
|
36
|
|
|
|
|
37
|
71 |
|
public function __construct(\Predis\Client $client, Serializer $serializer, int $options, int $ttl = null) |
|
38
|
|
|
{ |
|
39
|
71 |
|
$this->suffix = '__' . $serializer->type() . '__'; |
|
40
|
71 |
|
$this->serializer = $serializer; |
|
41
|
71 |
|
$this->options = $options; |
|
42
|
71 |
|
$this->client = $client; |
|
43
|
71 |
|
$this->ttl = $ttl; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
46 |
|
public function get(string $key, mixed $default = null): mixed |
|
47
|
|
|
{ |
|
48
|
46 |
|
return $this->has($key) |
|
49
|
38 |
|
? $this->serializer->unserialize($this->client->get($key . $this->suffix)) |
|
50
|
46 |
|
: $default; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
48 |
|
public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool |
|
54
|
|
|
{ |
|
55
|
48 |
|
verify_key($key); |
|
56
|
48 |
|
$expiration = $this->secondsWithGlobalTtl($ttl); |
|
57
|
48 |
|
if (null === $ttl && 0 === $expiration) { |
|
58
|
45 |
|
return 'OK' === $this->client->set($key, json_serialize($value, $this->options))->getPayload() |
|
59
|
45 |
|
&& 'OK' === $this->client->set($key . $this->suffix, $this->serializer->serialize($value))->getPayload(); |
|
60
|
|
|
} |
|
61
|
4 |
|
if ($expiration > 0) { |
|
62
|
2 |
|
return 'OK' === $this->client->setex($key, $expiration, json_serialize($value, $this->options))->getPayload() |
|
|
|
|
|
|
63
|
2 |
|
&& 'OK' === $this->client->setex($key . $this->suffix, $expiration, $this->serializer->serialize($value))->getPayload(); |
|
64
|
|
|
} |
|
65
|
2 |
|
$this->client->del([$key, $key . $this->suffix]); |
|
|
|
|
|
|
66
|
2 |
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
8 |
|
public function delete(string $key): bool |
|
70
|
|
|
{ |
|
71
|
8 |
|
if (false === $this->has($key)) { |
|
72
|
6 |
|
return true; |
|
73
|
|
|
} |
|
74
|
6 |
|
return 2 === $this->client->del([$key, $key . $this->suffix]); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
70 |
|
public function clear(): bool |
|
78
|
|
|
{ |
|
79
|
70 |
|
return 'OK' === $this->client->flushDb()->getPayload(); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
51 |
|
public function has(string $key): bool |
|
83
|
|
|
{ |
|
84
|
51 |
|
verify_key($key); |
|
85
|
51 |
|
return (bool)$this->client->exists($key) |
|
|
|
|
|
|
86
|
51 |
|
&& (bool)$this->client->exists($key . $this->suffix); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
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.