Skip to content

Commit ffc134a

Browse files
jonashremnicolas-grekas
authored andcommitted
[HttpFoundation][Cache][Messenger] Replace redis "DEL" commands with "UNLINK"
1 parent 2abc465 commit ffc134a

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,11 @@ protected function doDeleteYieldTags(array $ids): iterable
149149
{
150150
$lua = <<<'EOLUA'
151151
local v = redis.call('GET', KEYS[1])
152-
redis.call('DEL', KEYS[1])
152+
local e = redis.pcall('UNLINK', KEYS[1])
153+
154+
if type(e) ~= 'number' then
155+
redis.call('DEL', KEYS[1])
156+
end
153157
154158
if not v or v:len() <= 13 or v:byte(1) ~= 0x9D or v:byte(6) ~= 0 or v:byte(10) ~= 0x5F then
155159
return ''

src/Symfony/Component/Cache/Traits/RedisTrait.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Cache\Traits;
1313

14+
use Predis\Command\Redis\UNLINK;
1415
use Predis\Connection\Aggregate\ClusterInterface;
1516
use Predis\Connection\Aggregate\RedisCluster;
1617
use Predis\Response\Status;
@@ -363,7 +364,8 @@ protected function doClear(string $namespace)
363364
// As documented in Redis documentation (http://redis.io/commands/keys) using KEYS
364365
// can hang your server when it is executed against large databases (millions of items).
365366
// Whenever you hit this scale, you should really consider upgrading to Redis 2.8 or above.
366-
$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;
367+
$unlink = version_compare($info['redis_version'], '4.0', '>=') ? 'UNLINK' : 'DEL';
368+
$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;
367369
continue;
368370
}
369371

@@ -393,12 +395,27 @@ protected function doDelete(array $ids)
393395
}
394396

395397
if ($this->redis instanceof \Predis\ClientInterface && $this->redis->getConnection() instanceof ClusterInterface) {
398+
static $del;
399+
$del = $del ?? (class_exists(UNLINK::class) ? 'unlink' : 'del');
400+
396401
$this->pipeline(function () use ($ids) {
397402
foreach ($ids as $id) {
398-
yield 'del' => [$id];
403+
yield $del => [$id];
399404
}
400405
})->rewind();
401406
} else {
407+
static $unlink = true;
408+
409+
if ($unlink) {
410+
try {
411+
$this->redis->unlink($ids);
412+
413+
return true;
414+
} catch (\Throwable $e) {
415+
$unlink = false;
416+
}
417+
}
418+
402419
$this->redis->del($ids);
403420
}
404421

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/RedisSessionHandler.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ protected function doWrite(string $sessionId, string $data): bool
8989
*/
9090
protected function doDestroy(string $sessionId): bool
9191
{
92+
static $unlink = true;
93+
94+
if ($unlink) {
95+
try {
96+
$this->redis->unlink($this->prefix.$sessionId);
97+
98+
return true;
99+
} catch (\Throwable $e) {
100+
$unlink = false;
101+
}
102+
}
103+
92104
$this->redis->del($this->prefix.$sessionId);
93105

94106
return true;

src/Symfony/Component/Messenger/Bridge/Redis/Transport/Connection.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,19 @@ private function getCurrentTimeInMilliseconds(): int
460460

461461
public function cleanup(): void
462462
{
463+
static $unlink = true;
464+
465+
if ($unlink) {
466+
try {
467+
$this->connection->unlink($this->stream);
468+
$this->connection->unlink($this->queue);
469+
470+
return;
471+
} catch (\Throwable $e) {
472+
$unlink = false;
473+
}
474+
}
475+
463476
$this->connection->del($this->stream);
464477
$this->connection->del($this->queue);
465478
}

0 commit comments

Comments
 (0)