We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
Virtuall
1 parent fb5776a commit c27cc5bCopy full SHA for c27cc5b
README.markdown
@@ -502,7 +502,23 @@ $redis->sAdd('key1' , 'set2');
502
$redis->sAdd('key1' , 'set3'); /* 'key1' => {'set3', 'set1', 'set2'}*/
503
$redis->sPop('key1'); /* 'set1', 'key1' => {'set3', 'set2'} */
504
$redis->sPop('key1'); /* 'set3', 'key1' => {'set2'} */
505
+</pre>
506
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'} */
522
</pre>
523
524
## sInter
php_redis.h
@@ -59,6 +59,7 @@ PHP_METHOD(Redis, sSize);
59
PHP_METHOD(Redis, sRemove);
60
PHP_METHOD(Redis, sMove);
61
PHP_METHOD(Redis, sPop);
62
+PHP_METHOD(Redis, sRandMember);
63
PHP_METHOD(Redis, sContains);
64
PHP_METHOD(Redis, sMembers);
65
PHP_METHOD(Redis, sInter);
redis.c
@@ -83,6 +83,7 @@ static zend_function_entry redis_functions[] = {
83
PHP_ME(Redis, sRemove, NULL, ZEND_ACC_PUBLIC)
84
PHP_ME(Redis, sMove, NULL, ZEND_ACC_PUBLIC)
85
PHP_ME(Redis, sPop, NULL, ZEND_ACC_PUBLIC)
86
+ PHP_ME(Redis, sRandMember, NULL, ZEND_ACC_PUBLIC)
87
PHP_ME(Redis, sContains, NULL, ZEND_ACC_PUBLIC)
88
PHP_ME(Redis, sMembers, NULL, ZEND_ACC_PUBLIC)
89
PHP_ME(Redis, sInter, NULL, ZEND_ACC_PUBLIC)
@@ -1601,6 +1602,15 @@ PHP_METHOD(Redis, sPop)
1601
1602
}
1603
/* }}} */
1604
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
+
1614
/* {{{ proto boolean Redis::sContains(string set, string value)
1615
*/
1616
PHP_METHOD(Redis, sContains)
tests/TestRedis.php
@@ -760,13 +760,35 @@ public function testsPop()
760
$this->redis->sAdd('set0', 'val2');
761
762
$v0 = $this->redis->sPop('set0');
763
+ $this->assertTrue(1 === $this->redis->sSize('set0'));
764
$this->assertTrue($v0 === 'val' || $v0 === 'val2');
765
$v1 = $this->redis->sPop('set0');
766
+ $this->assertTrue(0 === $this->redis->sSize('set0'));
767
$this->assertTrue(($v0 === 'val' && $v1 === 'val2') || ($v1 === 'val' && $v0 === 'val2'));
768
769
$this->assertTrue($this->redis->sPop('set0') === FALSE);
770
771
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
792
public function testsContains()
793
{
794
$this->redis->delete('set');
0 commit comments