Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fe09260

Browse files
committed
Added doc & tests for SETRANGE.
1 parent 7b368a0 commit fe09260

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

README.markdown

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,25 @@ $redis->getRange('key', 0, 5); /* 'string' */
13921392
$redis->getRange('key', -5, -1); /* 'value' */
13931393
</pre>
13941394

1395+
## setRange
1396+
##### *Description*
1397+
Changes a substring of a larger string.
1398+
1399+
##### *Parameters*
1400+
*key*
1401+
*offset*
1402+
*value*
1403+
1404+
##### *Return value*
1405+
*STRING*: the length of the string after it was modified.
1406+
1407+
##### *Example*
1408+
<pre>
1409+
$redis->set('key', 'Hello world');
1410+
$redis->setRange('key', 6, "redis"); /* returns 11 */
1411+
$redis->get('key'); /* "Hello redis" */
1412+
</pre>
1413+
13951414
## strlen
13961415
##### *Description*
13971416
Get the length of a string value.

tests/TestRedis.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,9 @@ public function testStr() {
453453
$this->assertTrue($this->redis->get('keyNotExist') === 'value');
454454

455455
$this->redis->set('key', 'This is a string') ;
456-
$this->assertTrue($this->redis->substr('key', 0, 3) === 'This');
457-
$this->assertTrue($this->redis->substr('key', -6, -1) === 'string');
458-
$this->assertTrue($this->redis->substr('key', -6, 100000) === 'string');
456+
$this->assertTrue($this->redis->getRange('key', 0, 3) === 'This');
457+
$this->assertTrue($this->redis->getRange('key', -6, -1) === 'string');
458+
$this->assertTrue($this->redis->getRange('key', -6, 100000) === 'string');
459459
$this->assertTrue($this->redis->get('key') === 'This is a string');
460460

461461
$this->redis->set('key', 'This is a string') ;
@@ -1778,6 +1778,25 @@ public function testHashes() {
17781778

17791779
}
17801780

1781+
public function testSetRange() {
1782+
1783+
$this->redis->delete('key');
1784+
$this->redis->set('key', 'hello world');
1785+
$this->redis->setRange('key', 6, 'redis');
1786+
$this->assertTrue('hello redis' === $this->redis->get('key'));
1787+
$this->redis->setRange('key', 6, 'you'); // don't cut off the end
1788+
$this->assertTrue('hello youis' === $this->redis->get('key'));
1789+
1790+
$this->assertTrue(FALSE === $this->redis->setRange('key', -1, 'plop')); // doesn't work with negative offsets
1791+
1792+
// fill with zeros if needed
1793+
$this->redis->delete('key');
1794+
$this->redis->setRange('key', 6, 'foo');
1795+
$this->assertTrue("\x00\x00\x00\x00\x00\x00foo" === $this->redis->get('key'));
1796+
1797+
1798+
}
1799+
17811800
public function testMultiExec() {
17821801
$this->sequence(Redis::MULTI);
17831802

0 commit comments

Comments
 (0)