Przeglądaj źródła

New command: SETRANGE (Redis v2.2-dev).

Daniele Alessandri 14 lat temu
rodzic
commit
cc16311950
3 zmienionych plików z 28 dodań i 0 usunięć
  1. 5 0
      lib/Predis.php
  2. 1 0
      test/PredisShared.php
  3. 22 0
      test/RedisCommandsTest.php

+ 5 - 0
lib/Predis.php

@@ -1823,6 +1823,7 @@ class RedisServer_vNext extends RedisServer_v2_0 {
 
             /* commands operating on string values */
             'strlen'                    => '\Predis\Commands\Strlen',
+            'setrange'                  => '\Predis\Commands\SetRange',
             'getrange'                  => '\Predis\Commands\Substr',
 
             /* commands operating on the key space */
@@ -2358,6 +2359,10 @@ class Append extends \Predis\MultiBulkCommand {
     public function getCommandId() { return 'APPEND'; }
 }
 
+class SetRange extends \Predis\MultiBulkCommand {
+    public function getCommandId() { return 'SETRANGE'; }
+}
+
 class Substr extends \Predis\MultiBulkCommand {
     public function getCommandId() { return 'SUBSTR'; }
 }

+ 1 - 0
test/PredisShared.php

@@ -20,6 +20,7 @@ class RC {
     const EXCEPTION_WRONG_TYPE     = 'Operation against a key holding the wrong kind of value';
     const EXCEPTION_NO_SUCH_KEY    = 'no such key';
     const EXCEPTION_OUT_OF_RANGE   = 'index out of range';
+    const EXCEPTION_OFFSET_RANGE   = 'offset is out of range';
     const EXCEPTION_INVALID_DB_IDX = 'invalid DB index';
     const EXCEPTION_VALUE_NOT_INT  = 'value is not an integer';
     const EXCEPTION_EXEC_NO_MULTI  = 'EXEC without MULTI';

+ 22 - 0
test/RedisCommandsTest.php

@@ -223,6 +223,28 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
         });
     }
 
+    function testSetRange() {
+        $this->assertEquals(6, $this->redis->setrange('var', 0, 'foobar'));
+        $this->assertEquals('foobar', $this->redis->get('var'));
+        $this->assertEquals(6, $this->redis->setrange('var', 3, 'foo'));
+        $this->assertEquals('foofoo', $this->redis->get('var'));
+        $this->assertEquals(16, $this->redis->setrange('var', 10, 'barbar'));
+        $this->assertEquals("foofoo\x00\x00\x00\x00barbar", $this->redis->get('var'));
+
+        $this->assertEquals(4, $this->redis->setrange('binary', 0, pack('l', -2147483648)));
+        list($unpacked) = array_values(unpack('l', $this->redis->get('binary')));
+        $this->assertEquals(-2147483648, $unpacked);
+
+        RC::testForServerException($this, RC::EXCEPTION_OFFSET_RANGE, function($test) {
+            $test->redis->setrange('var', -1, 'bogus');
+        });
+
+        RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
+            $test->redis->rpush('metavars', 'foo');
+            $test->redis->setrange('metavars', 0, 'hoge');
+        });
+    }
+
     function testSubstr() {
         $this->redis->set('var', 'foobar');
         $this->assertEquals('foo', $this->redis->substr('var', 0, 2));