Skip to content

Commit d93b135

Browse files
committed
Added ZREVRANGEBYSCORE.
1 parent 4c237c7 commit d93b135

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

README.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,9 +1775,9 @@ $redis->zReverseRange('key', 0, -1); /* array('val10', 'val2', 'val0') */
17751775
$redis->zReverseRange('key', 0, -1, true); /* array('val10' => 10, 'val2' => 2, 'val0' => 0) */
17761776
</pre>
17771777

1778-
## zRangeByScore
1778+
## zRangeByScore, zRevRangeByScore
17791779
##### *Description*
1780-
Returns the elements of the sorted set stored at the specified key which have scores in the range [start,end]. Adding a parenthesis before `start` or `end` excludes it from the range. +inf and -inf are also valid limits.
1780+
Returns the elements of the sorted set stored at the specified key which have scores in the range [start,end]. Adding a parenthesis before `start` or `end` excludes it from the range. +inf and -inf are also valid limits. zRevRangeByScore returns the same items in reverse order, when the `start` and `end` parameters are swapped.
17811781
##### *Parameters*
17821782
*key*
17831783
*start*: string

php_redis.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ PHP_METHOD(Redis, zDelete);
103103
PHP_METHOD(Redis, zRange);
104104
PHP_METHOD(Redis, zReverseRange);
105105
PHP_METHOD(Redis, zRangeByScore);
106+
PHP_METHOD(Redis, zRevRangeByScore);
106107
PHP_METHOD(Redis, zCount);
107108
PHP_METHOD(Redis, zDeleteRangeByScore);
108109
PHP_METHOD(Redis, zCard);

