Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpFoundation][Cache][Messenger] Replace redis "DEL" commands with "UNLINK" #37993

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
Sep 27, 2020
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ protected function doDeleteYieldTags(array $ids): iterable
{
$lua = <<<'EOLUA'
local v = redis.call('GET', KEYS[1])
redis.call('DEL', KEYS[1])
local e = redis.pcall('UNLINK', KEYS[1])

if type(e) ~= 'number' then
redis.call('DEL', KEYS[1])
end

if not v or v:len() <= 13 or v:byte(1) ~= 0x9D or v:byte(6) ~= 0 or v:byte(10) ~= 0x5F then
return ''
Expand Down
23 changes: 20 additions & 3 deletions src/Symfony/Component/Cache/Traits/RedisTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Cache\Traits;

use Predis\Command\Redis\UNLINK;
use Predis\Connection\Aggregate\ClusterInterface;
use Predis\Connection\Aggregate\RedisCluster;
use Predis\Response\Status;
Expand Down Expand Up @@ -363,7 +364,8 @@ protected function doClear(string $namespace)
// 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, 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;
$unlink = version_compare($info['redis_version'], '4.0', '>=') ? 'UNLINK' : 'DEL';
$cleared = $host->eval("local keys=redis.call('KEYS',ARGV[1]..'*') for i=1,#keys,5000 do redis.call('$unlink',unpack(keys,i,math.min(i+4999,#keys))) end return 1", $evalArgs[0], $evalArgs[1]) && $cleared;
continue;
}

Expand Down Expand Up @@ -393,12 +395,27 @@ protected function doDelete(array $ids)
}

if ($this->redis instanceof \Predis\ClientInterface && $this->redis->getConnection() instanceof ClusterInterface) {
$this->pipeline(function () use ($ids) {
static $del;
$del = $del ?? (class_exists(UNLINK::class) ? 'unlink' : 'del');

$this->pipeline(function () use ($ids, $del) {
foreach ($ids as $id) {
yield 'del' => [$id];
yield $del => [$id];
}
})->rewind();
} else {
static $unlink = true;

if ($unlink) {
try {
$this->redis->unlink($ids);

return true;
} catch (\Throwable $e) {
$unlink = false;
}
}

$this->redis->del($ids);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ protected function doWrite(string $sessionId, string $data): bool
*/
protected function doDestroy(string $sessionId): bool
{
static $unlink = true;

if ($unlink) {
try {
$this->redis->unlink($this->prefix.$sessionId);

return true;
} catch (\Throwable $e) {
$unlink = false;
}
}

$this->redis->del($this->prefix.$sessionId);

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,19 @@ private function getCurrentTimeInMilliseconds(): int

public function cleanup(): void
{
static $unlink = true;

if ($unlink) {
try {
$this->connection->unlink($this->stream);
$this->connection->unlink($this->queue);

return;
} catch (\Throwable $e) {
$unlink = false;
}
}

$this->connection->del($this->stream);
$this->connection->del($this->queue);
}
Expand Down