Skip to content

Commit c27cc5b

Browse files
committed
Added SRANDMEMBER, reported by Virtuall on IRC.
1 parent fb5776a commit c27cc5b

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

README.markdown

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,23 @@ $redis->sAdd('key1' , 'set2');
502502
$redis->sAdd('key1' , 'set3'); /* 'key1' => {'set3', 'set1', 'set2'}*/
503503
$redis->sPop('key1'); /* 'set1', 'key1' => {'set3', 'set2'} */
504504
$redis->sPop('key1'); /* 'set3', 'key1' => {'set2'} */
505+
</pre>
505506

507+
## sRandMember
508+
##### *Description*
509+
Returns a random element from the set value at Key, without removing it.
510+
##### *Parameters*
511+
*key*
512+
##### *Return value*
513+
*String* value from the set
514+
*Bool* `FALSE` if set identified by key is empty or doesn't exist.
515+
##### *Example*
516+
<pre>
517+
$redis->sAdd('key1' , 'set1');
518+
$redis->sAdd('key1' , 'set2');
519+
$redis->sAdd('key1' , 'set3'); /* 'key1' => {'set3', 'set1', 'set2'}*/
520+
$redis->sRandMember('key1'); /* 'set1', 'key1' => {'set3', 'set1', 'set2'} */
521+
$redis->sRandMember('key1'); /* 'set3', 'key1' => {'set3', 'set1', 'set2'} */
506522
</pre>
507523

508524
## sInter

php_redis.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ PHP_METHOD(Redis, sSize);
5959
PHP_METHOD(Redis, sRemove);
6060
PHP_METHOD(Redis, sMove);
6161
PHP_METHOD(Redis, sPop);
62+
PHP_METHOD(Redis, sRandMember);
6263
PHP_METHOD(Redis, sContains);
6364
PHP_METHOD(Redis, sMembers);
6465
PHP_METHOD(Redis, sInter);

redis.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ static zend_function_entry redis_functions[] = {
8383
PHP_ME(Redis, sRemove, NULL, ZEND_ACC_PUBLIC)
8484
PHP_ME(Redis, sMove, NULL, ZEND_ACC_PUBLIC)
8585
PHP_ME(Redis, sPop, NULL, ZEND_ACC_PUBLIC)
86+
PHP_ME(Redis, sRandMember, NULL, ZEND_ACC_PUBLIC)
8687
PHP_ME(Redis, sContains, NULL, ZEND_ACC_PUBLIC)
8788
PHP_ME(Redis, sMembers, NULL, ZEND_ACC_PUBLIC)
8889
PHP_ME(Redis, sInter, NULL, ZEND_ACC_PUBLIC)
@@ -1601,6 +1602,15 @@ PHP_METHOD(Redis, sPop)
16011602
}
16021603
/* }}} */
16031604

1605+
/* }}} */
1606+
/* {{{ proto string Redis::sRandMember(string key)
1607+
*/
1608+
PHP_METHOD(Redis, sRandMember)
1609+
{
1610+
generic_pop_function(INTERNAL_FUNCTION_PARAM_PASSTHRU, "SRANDMEMBER", 11 TSRMLS_CC);
1611+
}
1612+
/* }}} */
1613+
16041614
/* {{{ proto boolean Redis::sContains(string set, string value)
16051615
*/
16061616
PHP_METHOD(Redis, sContains)

tests/TestRedis.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,13 +760,35 @@ public function testsPop()
760760
$this->redis->sAdd('set0', 'val2');
761761

762762
$v0 = $this->redis->sPop('set0');
763+
$this->assertTrue(1 === $this->redis->sSize('set0'));
763764
$this->assertTrue($v0 === 'val' || $v0 === 'val2');
764765
$v1 = $this->redis->sPop('set0');
766+
$this->assertTrue(0 === $this->redis->sSize('set0'));
765767
$this->assertTrue(($v0 === 'val' && $v1 === 'val2') || ($v1 === 'val' && $v0 === 'val2'));
766768

767769
$this->assertTrue($this->redis->sPop('set0') === FALSE);
768770
}
769771

772+
public function testsRandMember() {
773+
$this->redis->delete('set0');
774+
$this->assertTrue($this->redis->sRandMember('set0') === FALSE);
775+
776+
$this->redis->sAdd('set0', 'val');
777+
$this->redis->sAdd('set0', 'val2');
778+
779+
$got = array();
780+
while(true) {
781+
$v = $this->redis->sRandMember('set0');
782+
$this->assertTrue(2 === $this->redis->sSize('set0')); // no change.
783+
$this->assertTrue($v === 'val' || $v === 'val2');
784+
785+
$got[$v] = $v;
786+
if(count($got) == 2) {
787+
break;
788+
}
789+
}
790+
}
791+
770792
public function testsContains()
771793
{
772794
$this->redis->delete('set');

0 commit comments

Comments
 (0)