Skip to content

Commit

Permalink
Fix prefixes for LMOVE and BLMOVE (predis#1455)
Browse files Browse the repository at this point in the history
Co-authored-by: Till Krüss <tillkruss@users.noreply.github.com>
  • Loading branch information
marein and tillkruss committed May 2, 2024
1 parent 4dc7245 commit cbef710
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Command/Processor/KeyPrefixProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function __construct($prefix)
$this->prefix = $prefix;

$prefixFirst = static::class . '::first';
$prefixFirstTwo = static::class . '::firstTwo';
$prefixAll = static::class . '::all';
$prefixInterleaved = static::class . '::interleaved';
$prefixSkipFirst = static::class . '::skipFirst';
Expand Down Expand Up @@ -198,6 +199,8 @@ public function __construct($prefix)
/* ---------------- Redis 6.2 ---------------- */
'GETDEL' => $prefixFirst,
'ZMSCORE' => $prefixFirst,
'LMOVE' => $prefixFirstTwo,
'BLMOVE' => $prefixFirstTwo,
'GEOSEARCH' => $prefixFirst,

/* ---------------- Redis 7.0 ---------------- */
Expand Down Expand Up @@ -396,6 +399,24 @@ public static function first(CommandInterface $command, $prefix)
}
}

/**
* Applies the specified prefix only to the first two arguments.
*
* @param CommandInterface $command Command instance.
* @param string $prefix Prefix string.
*/
public static function firstTwo(CommandInterface $command, $prefix)
{
$arguments = $command->getArguments();
$length = min(count($arguments), 2);

for ($i = 0; $i < $length; $i++) {
$arguments[$i] = "$prefix{$arguments[$i]}";
}

$command->setRawArguments($arguments);
}

/**
* Applies the specified prefix to all the arguments.
*
Expand Down
39 changes: 39 additions & 0 deletions tests/Predis/Command/Processor/KeyPrefixProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,37 @@ public function testPrefixFirst(): void
$this->assertEmpty($command->getArguments());
}

/**
* @group disconnected
*/
public function testPrefixFirstTwo(): void
{
$arguments = ['1st', '2nd', '3rd', '4th'];
$expected = ['prefix:1st', 'prefix:2nd', '3rd', '4th'];

$command = $this->getMockForAbstractClass('Predis\Command\Command');
$command->setRawArguments($arguments);

KeyPrefixProcessor::firstTwo($command, 'prefix:');
$this->assertSame($expected, $command->getArguments());

// One argument
$arguments = ['1st'];
$expected = ['prefix:1st'];

$command = $this->getMockForAbstractClass('Predis\Command\Command');
$command->setRawArguments($arguments);

KeyPrefixProcessor::firstTwo($command, 'prefix:');
$this->assertSame($expected, $command->getArguments());

// Empty arguments
$command = $this->getMockForAbstractClass('Predis\Command\Command');

KeyPrefixProcessor::firstTwo($command, 'prefix:');
$this->assertEmpty($command->getArguments());
}

/**
* @group disconnected
*/
Expand Down Expand Up @@ -978,6 +1009,14 @@ public function commandArgumentsDataProvider(): array
['key'],
['prefix:key'],
],
['LMOVE',
['key:source', 'key:destination', 'left', 'right'],
['prefix:key:source', 'prefix:key:destination', 'left', 'right'],
],
['BLMOVE',
['key:source', 'key:destination', 'left', 'right', 10],
['prefix:key:source', 'prefix:key:destination', 'left', 'right', 10],
],
/* ---------------- Redis 7.0 ---------------- */
['EXPIRETIME',
['key'],
Expand Down

0 comments on commit cbef710

Please sign in to comment.