Skip to content

Commit

Permalink
bugfix: handle non-array return values for RedisCluster#mget
Browse files Browse the repository at this point in the history
Signed-off-by: Maximilian Bösing <[email protected]>
  • Loading branch information
boesing committed Jul 3, 2023
1 parent 81aea68 commit 5c07761
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/Exception/RedisRuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

final class RedisRuntimeException extends LaminasCacheRuntimeException
{
private const INTERNAL_REDIS_ERROR = 'Something went wrong while interacting with redis cluster.';

public static function fromClusterException(RedisClusterException $exception, RedisCluster $redis): self
{
$message = $redis->getLastError() ?? $exception->getMessage();
Expand All @@ -39,4 +41,15 @@ public static function fromRedisException(RedisException $exception, Redis $redi

return new self($message, $exception->getCode(), $exception);
}

public static function fromInternalRedisError(RedisCluster|Redis $redis): self
{
try {
$message = $redis->getLastError() ?? self::INTERNAL_REDIS_ERROR;
} catch (RedisException) {
$message = self::INTERNAL_REDIS_ERROR;
}

return new self($message);
}
}
14 changes: 13 additions & 1 deletion src/RedisCluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@

use function array_key_exists;
use function array_values;
use function assert;
use function count;
use function in_array;
use function is_array;
use function is_int;
use function sprintf;
use function version_compare;

Expand Down Expand Up @@ -192,14 +195,23 @@ protected function internalGetItems(array &$normalizedKeys): array
$redis = $this->getRedisResource();

try {
/** @var array<int,mixed> $resultsByIndex */
$resultsByIndex = $redis->mget($namespacedKeys);
} catch (RedisClusterException $exception) {
throw $this->clusterException($exception, $redis);
}

/**
* @link https://github.com/phpredis/phpredis/blob/35a7cc094c6c264aa37738b074c4c54c4ca73b87/redis_cluster.stub.php#L621
*
* @psalm-suppress TypeDoesNotContainType RedisCluster#mget can return `false` on error
*/
if (! is_array($resultsByIndex)) {
throw RedisRuntimeException::fromInternalRedisError($redis);
}

$result = [];
foreach ($resultsByIndex as $keyIndex => $value) {
assert(is_int($keyIndex));
$normalizedKey = $normalizedKeys[$keyIndex];
$namespacedKey = $namespacedKeys[$keyIndex];
if ($value === false && ! $this->isFalseReturnValuePersisted($redis, $namespacedKey)) {
Expand Down

0 comments on commit 5c07761

Please sign in to comment.