Przeglądaj źródła

New commands: RPUSHX, LPUSHX (Redis v2.2-dev).

Daniele Alessandri 14 lat temu
rodzic
commit
fc8f363fe5
2 zmienionych plików z 38 dodań i 0 usunięć
  1. 10 0
      lib/Predis.php
  2. 28 0
      test/RedisCommandsTest.php

+ 10 - 0
lib/Predis.php

@@ -1751,6 +1751,8 @@ class RedisServer_vNext extends RedisServer_v2_0 {
             'strlen'                    => '\Predis\Commands\Strlen',
 
             /* commands operating on lists */
+            'rpushx'                    => '\Predis\Commands\ListPushTailX',
+            'lpushx'                    => '\Predis\Commands\ListPushHeadX',
             'linsert'                   => '\Predis\Commands\ListInsert',
         ));
     }
@@ -2354,10 +2356,18 @@ class ListPushTail extends Command {
     public function getCommandId() { return 'RPUSH'; }
 }
 
+class ListPushTailX extends Command {
+    public function getCommandId() { return 'RPUSHX'; }
+}
+
 class ListPushHead extends Command {
     public function getCommandId() { return 'LPUSH'; }
 }
 
+class ListPushHeadX extends Command {
+    public function getCommandId() { return 'LPUSHX'; }
+}
+
 class ListLength extends Command {
     public function getCommandId() { return 'LLEN'; }
 }

+ 28 - 0
test/RedisCommandsTest.php

@@ -380,6 +380,20 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
         });
     }
 
+    function testPushTailX() {
+        $this->assertEquals(0, $this->redis->rpushx('numbers', 1));
+        $this->assertEquals(1, $this->redis->rpush('numbers', 2));
+        $this->assertEquals(2, $this->redis->rpushx('numbers', 3));
+
+        $this->assertEquals(2, $this->redis->llen('numbers'));
+        $this->assertEquals(array(2, 3), $this->redis->lrange('numbers', 0, -1));
+
+        RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
+            $test->redis->set('foo', 'bar');
+            $test->redis->rpushx('foo', 'bar');
+        });
+    }
+
     function testPushHead() {
         // NOTE: List push operations return the list length since Redis commit 520b5a3
         $this->assertEquals(1, $this->redis->lpush('metavars', 'foo'));
@@ -393,6 +407,20 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
         });
     }
 
+    function testPushHeadX() {
+        $this->assertEquals(0, $this->redis->lpushx('numbers', 1));
+        $this->assertEquals(1, $this->redis->lpush('numbers', 2));
+        $this->assertEquals(2, $this->redis->lpushx('numbers', 3));
+
+        $this->assertEquals(2, $this->redis->llen('numbers'));
+        $this->assertEquals(array(3, 2), $this->redis->lrange('numbers', 0, -1));
+
+        RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
+            $test->redis->set('foo', 'bar');
+            $test->redis->lpushx('foo', 'bar');
+        });
+    }
+
     function testListLength() {
         $this->assertEquals(1, $this->redis->rpush('metavars', 'foo'));
         $this->assertEquals(2, $this->redis->rpush('metavars', 'hoge'));