1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Common\Cache; |
4
|
|
|
|
5
|
|
|
use Predis\ClientInterface; |
6
|
|
|
use RuntimeException; |
7
|
|
|
use function array_combine; |
8
|
|
|
use function array_filter; |
9
|
|
|
use function array_map; |
10
|
|
|
use function call_user_func_array; |
11
|
|
|
use function class_exists; |
12
|
|
|
use function serialize; |
13
|
|
|
use function unserialize; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Predis cache provider. |
17
|
|
|
*/ |
18
|
|
|
class PredisCache extends CacheProvider |
19
|
|
|
{ |
20
|
|
|
/** @var ClientInterface */ |
21
|
78 |
|
private $client; |
22
|
|
|
|
23
|
78 |
|
public function __construct(ClientInterface $client) |
24
|
78 |
|
{ |
25
|
|
|
$this->client = $client; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
74 |
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
74 |
|
protected function doFetch($id) |
32
|
74 |
|
{ |
33
|
64 |
|
$result = $this->client->get($id); |
34
|
|
|
if ($result === null) { |
35
|
|
|
return false; |
36
|
69 |
|
} |
37
|
|
|
|
38
|
|
|
return unserialize($result); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
2 |
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
2 |
|
protected function doFetchAtomic(string $id, callable $generator, int $ttl) |
45
|
|
|
{ |
46
|
2 |
|
if (! class_exists('\Predis\Pipeline\Atomic', false)) { |
47
|
|
|
throw new RuntimeException('Atomic fetch (atomic pipeline) is not supported by this version of Predis'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->client->pipeline(['atomic'], static function ($pipe) use ($id, $generator, $ttl) { |
|
|
|
|
51
|
|
|
$pipelineCache = new static($pipe); |
52
|
1 |
|
if ($pipelineCache->contains($id)) { |
53
|
|
|
return $pipelineCache->fetch($id); |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
$data = $generator($id); |
57
|
|
|
$pipelineCache->save($id, $data, $ttl); |
58
|
|
|
|
59
|
|
|
return $data; |
60
|
|
|
}); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
protected function doFetchMultiple(array $keys) |
67
|
|
|
{ |
68
|
|
|
$fetchedItems = call_user_func_array([$this->client, 'mget'], $keys); |
69
|
|
|
|
70
|
|
|
return array_map('unserialize', array_filter(array_combine($keys, $fetchedItems))); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
/** |
74
|
1 |
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
1 |
|
protected function doSaveMultiple(array $keysAndValues, $lifetime = 0) |
77
|
|
|
{ |
78
|
|
|
if ($lifetime) { |
79
|
|
|
$success = true; |
80
|
|
|
|
81
|
|
|
// Keys have lifetime, use SETEX for each of them |
82
|
69 |
|
foreach ($keysAndValues as $key => $value) { |
83
|
|
|
$response = (string) $this->client->setex($key, $lifetime, serialize($value)); |
84
|
69 |
|
|
85
|
|
|
if ($response == 'OK') { |
86
|
|
|
continue; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$success = false; |
90
|
72 |
|
} |
91
|
|
|
|
92
|
72 |
|
return $success; |
93
|
72 |
|
} |
94
|
3 |
|
|
95
|
|
|
// No lifetime, use MSET |
96
|
70 |
|
$response = $this->client->mset(array_map(static function ($value) { |
97
|
|
|
return serialize($value); |
98
|
|
|
}, $keysAndValues)); |
99
|
72 |
|
|
100
|
|
|
return (string) $response == 'OK'; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
43 |
|
*/ |
106
|
|
|
protected function doContains($id) |
107
|
43 |
|
{ |
108
|
|
|
return (bool) $this->client->exists($id); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
1 |
|
*/ |
114
|
|
|
protected function doSave($id, $data, $lifeTime = 0) |
115
|
1 |
|
{ |
116
|
|
|
$data = serialize($data); |
117
|
|
|
if ($lifeTime > 0) { |
118
|
|
|
$response = $this->client->setex($id, $lifeTime, $data); |
119
|
|
|
} else { |
120
|
|
|
$response = $this->client->set($id, $data); |
121
|
2 |
|
} |
122
|
|
|
|
123
|
2 |
|
return $response === true || $response == 'OK'; |
124
|
|
|
} |
125
|
2 |
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
protected function doDelete($id) |
130
|
|
|
{ |
131
|
2 |
|
return $this->client->del($id) >= 0; |
132
|
|
|
} |
133
|
2 |
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
2 |
|
*/ |
137
|
2 |
|
protected function doDeleteMultiple(array $keys) |
138
|
2 |
|
{ |
139
|
2 |
|
return $this->client->del($keys) >= 0; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
|
|
protected function doFlush() |
146
|
|
|
{ |
147
|
|
|
$response = $this->client->flushdb(); |
148
|
|
|
|
149
|
|
|
return $response === true || $response == 'OK'; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* {@inheritdoc} |
154
|
|
|
*/ |
155
|
|
|
protected function doGetStats() |
156
|
|
|
{ |
157
|
|
|
$info = $this->client->info(); |
158
|
|
|
|
159
|
|
|
return [ |
160
|
|
|
Cache::STATS_HITS => $info['Stats']['keyspace_hits'], |
161
|
|
|
Cache::STATS_MISSES => $info['Stats']['keyspace_misses'], |
162
|
|
|
Cache::STATS_UPTIME => $info['Server']['uptime_in_seconds'], |
163
|
|
|
Cache::STATS_MEMORY_USAGE => $info['Memory']['used_memory'], |
164
|
|
|
Cache::STATS_MEMORY_AVAILABLE => false, |
165
|
|
|
]; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|