1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Koded package. |
5
|
|
|
* |
6
|
|
|
* (c) Mihail Binev <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Please view the LICENSE distributed with this source code |
9
|
|
|
* for the full copyright and license information. |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Koded\Caching\Client; |
14
|
|
|
|
15
|
|
|
use Koded\Caching\Cache; |
16
|
|
|
use Koded\Stdlib\Interfaces\Serializer; |
17
|
|
|
use function Koded\Caching\verify_key; |
18
|
|
|
use function Koded\Stdlib\json_serialize; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* RedisJsonClient uses the Predis package. |
22
|
|
|
* |
23
|
|
|
* It will create 2 entries in Redis |
24
|
|
|
* - one as JSON cache item |
25
|
|
|
* - and other as serialized PHP value |
26
|
|
|
* |
27
|
|
|
* The first is useful for other programming languages to use it, |
28
|
|
|
* and the PHP serialized variant is useful only for PHP applications |
29
|
|
|
* where the cached item is handled by PHP serialization. |
30
|
|
|
* |
31
|
|
|
*/ |
32
|
|
|
final class PredisJsonClient implements Cache |
33
|
|
|
{ |
34
|
|
|
use ClientTrait, MultiplesTrait; |
35
|
|
|
|
36
|
|
|
private $suffix; |
37
|
|
|
private $options; |
38
|
|
|
private $serializer; |
39
|
|
|
|
40
|
197 |
|
public function __construct(\Predis\Client $client, Serializer $serializer, int $options, int $ttl = null) |
41
|
|
|
{ |
42
|
197 |
|
$this->suffix = '__' . $serializer->type() . '__'; |
43
|
197 |
|
$this->serializer = $serializer; |
44
|
197 |
|
$this->options = $options; |
45
|
197 |
|
$this->client = $client; |
46
|
197 |
|
$this->ttl = $ttl; |
47
|
197 |
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
64 |
|
public function get($key, $default = null) |
51
|
|
|
{ |
52
|
64 |
|
return $this->has($key) |
53
|
39 |
|
? $this->serializer->unserialize($this->client->get($key . $this->suffix)) |
54
|
47 |
|
: $default; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
76 |
|
public function set($key, $value, $ttl = null) |
59
|
|
|
{ |
60
|
76 |
|
verify_key($key); |
61
|
59 |
|
$expiration = $this->secondsWithGlobalTtl($ttl); |
62
|
|
|
|
63
|
49 |
|
if (null === $ttl && 0 === $expiration) { |
64
|
46 |
|
return 'OK' === $this->client->set($key, json_serialize($value, $this->options))->getPayload() |
65
|
46 |
|
&& 'OK' === $this->client->set($key . $this->suffix, $this->serializer->serialize($value))->getPayload(); |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
if ($expiration > 0) { |
69
|
2 |
|
return 'OK' === $this->client->setex($key, $expiration, json_serialize($value, $this->options))->getPayload() |
|
|
|
|
70
|
2 |
|
&& 'OK' === $this->client->setex($key . $this->suffix, $expiration, $this->serializer->serialize($value))->getPayload(); |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
$this->client->del([$key, $key . $this->suffix]); |
|
|
|
|
74
|
|
|
|
75
|
2 |
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
25 |
|
public function delete($key) |
80
|
|
|
{ |
81
|
25 |
|
if (false === $this->has($key)) { |
82
|
6 |
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
6 |
|
return 2 === $this->client->del([$key, $key . $this->suffix]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
196 |
|
public function clear() |
90
|
|
|
{ |
91
|
196 |
|
return 'OK' === $this->client->flushDb()->getPayload(); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
103 |
|
public function has($key) |
96
|
|
|
{ |
97
|
103 |
|
verify_key($key); |
98
|
|
|
|
99
|
52 |
|
return (bool)$this->client->exists($key) |
|
|
|
|
100
|
52 |
|
&& (bool)$this->client->exists($key . $this->suffix); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
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.