Extended Key Space support by implementing COPY command (#866)

* Added support for COPY command

* Revert getCLient signature changes, used createClient instead

Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local>
This commit is contained in:
Vladyslav Vildanov
2022-12-29 22:18:08 +02:00
committed by GitHub
parent b47aa7ad3a
commit b8cc7c643a
9 changed files with 363 additions and 1 deletions
+1
View File
@@ -18,6 +18,7 @@ use Predis\Command\CommandInterface;
/**
* Interface defining a client-side context such as a pipeline or transaction.
*
* @method $this copy(string $source, string $destination, int $db = -1, bool $replace = false)
* @method $this del(array|string $keys)
* @method $this dump($key)
* @method $this exists($key)
+1
View File
@@ -27,6 +27,7 @@ use Predis\Response\Status;
* and more friendly interface to ease programming which is described in the
* following list of methods:
*
* @method int copy(string $source, string $destination, int $db = -1, bool $replace = false)
* @method int del(string[]|string $keyOrKeys, string ...$keys = null)
* @method string|null dump(string $key)
* @method int exists(string $key)
+36
View File
@@ -0,0 +1,36 @@
<?php
namespace Predis\Command\Redis;
use Predis\Command\Command as RedisCommand;
use Predis\Command\Traits\DB;
use Predis\Command\Traits\Replace;
/**
* @link https://redis.io/commands/copy/
*
* This command copies the value stored at the source key to the destination key.
*/
class COPY extends RedisCommand
{
use DB {
DB::setArguments as setDB;
}
use Replace {
Replace::setArguments as setReplace;
}
protected static $dbArgumentPositionOffset = 2;
public function getId()
{
return 'COPY';
}
public function setArguments(array $arguments)
{
$this->setDB($arguments);
$arguments = $this->getArguments();
$this->setReplace($arguments);
}
}
+41
View File
@@ -0,0 +1,41 @@
<?php
namespace Predis\Command\Traits;
use UnexpectedValueException;
trait DB
{
private $dbModifier = 'DB';
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (static::$dbArgumentPositionOffset >= $argumentsLength) {
parent::setArguments($arguments);
return;
}
if (!is_numeric($arguments[static::$dbArgumentPositionOffset])) {
throw new UnexpectedValueException('DB argument should be a valid numeric value');
}
if ($arguments[static::$dbArgumentPositionOffset] < 0) {
array_splice($arguments, static::$dbArgumentPositionOffset, 1);
parent::setArguments($arguments);
return;
}
$argument = $arguments[static::$dbArgumentPositionOffset];
$argumentsBefore = array_slice($arguments, 0, static::$dbArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$dbArgumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
[$this->dbModifier],
[$argument],
$argumentsAfter
));
}
}
+24
View File
@@ -0,0 +1,24 @@
<?php
namespace Predis\Command\Traits;
use Predis\Command\Command;
/**
* @mixin Command
*/
trait Replace
{
public function setArguments(array $arguments)
{
$replace = array_pop($arguments);
if (is_bool($replace) && $replace) {
$arguments[] = 'REPLACE';
} else if (!is_bool($replace)) {
$arguments[] = $replace;
}
parent::setArguments($arguments);
}
}