1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\Common\Cache; |
21
|
|
|
|
22
|
|
|
use Redis; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Redis cache provider. |
26
|
|
|
* |
27
|
|
|
* @link www.doctrine-project.org |
28
|
|
|
* @since 2.2 |
29
|
|
|
* @author Osman Ungur <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class RedisCache extends CacheProvider |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var Redis|null |
35
|
|
|
*/ |
36
|
|
|
private $redis; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Sets the redis instance to use. |
40
|
|
|
* |
41
|
|
|
* @param Redis $redis |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
80 |
|
public function setRedis(Redis $redis) |
46
|
|
|
{ |
47
|
80 |
|
$redis->setOption(Redis::OPT_SERIALIZER, $this->getSerializerValue()); |
48
|
80 |
|
$this->redis = $redis; |
49
|
80 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Gets the redis instance used by the cache. |
53
|
|
|
* |
54
|
|
|
* @return Redis|null |
55
|
|
|
*/ |
56
|
2 |
|
public function getRedis() |
57
|
|
|
{ |
58
|
2 |
|
return $this->redis; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
75 |
|
protected function doFetch($id) |
65
|
|
|
{ |
66
|
75 |
|
return $this->redis->get($id); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
2 |
|
protected function doFetchMultiple(array $keys) |
73
|
|
|
{ |
74
|
2 |
|
$fetchedItems = array_combine($keys, $this->redis->mget($keys)); |
75
|
|
|
|
76
|
|
|
// Redis mget returns false for keys that do not exist. So we need to filter those out unless it's the real data. |
77
|
2 |
|
$foundItems = []; |
78
|
|
|
|
79
|
2 |
|
foreach ($fetchedItems as $key => $value) { |
80
|
2 |
|
if (false !== $value || $this->redis->exists($key)) { |
81
|
2 |
|
$foundItems[$key] = $value; |
82
|
2 |
|
} |
83
|
2 |
|
} |
84
|
|
|
|
85
|
2 |
|
return $foundItems; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
1 |
|
protected function doSaveMultiple(array $keysAndValues, $lifetime = 0) |
92
|
|
|
{ |
93
|
1 |
|
if ($lifetime) { |
94
|
|
|
$success = true; |
95
|
|
|
|
96
|
|
|
// Keys have lifetime, use SETEX for each of them |
97
|
|
|
foreach ($keysAndValues as $key => $value) { |
98
|
|
|
if (!$this->redis->setex($key, $lifetime, $value)) { |
99
|
|
|
$success = false; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $success; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// No lifetime, use MSET |
107
|
1 |
|
return (bool) $this->redis->mset($keysAndValues); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
70 |
|
protected function doContains($id) |
114
|
|
|
{ |
115
|
70 |
|
return $this->redis->exists($id); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
73 |
|
protected function doSave($id, $data, $lifeTime = 0) |
122
|
|
|
{ |
123
|
73 |
|
if ($lifeTime > 0) { |
124
|
3 |
|
return $this->redis->setex($id, $lifeTime, $data); |
125
|
|
|
} |
126
|
|
|
|
127
|
71 |
|
return $this->redis->set($id, $data); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
44 |
|
protected function doDelete($id) |
134
|
|
|
{ |
135
|
44 |
|
return $this->redis->delete($id) >= 0; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
1 |
|
protected function doDeleteMultiple(array $keys) |
142
|
|
|
{ |
143
|
1 |
|
return $this->redis->delete($keys) >= 0; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
2 |
|
protected function doFlush() |
150
|
|
|
{ |
151
|
2 |
|
return $this->redis->flushDB(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
2 |
|
protected function doGetStats() |
158
|
|
|
{ |
159
|
2 |
|
$info = $this->redis->info(); |
160
|
|
|
return [ |
161
|
2 |
|
Cache::STATS_HITS => $info['keyspace_hits'], |
162
|
2 |
|
Cache::STATS_MISSES => $info['keyspace_misses'], |
163
|
2 |
|
Cache::STATS_UPTIME => $info['uptime_in_seconds'], |
164
|
2 |
|
Cache::STATS_MEMORY_USAGE => $info['used_memory'], |
165
|
2 |
|
Cache::STATS_MEMORY_AVAILABLE => false |
166
|
2 |
|
]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Returns the serializer constant to use. If Redis is compiled with |
171
|
|
|
* igbinary support, that is used. Otherwise the default PHP serializer is |
172
|
|
|
* used. |
173
|
|
|
* |
174
|
|
|
* @return integer One of the Redis::SERIALIZER_* constants |
175
|
|
|
*/ |
176
|
80 |
|
protected function getSerializerValue() |
177
|
|
|
{ |
178
|
80 |
|
if (defined('HHVM_VERSION')) { |
179
|
|
|
return Redis::SERIALIZER_PHP; |
180
|
|
|
} |
181
|
|
|
|
182
|
80 |
|
if (defined('Redis::SERIALIZER_IGBINARY') && extension_loaded('igbinary')) { |
183
|
|
|
return Redis::SERIALIZER_IGBINARY; |
184
|
|
|
} |
185
|
|
|
|
186
|
80 |
|
return Redis::SERIALIZER_PHP; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|