8000 [Cache] Use SCAN instead of KEYS with Redis >= 2.8 · symfony/symfony@bf57c04 · GitHub
[go: up one dir, main page]

Skip to content

Commit bf57c04

Browse files
[Cache] Use SCAN instead of KEYS with Redis >= 2.8
1 parent 49a2cb8 commit bf57c04

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/Symfony/Component/Cache/Adapter/RedisAdapter.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ protected function doClear($namespace)
181181
return false;
182182
}
183183
} elseif ($this->redis instanceof \RedisArray) {
184+
$hosts = array();
184185
foreach ($this->redis->_hosts() as $host) {
185186
$hosts[] = $this->redis->_instance($host);
186187
}
@@ -190,13 +191,31 @@ protected function doClear($namespace)
190191
foreach ($hosts as $host) {
191192
if (!isset($namespace[0])) {
192193
$host->flushDb();
193-
} else {
194+
continue;
195+
}
196+
197+
$info = $host->info('Server');
198+
$info = isset($info['Server']) ? $info['Server'] : $info;
199+
200+
if (!version_compare($info['redis_version'], '2.8', '>=')) {
194201
// As documented in Redis documentation (http://redis.io/commands/keys) using KEYS
195202
// can hang your server when it is executed against large databases (millions of items).
196-
// Whenever you hit this scale, it is advised to deploy one Redis database per cache pool
197-
// instead of using namespaces, so that FLUSHDB is used instead.
203+
// Whenever you hit this scale, you should really consider upgrading to Redis 2.8 or above.
198204
$host->eval("local keys=redis.call('KEYS',ARGV[1]..'*') for i=1,#keys,5000 do redis.call('DEL',unpack(keys,i,math.min(i+4999,#keys))) end", $evalArgs[0], $evalArgs[1]);
205+
continue;
199206
}
207+
208+
$cursor = null;
209+
do {
210+
$keys = $host instanceof \Predis\Client ? $host->scan($cursor, 'MATCH', $namespace.'*', 'COUNT', 5000) : $host->scan($cursor, $namespace.'*', 5000);
211+
if (isset($keys[1]) && is_array($keys[1])) {
212+
$cursor = $keys[0];
213+
$keys = $keys[1];
214+
}
215+
if ($keys) {
216+
$host->del($keys);
217+
}
218+
} while ($cursor = (int) $cursor);
200219
}
201220

202221
return true;

0 commit comments

Comments
 (0)
0