8000 [Cache] Use SCAN instead of KEYS with Redis >= 2.8 by nicolas-grekas · Pull Request #19551 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] Use SCAN instead of KEYS with Redis >= 2.8 #19551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions src/Symfony/Component/Cache/Adapter/RedisAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ protected function doClear($namespace)
// When using a native Redis cluster, clearing the cache cannot work and always returns false.
// Clearing the cache should then be done by any other means (e.g. by restarting the cluster).

$cleared = true;
$hosts = array($this->redis);
$evalArgs = array(array($namespace), 0);

Expand All @@ -181,6 +182,7 @@ protected function doClear($namespace)
return false;
}
} elseif ($this->redis instanceof \RedisArray) {
$hosts = array();
foreach ($this->redis->_hosts() as $host) {
$hosts[] = $this->redis->_instance($host);
}
Expand All @@ -189,17 +191,35 @@ protected function doClear($namespace)
}
foreach ($hosts as $host) {
if (!isset($namespace[0])) {
$host->flushDb();
} else {
$cleared = $host->flushDb() && $cleared;
continue;
}

$info = $host->info('Server');
$info = isset($info['Server']) ? $info['Server'] : $info;

if (!version_compare($info['redis_version'], '2.8', '>=')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for curiosity, can't you do: version_compare($info['redis_version'], '2.8', '<') ?

Copy link
Member Author

Choose a re 8000 ason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same yes

// As documented in Redis documentation (http://redis.io/commands/keys) using KEYS
// can hang your server when it is executed against large databases (millions of items).
// Whenever you hit this scale, it is advised to deploy one Redis database per cache pool
// instead of using namespaces, so that FLUSHDB is used instead.
$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]);
// Whenever you hit this scale, you should really consider upgrading to Redis 2.8 or above.
$cleared = $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 return 1", $evalArgs[0], $evalArgs[1]) && $cleared;
continue;
}

$cursor = null;
do {
$keys = $host instanceof \Predis\Client ? $host->scan($cursor, 'MATCH', $namespace.'*', 'COUNT', 1000) : $host->scan($cursor, $namespace.'*', 1000);
if (isset($keys[1]) && is_array($keys[1])) {
$cursor = $keys[0];
$keys = $keys[1];
}
if ($keys) {
$host->del($keys);
}
} while ($cursor = (int) $cursor);
}

return true;
return $cleared;
}

/**
Expand Down
0