diff --git a/src/Exception/RedisRuntimeException.php b/src/Exception/RedisRuntimeException.php index d2fe05a..b369549 100644 --- a/src/Exception/RedisRuntimeException.php +++ b/src/Exception/RedisRuntimeException.php @@ -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(); @@ -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); + } } diff --git a/src/RedisCluster.php b/src/RedisCluster.php index 7847cb6..4ed5584 100644 --- a/src/RedisCluster.php +++ b/src/RedisCluster.php @@ -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; @@ -192,14 +195,23 @@ protected function internalGetItems(array &$normalizedKeys): array $redis = $this->getRedisResource(); try { - /** @var array $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)) {