diff --git a/CHANGELOG.md b/CHANGELOG.md index 10e0ede6..3afe75cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - Added testing for subkey notification channels (#1671) - Added support for new array commands (#1672) - Added support for INCREX command (#1674) +- Added support for UNLINK command (#1680) ### Changed - Include command name in unsupported container command error messages (#1653) diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index d3eada45..0425ef2d 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -77,6 +77,7 @@ use Predis\Command\Redis\VADD; * @method $this sort_ro(string $key, ?string $byPattern = null, ?LimitOffsetCount $limit = null, array $getPatterns = [], ?string $sorting = null, bool $alpha = false) * @method $this ttl($key) * @method $this type($key) + * @method $this unlink(string ...$keys) * @method $this append($key, $value) * @method $this arcount(string $key) * @method $this ardel(string $key, int ...$index) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index ceadec02..1a3fd8e1 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -87,6 +87,7 @@ use Predis\Response\Status; * @method array sort_ro(string $key, ?string $byPattern = null, ?LimitOffsetCount $limit = null, array $getPatterns = [], ?string $sorting = null, bool $alpha = false) * @method int ttl(string $key) * @method mixed type(string $key) + * @method int unlink(string ...$keys) * @method int append(string $key, $value) * @method int arcount(string $key) * @method int ardel(string $key, int ...$index) diff --git a/src/Cluster/ClusterStrategy.php b/src/Cluster/ClusterStrategy.php index 6ed3a8d8..16064b06 100644 --- a/src/Cluster/ClusterStrategy.php +++ b/src/Cluster/ClusterStrategy.php @@ -42,6 +42,7 @@ abstract class ClusterStrategy implements StrategyInterface /* commands operating on the key space */ 'EXISTS' => $getKeyFromAllArguments, 'DEL' => $getKeyFromAllArguments, + 'UNLINK' => $getKeyFromAllArguments, 'TYPE' => $getKeyFromFirstArgument, 'EXPIRE' => $getKeyFromFirstArgument, 'EXPIREAT' => $getKeyFromFirstArgument, diff --git a/src/Command/Redis/UNLINK.php b/src/Command/Redis/UNLINK.php new file mode 100644 index 00000000..c9c0fd07 --- /dev/null +++ b/src/Command/Redis/UNLINK.php @@ -0,0 +1,34 @@ +applyPrefixForAllArguments($prefix); + } +} diff --git a/tests/Predis/Cluster/PredisStrategyTest.php b/tests/Predis/Cluster/PredisStrategyTest.php index 3be751e5..1679f210 100644 --- a/tests/Predis/Cluster/PredisStrategyTest.php +++ b/tests/Predis/Cluster/PredisStrategyTest.php @@ -369,6 +369,7 @@ class PredisStrategyTest extends PredisTestCase /* commands operating on the key space */ 'EXISTS' => 'keys-all', 'DEL' => 'keys-all', + 'UNLINK' => 'keys-all', 'TYPE' => 'keys-first', 'EXPIRE' => 'keys-first', 'EXPIREAT' => 'keys-first', diff --git a/tests/Predis/Cluster/RedisStrategyTest.php b/tests/Predis/Cluster/RedisStrategyTest.php index 9e4a9595..d23b53c3 100644 --- a/tests/Predis/Cluster/RedisStrategyTest.php +++ b/tests/Predis/Cluster/RedisStrategyTest.php @@ -392,6 +392,7 @@ class RedisStrategyTest extends PredisTestCase /* commands operating on the key space */ 'EXISTS' => 'keys-all', 'DEL' => 'keys-all', + 'UNLINK' => 'keys-all', 'TYPE' => 'keys-first', 'EXPIRE' => 'keys-first', 'EXPIREAT' => 'keys-first', diff --git a/tests/Predis/Command/Redis/UNLINK_Test.php b/tests/Predis/Command/Redis/UNLINK_Test.php new file mode 100644 index 00000000..6122ff27 --- /dev/null +++ b/tests/Predis/Command/Redis/UNLINK_Test.php @@ -0,0 +1,120 @@ +getCommand(); + $command->setArguments($arguments); + $this->assertSame($expected, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $command = $this->getCommand(); + + $this->assertSame(10, $command->parseResponse(10)); + } + + /** + * @group disconnected + */ + public function testPrefixKeys(): void + { + /** @var PrefixableCommand $command */ + $command = $this->getCommand(); + $actualArguments = ['arg1', 'arg2', 'arg3', 'arg4']; + $prefix = 'prefix:'; + $expectedArguments = ['prefix:arg1', 'prefix:arg2', 'prefix:arg3', 'prefix:arg4']; + + $command->setArguments($actualArguments); + $command->prefixKeys($prefix); + + $this->assertSame($expectedArguments, $command->getArguments()); + } + + /** + * @group connected + * @requiresRedisVersion >= 4.0.0 + */ + public function testReturnsNumberOfUnlinkedKeys(): void + { + $redis = $this->getClient(); + + $this->assertSame(0, $redis->unlink('foo')); + + $redis->set('foo', 'bar'); + $this->assertSame(1, $redis->unlink('foo')); + + $redis->set('foo', 'bar'); + $this->assertSame(1, $redis->unlink('foo', 'hoge')); + + $redis->set('foo', 'bar'); + $redis->set('hoge', 'piyo'); + $this->assertSame(2, $redis->unlink('foo', 'hoge')); + } + + /** + * @group connected + * @requiresRedisVersion >= 6.0.0 + */ + public function testReturnsNumberOfUnlinkedKeysResp3(): void + { + $redis = $this->getResp3Client(); + + $this->assertSame(0, $redis->unlink('foo')); + + $redis->set('foo', 'bar'); + $this->assertSame(1, $redis->unlink('foo')); + + $redis->set('foo', 'bar'); + $this->assertSame(1, $redis->unlink('foo', 'hoge')); + + $redis->set('foo', 'bar'); + $redis->set('hoge', 'piyo'); + $this->assertSame(2, $redis->unlink('foo', 'hoge')); + } +}