Skip to content

Commit

Permalink
Redis adapter improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Dec 5, 2023
1 parent 25b48ad commit 03849dc
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/Support/RedisAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public function xadd(string $key, array $dictionary): string|Pipeline|PhpRedis
*/
public function xrange(string $key, string $start, string $end, int $count = null): array
{
return collect($this->handle([
return collect($this->handle([ // @phpstan-ignore return.type argument.templateType argument.templateType
'XRANGE',
$this->config->get('database.redis.options.prefix').$key,
$start,
$end,
...$count !== null ? ['COUNT', "$count"] : [],
]))->mapWithKeys(fn ($value, $key) => [
$value[0] => collect($value[1])
$value[0] => collect($value[1]) // @phpstan-ignore argument.templateType argument.templateType
->chunk(2)
->map->values()
->mapWithKeys(fn ($value, $key) => [$value[0] => $value[1]])
Expand All @@ -74,7 +74,7 @@ public function xtrim(string $key, string $strategy, string $strategyModifier, s
$this->config->get('database.redis.options.prefix').$key,
$strategy,
$strategyModifier,
$threshold,
(string) $threshold,
]);
}

Expand Down Expand Up @@ -106,13 +106,15 @@ public function pipeline(callable $closure): array

/**
* Run the given command.
*
* @param list<string> $args
*/
protected function handle($args): mixed
protected function handle(array $args): mixed
{
try {
return tap($this->run($args), function ($result) {
if ($result === false && $this->client() instanceof PhpRedis) {
throw new RedisClientException($this->client()->getLastError());
throw new RedisClientException($this->client()->getLastError() ?? 'An unknown error occurred.');
}
});
} catch (PredisServerException $e) {
Expand All @@ -122,13 +124,15 @@ protected function handle($args): mixed

/**
* Run the given command.
*
* @param list<string> $args
*/
protected function run($args): mixed
protected function run(array $args): mixed
{
return match (true) {
$this->client() instanceof PhpRedis => $this->client()->rawCommand(...$args),
$this->client() instanceof Predis,
$this->client() instanceof Pipeline => $this->client()->executeCommand(new RawCommand($args)),
$this->client() instanceof Pipeline => $this->client()->executeCommand(RawCommand::create(...$args)),
};
}

Expand Down

0 comments on commit 03849dc

Please sign in to comment.