Skip to content

Commit 049727f

Browse files
committed
Added ZCOUNT + tests & docs.
1 parent 6d6b133 commit 049727f

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

README.markdown

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,24 @@ $redis->zRangeByScore('key', 0, 3, array('limit' => array(1, 1)); /* array('val2
12841284
$redis->zRangeByScore('key', 0, 3, array('withscores' => TRUE, 'limit' => array(1, 1)); /* array('val2' => 2) */
12851285
</pre>
12861286

1287+
## zCount
1288+
##### *Description*
1289+
Returns the *number* of elements of the sorted set stored at the specified key which have scores in the range [start,end].
1290+
##### *Parameters*
1291+
*key*
1292+
*start*: double
1293+
*end*: double
1294+
1295+
##### *Return value*
1296+
*LONG* the size of a corresponding zRangeByScore.
1297+
##### *Example*
1298+
<pre>
1299+
$redis->zAdd('key', 0, 'val0');
1300+
$redis->zAdd('key', 2, 'val2');
1301+
$redis->zAdd('key', 10, 'val10');
1302+
$redis->zCount('key', 0, 3); /* 2, corresponding to array('val0', 'val2') */
1303+
</pre>
1304+
12871305
## zDeleteRangeByScore, zRemoveRangeByScore
12881306
##### *Description*
12891307
Deletes the elements of the sorted set stored at the specified key which have scores in the range [start,end].

php_redis.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ PHP_METHOD(Redis, zDelete);
8585
PHP_METHOD(Redis, zRange);
8686
PHP_METHOD(Redis, zReverseRange);
8787
PHP_METHOD(Redis, zRangeByScore);
88+
PHP_METHOD(Redis, zCount);
8889
PHP_METHOD(Redis, zDeleteRangeByScore);
8990
PHP_METHOD(Redis, zCard);
9091
PHP_METHOD(Redis, zScore);

redis.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ static zend_function_entry redis_functions[] = {
113113
PHP_ME(Redis, zRange, NULL, ZEND_ACC_PUBLIC)
114114
PHP_ME(Redis, zReverseRange, NULL, ZEND_ACC_PUBLIC)
115115
PHP_ME(Redis, zRangeByScore, NULL, ZEND_ACC_PUBLIC)
116+
PHP_ME(Redis, zCount, NULL, ZEND_ACC_PUBLIC)
116117
PHP_ME(Redis, zDeleteRangeByScore, NULL, ZEND_ACC_PUBLIC)
117118
PHP_ME(Redis, zCard, NULL, ZEND_ACC_PUBLIC)
118119
PHP_ME(Redis, zScore, NULL, ZEND_ACC_PUBLIC)
@@ -3081,6 +3082,54 @@ PHP_METHOD(Redis, zRangeByScore)
30813082
}
30823083
/* }}} */
30833084

3085+
/* {{{ proto array Redis::zCount(string key, int start , int end)
3086+
*/
3087+
PHP_METHOD(Redis, zCount)
3088+
{
3089+
zval *object;
3090+
3091+
RedisSock *redis_sock;
3092+
char *key = NULL, *cmd;
3093+
int key_len, cmd_len, response_len;
3094+
double start, end;
3095+
3096+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osdd",
3097+
&object, redis_ce,
3098+
&key, &key_len, &start, &end) == FAILURE) {
3099+
RETURN_FALSE;
3100+
}
3101+
3102+
if (redis_sock_get(object, &redis_sock TSRMLS_CC) < 0) {
3103+
RETURN_FALSE;
3104+
}
3105+
3106+
cmd_len = redis_cmd_format(&cmd,
3107+
"*4" _NL
3108+
3109+
"$6" _NL
3110+
"ZCOUNT" _NL
3111+
3112+
"$%d" _NL /* key_len */
3113+
"%s" _NL /* key */
3114+
3115+
"$%d" _NL /* start_len */
3116+
"%F" _NL /* start */
3117+
3118+
"$%d" _NL /* end_len */
3119+
"%F" _NL /* end */
3120+
3121+
, key_len, key, key_len
3122+
, double_length(start), start
3123+
, double_length(end), end);
3124+
3125+
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
3126+
IF_ATOMIC() {
3127+
redis_long_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL TSRMLS_CC);
3128+
}
3129+
REDIS_PROCESS_RESPONSE(redis_long_response);
3130+
}
3131+
/* }}} */
3132+
30843133
/* {{{ proto long Redis::zCard(string key)
30853134
*/
30863135
PHP_METHOD(Redis, zCard)

tests/TestRedis.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,11 +1365,13 @@ public function testZX() {
13651365

13661366
$zero_to_three = $this->redis->zRangeByScore('key', 0, 3);
13671367
$this->assertTrue(array('val0', 'val1', 'val2', 'aal3', 'val3') === $zero_to_three || array('val0', 'val1', 'val2', 'val3', 'aal3') === $zero_to_three);
1368+
$this->assertTrue(5 === $this->redis->zCount('key', 0, 3));
13681369

13691370
// withscores
13701371
$this->redis->zRemove('key', 'aal3');
13711372
$zero_to_three = $this->redis->zRangeByScore('key', 0, 3, array('withscores' => TRUE));
13721373
$this->assertTrue(array('val0' => 0, 'val1' => 1, 'val2' => 2, 'val3' => 3) == $zero_to_three);
1374+
$this->assertTrue(4 === $this->redis->zCount('key', 0, 3));
13731375

13741376
// limit
13751377
$this->assertTrue(array('val0') === $this->redis->zRangeByScore('key', 0, 3, array('limit' => array(0, 1))));

0 commit comments

Comments
 (0)