redis.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ static zend_function_entry redis_functions[] = {
133133
PHP_ME(Redis, zRange, NULL, ZEND_ACC_PUBLIC)
134134
PHP_ME(Redis, zReverseRange, NULL, ZEND_ACC_PUBLIC)
135135
PHP_ME(Redis, zRangeByScore, NULL, ZEND_ACC_PUBLIC)
136+
PHP_ME(Redis, zRevRangeByScore, NULL, ZEND_ACC_PUBLIC)
136137
PHP_ME(Redis, zCount, NULL, ZEND_ACC_PUBLIC)
137138
PHP_ME(Redis, zDeleteRangeByScore, NULL, ZEND_ACC_PUBLIC)
138139
PHP_ME(Redis, zCard, NULL, ZEND_ACC_PUBLIC)
@@ -3257,10 +3258,10 @@ PHP_METHOD(Redis, zReverseRange)
32573258
}
32583259
}
32593260
/* }}} */
3260-
/* {{{ proto array Redis::zRangeByScore(string key, string start , string end [,array options = NULL])
3261-
*/
3262-
PHP_METHOD(Redis, zRangeByScore)
3263-
{
3261+
3262+
PHPAPI void
3263+
redis_generic_zrange_by_score(INTERNAL_FUNCTION_PARAMETERS, char *keyword TSRMLS_DC) {
3264+
32643265
zval *object, *z_options = NULL, **z_limit_val_pp = NULL, **z_withscores_val_pp = NULL;
32653266

32663267
RedisSock *redis_sock;
@@ -3314,18 +3315,18 @@ PHP_METHOD(Redis, zRangeByScore)
33143315
int key_free = redis_key_prefix(redis_sock, &key, &key_len);
33153316
if(withscores) {
33163317
if(has_limit) {
3317-
cmd_len = redis_cmd_format_static(&cmd, "ZRANGEBYSCORE", "ssssdds",
3318+
cmd_len = redis_cmd_format_static(&cmd, keyword, "ssssdds",
33183319
key, key_len, start, start_len, end, end_len, "LIMIT", 5, limit_low, limit_high, "WITHSCORES", 10);
33193320
} else {
3320-
cmd_len = redis_cmd_format_static(&cmd, "ZRANGEBYSCORE", "ssss",
3321+
cmd_len = redis_cmd_format_static(&cmd, keyword, "ssss",
33213322
key, key_len, start, start_len, end, end_len, "WITHSCORES", 10);
33223323
}
33233324
} else {
33243325
if(has_limit) {
3325-
cmd_len = redis_cmd_format_static(&cmd, "ZRANGEBYSCORE", "ssssdd",
3326+
cmd_len = redis_cmd_format_static(&cmd, keyword, "ssssdd",
33263327
key, key_len, start, start_len, end, end_len, "LIMIT", 5, limit_low, limit_high);
33273328
} else {
3328-
cmd_len = redis_cmd_format_static(&cmd, "ZRANGEBYSCORE", "sss", key, key_len, start, start_len, end, end_len);
3329+
cmd_len = redis_cmd_format_static(&cmd, keyword, "sss", key, key_len, start, start_len, end, end_len);
33293330
}
33303331
}
33313332
if(key_free) efree(key);
@@ -3352,7 +3353,20 @@ PHP_METHOD(Redis, zRangeByScore)
33523353
REDIS_PROCESS_RESPONSE(redis_sock_read_multibulk_reply);
33533354
}
33543355
}
3356+
3357+
/* {{{ proto array Redis::zRangeByScore(string key, string start , string end [,array options = NULL])
3358+
*/
3359+
PHP_METHOD(Redis, zRangeByScore)
3360+
{
3361+
redis_generic_zrange_by_score(INTERNAL_FUNCTION_PARAM_PASSTHRU, "ZRANGEBYSCORE" TSRMLS_CC);
3362+
}
33553363
/* }}} */
3364+
/* {{{ proto array Redis::zRevRangeByScore(string key, string start , string end [,array options = NULL])
3365+
*/
3366+
PHP_METHOD(Redis, zRevRangeByScore)
3367+
{
3368+
redis_generic_zrange_by_score(INTERNAL_FUNCTION_PARAM_PASSTHRU, "ZREVRANGEBYSCORE" TSRMLS_CC);
3369+
}
33563370

33573371
/* {{{ proto array Redis::zCount(string key, string start , string end)
33583372
*/

tests/TestRedis.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,10 @@ public function testZX() {
15921592

15931593
$zero_to_three = $this->redis->zRangeByScore('key', 0, 3);
15941594
$this->assertTrue(array('val0', 'val1', 'val2', 'aal3', 'val3') === $zero_to_three || array('val0', 'val1', 'val2', 'val3', 'aal3') === $zero_to_three);
1595+
1596+
$three_to_zero = $this->redis->zRevRangeByScore('key', 3, 0);
1597+
$this->assertTrue(array_reverse(array('val0', 'val1', 'val2', 'aal3', 'val3')) === $three_to_zero || array_reverse(array('val0', 'val1', 'val2', 'val3', 'aal3')) === $three_to_zero);
1598+
15951599
$this->assertTrue(5 === $this->redis->zCount('key', 0, 3));
15961600

15971601
// withscores
@@ -1606,6 +1610,11 @@ public function testZX() {
16061610
$this->assertTrue(array('val1', 'val2') === $this->redis->zRangeByScore('key', 0, 3, array('limit' => array(1, 2))));
16071611
$this->assertTrue(array('val0', 'val1') === $this->redis->zRangeByScore('key', 0, 1, array('limit' => array(0, 100))));
16081612

1613+
$this->assertTrue(array('val3') === $this->redis->zRevRangeByScore('key', 3, 0, array('limit' => array(0, 1))));
1614+
$this->assertTrue(array('val3', 'val2') === $this->redis->zRevRangeByScore('key', 3, 0, array('limit' => array(0, 2))));
1615+
$this->assertTrue(array('val2', 'val1') === $this->redis->zRevRangeByScore('key', 3, 0, array('limit' => array(1, 2))));
1616+
$this->assertTrue(array('val1', 'val0') === $this->redis->zRevRangeByScore('key', 1, 0, array('limit' => array(0, 100))));
1617+
16091618
$this->assertTrue(4 === $this->redis->zSize('key'));
16101619
$this->assertTrue(1.0 === $this->redis->zScore('key', 'val1'));
16111620
$this->assertFalse($this->redis->zScore('key', 'val'));

0 commit comments

Comments
 (0)