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

Skip to content
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Lock/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
7.2
---

* RedisStore uses `EVALSHA` over `EVAL` when evaluating LUA scripts
* Add `NullStore`

7.0
Expand Down
64 changes: 57 additions & 7 deletions src/Symfony/Component/Lock/Store/RedisStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class RedisStore implements SharedLockStoreInterface
{
use ExpiringStoreTrait;

private const NO_SCRIPT_ERROR_MESSAGE = 'NOSCRIPT No matching script. Please use EVAL.';

private bool $supportTime;

/**
Expand Down Expand Up @@ -226,11 +228,32 @@ public function exists(Key $key): bool

private function evaluate(string $script, string $resource, array $args): mixed
{
$scriptSha = sha1($script);

if ($this->redis instanceof \Redis || $this->redis instanceof Relay || $this->redis instanceof \RedisCluster) {
$this->redis->clearLastError();
$result = $this->redis->eval($script, array_merge([$resource], $args), 1);
if (null !== $err = $this->redis->getLastError()) {
throw new LockStorageException($err);

$result = $this->redis->evalSha($scriptSha, array_merge([$resource], $args), 1);
if (self::NO_SCRIPT_ERROR_MESSAGE === $err = $this->redis->getLastError()) {
$this->redis->clearLastError();

if ($this->redis instanceof \RedisCluster) {
foreach ($this->redis->_masters() as $master) {
$this->redis->script($master, 'LOAD', $script);
}
} else {
$this->redis->script('LOAD', $script);
}

if (null !== $err = $this->redis->getLastError()) {
throw new LockStorageException($err);
}

$result = $this->redis->evalSha($scriptSha, array_merge([$resource], $args), 1);

if (null !== $err = $this->redis->getLastError()) {
throw new LockStorageException($err);
}
}

return $result;
Expand All @@ -239,9 +262,21 @@ private function evaluate(string $script, string $resource, array $args): mixed
if ($this->redis instanceof \RedisArray) {
$client = $this->redis->_instance($this->redis->_target($resource));
$client->clearLastError();
$result = $client->eval($script, array_merge([$resource], $args), 1);
if (null !== $err = $client->getLastError()) {
throw new LockStorageException($err);
$result = $client->evalSha($scriptSha, array_merge([$resource], $args), 1);
if (self::NO_SCRIPT_ERROR_MESSAGE === $err = $client->getLastError()) {
$client->clearLastError();

$client->script('LOAD', $script);

if (null !== $err = $client->getLastError()) {
throw new LockStorageException($err);
}

$result = $client->evalSha($scriptSha, array_merge([$resource], $args), 1);

if (null !== $err = $client->getLastError()) {
throw new LockStorageException($err);
}
}

return $result;
Expand All @@ -250,7 +285,22 @@ private function evaluate(string $script, string $resource, array $args): mixed
\assert($this->redis instanceof \Predis\ClientInterface);

try {
return $this->redis->eval(...array_merge([$script, 1, $resource], $args));
return $this->redis->evalSha($scriptSha, 1, $resource, ...$args);
} catch (ServerException $e) {
// Fallthrough only if we need to load the script
if (self::NO_SCRIPT_ERROR_MESSAGE !== $e->getMessage()) {
throw new LockStorageException($e->getMessage(), $e->getCode(), $e);
}
}

try {
$this->redis->script('LOAD', $script);
} catch (ServerException $e) {
throw new LockStorageException($e->getMessage(), $e->getCode(), $e);
}

try {
return $this->redis->evalSha($scriptSha, 1, $resource, ...$args);
} catch (ServerException $e) {
throw new LockStorageException($e->getMessage(), $e->getCode(), $e);
}
Expand Down
Loading