|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Common\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use Redis; |
|
6
|
|
|
use function array_combine; |
|
7
|
|
|
use function defined; |
|
8
|
|
|
use function extension_loaded; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Redis cache provider. |
|
12
|
|
|
* |
|
13
|
|
|
* @link www.doctrine-project.org |
|
14
|
|
|
*/ |
|
15
|
|
|
class RedisCache extends CacheProvider |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var Redis|null */ |
|
18
|
|
|
private $redis; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Sets the redis instance to use. |
|
22
|
|
|
* |
|
23
|
|
|
* @return void |
|
24
|
|
|
*/ |
|
25
|
|
|
public function setRedis(Redis $redis) |
|
26
|
|
|
{ |
|
27
|
|
|
$redis->setOption(Redis::OPT_SERIALIZER, $this->getSerializerValue()); |
|
28
|
|
|
$this->redis = $redis; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Gets the redis instance used by the cache. |
|
33
|
|
|
* |
|
34
|
|
|
* @return Redis|null |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getRedis() |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->redis; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function doFetch($id) |
|
45
|
81 |
|
{ |
|
46
|
|
|
return $this->redis->get($id); |
|
|
|
|
|
|
47
|
81 |
|
} |
|
48
|
81 |
|
|
|
49
|
81 |
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function doFetchMultiple(array $keys) |
|
53
|
|
|
{ |
|
54
|
|
|
$fetchedItems = array_combine($keys, $this->redis->mget($keys)); |
|
55
|
|
|
|
|
56
|
2 |
|
// Redis mget returns false for keys that do not exist. So we need to filter those out unless it's the real data. |
|
57
|
|
|
$foundItems = []; |
|
58
|
2 |
|
|
|
59
|
|
|
foreach ($fetchedItems as $key => $value) { |
|
60
|
|
|
if ($value === false && ! $this->redis->exists($key)) { |
|
61
|
|
|
continue; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
76 |
|
$foundItems[$key] = $value; |
|
65
|
|
|
} |
|
66
|
76 |
|
|
|
67
|
|
|
return $foundItems; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
2 |
|
*/ |
|
73
|
|
|
protected function doSaveMultiple(array $keysAndValues, $lifetime = 0) |
|
74
|
2 |
|
{ |
|
75
|
|
|
if ($lifetime) { |
|
76
|
|
|
$success = true; |
|
77
|
2 |
|
|
|
78
|
|
|
// Keys have lifetime, use SETEX for each of them |
|
79
|
2 |
|
foreach ($keysAndValues as $key => $value) { |
|
80
|
2 |
|
if ($this->redis->setex($key, $lifetime, $value)) { |
|
81
|
2 |
|
continue; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$success = false; |
|
85
|
2 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $success; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// No lifetime, use MSET |
|
91
|
1 |
|
return (bool) $this->redis->mset($keysAndValues); |
|
92
|
|
|
} |
|
93
|
1 |
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritdoc} |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function doContains($id) |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->redis->exists($id); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritdoc} |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function doSave($id, $data, $lifeTime = 0) |
|
106
|
|
|
{ |
|
107
|
1 |
|
if ($lifeTime > 0) { |
|
108
|
|
|
return $this->redis->setex($id, $lifeTime, $data); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $this->redis->set($id, $data); |
|
112
|
|
|
} |
|
113
|
71 |
|
|
|
114
|
|
|
/** |
|
115
|
71 |
|
* {@inheritdoc} |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function doDelete($id) |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->redis->delete($id) >= 0; |
|
120
|
|
|
} |
|
121
|
74 |
|
|
|
122
|
|
|
/** |
|
123
|
74 |
|
* {@inheritdoc} |
|
124
|
3 |
|
*/ |
|
125
|
|
|
protected function doDeleteMultiple(array $keys) |
|
126
|
|
|
{ |
|
127
|
72 |
|
return $this->redis->delete($keys) >= 0; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritdoc} |
|
132
|
|
|
*/ |
|
133
|
44 |
|
protected function doFlush() |
|
134
|
|
|
{ |
|
135
|
44 |
|
return $this->redis->flushDB(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* {@inheritdoc} |
|
140
|
|
|
*/ |
|
141
|
1 |
|
protected function doGetStats() |
|
142
|
|
|
{ |
|
143
|
1 |
|
$info = $this->redis->info(); |
|
144
|
|
|
return [ |
|
145
|
|
|
Cache::STATS_HITS => $info['keyspace_hits'], |
|
146
|
|
|
Cache::STATS_MISSES => $info['keyspace_misses'], |
|
147
|
|
|
Cache::STATS_UPTIME => $info['uptime_in_seconds'], |
|
148
|
|
|
Cache::STATS_MEMORY_USAGE => $info['used_memory'], |
|
149
|
2 |
|
Cache::STATS_MEMORY_AVAILABLE => false, |
|
150
|
|
|
]; |
|
151
|
2 |
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Returns the serializer constant to use. If Redis is compiled with |
|
155
|
|
|
* igbinary support, that is used. Otherwise the default PHP serializer is |
|
156
|
|
|
* used. |
|
157
|
2 |
|
* |
|
158
|
|
|
* @return int One of the Redis::SERIALIZER_* constants |
|
159
|
2 |
|
*/ |
|
160
|
|
|
protected function getSerializerValue() |
|
161
|
2 |
|
{ |
|
162
|
2 |
|
if (defined('Redis::SERIALIZER_IGBINARY') && extension_loaded('igbinary')) { |
|
163
|
2 |
|
return Redis::SERIALIZER_IGBINARY; |
|
164
|
2 |
|
} |
|
165
|
|
|
|
|
166
|
|
|
return Redis::SERIALIZER_PHP; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
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.