Skip to content
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

Handle non-array return values for RedisCluster#mget #75

Merged
merged 1 commit into from
Sep 13, 2023
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
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