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

Skip to content

Commit 46f0356

Browse files
committed
Add ZPOPMAX and ZPOPMIN support
1 parent 22d81a9 commit 46f0356

5 files changed

Lines changed: 81 additions & 0 deletions

File tree

php_redis.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ PHP_METHOD(Redis, zRevRank);
131131
PHP_METHOD(Redis, zIncrBy);
132132
PHP_METHOD(Redis, zInter);
133133
PHP_METHOD(Redis, zUnion);
134+
PHP_METHOD(Redis, zPopMax);
135+
PHP_METHOD(Redis, zPopMin);
134136
PHP_METHOD(Redis, expireAt);
135137
PHP_METHOD(Redis, pexpireAt);
136138
PHP_METHOD(Redis, bgrewriteaof);

redis.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ static zend_function_entry redis_functions[] = {
445445
PHP_ME(Redis, zScore, arginfo_key_member, ZEND_ACC_PUBLIC)
446446
PHP_ME(Redis, zUnion, arginfo_zstore, ZEND_ACC_PUBLIC)
447447
PHP_ME(Redis, zscan, arginfo_kscan, ZEND_ACC_PUBLIC)
448+
PHP_ME(Redis, zPopMax, arginfo_key, ZEND_ACC_PUBLIC)
449+
PHP_ME(Redis, zPopMin, arginfo_key, ZEND_ACC_PUBLIC)
448450
PHP_MALIAS(Redis, del, delete, arginfo_del, ZEND_ACC_PUBLIC)
449451
PHP_MALIAS(Redis, evaluate, eval, arginfo_eval, ZEND_ACC_PUBLIC)
450452
PHP_MALIAS(Redis, evaluateSha, evalsha, arginfo_evalsha, ZEND_ACC_PUBLIC)
@@ -2138,6 +2140,32 @@ PHP_METHOD(Redis, zUnion) {
21382140
REDIS_PROCESS_KW_CMD("ZUNIONSTORE", redis_zinter_cmd, redis_long_response);
21392141
}
21402142

2143+
/* {{{ proto array Redis::zPopMax(string key) */
2144+
PHP_METHOD(Redis, zPopMax)
2145+
{
2146+
if (ZEND_NUM_ARGS() == 1) {
2147+
REDIS_PROCESS_KW_CMD("ZPOPMAX", redis_key_cmd, redis_sock_read_multibulk_reply);
2148+
} else if (ZEND_NUM_ARGS() == 2) {
2149+
REDIS_PROCESS_KW_CMD("ZPOPMAX", redis_key_long_cmd, redis_sock_read_multibulk_reply);
2150+
} else {
2151+
ZEND_WRONG_PARAM_COUNT();
2152+
}
2153+
}
2154+
/* }}} */
2155+
2156+
/* {{{ proto array Redis::zPopMin(string key) */
2157+
PHP_METHOD(Redis, zPopMin)
2158+
{
2159+
if (ZEND_NUM_ARGS() == 1) {
2160+
REDIS_PROCESS_KW_CMD("ZPOPMIN", redis_key_cmd, redis_sock_read_multibulk_reply);
2161+
} else if (ZEND_NUM_ARGS() == 2) {
2162+
REDIS_PROCESS_KW_CMD("ZPOPMIN", redis_key_long_cmd, redis_sock_read_multibulk_reply);
2163+
} else {
2164+
ZEND_WRONG_PARAM_COUNT();
2165+
}
2166+
}
2167+
/* }}} */
2168+
21412169
/* hashes */
21422170

21432171
/* {{{ proto long Redis::hset(string key, string mem, string val) */

redis_cluster.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ zend_function_entry redis_cluster_functions[] = {
274274
PHP_ME(RedisCluster, zincrby, arginfo_zincrby, ZEND_ACC_PUBLIC)
275275
PHP_ME(RedisCluster, zinterstore, arginfo_zstore, ZEND_ACC_PUBLIC)
276276
PHP_ME(RedisCluster, zlexcount, arginfo_key_min_max, ZEND_ACC_PUBLIC)
277+
PHP_ME(RedisCluster, zpopmax, arginfo_key, ZEND_ACC_PUBLIC)
278+
PHP_ME(RedisCluster, zpopmin, arginfo_key, ZEND_ACC_PUBLIC)
277279
PHP_ME(RedisCluster, zrange, arginfo_zrange, ZEND_ACC_PUBLIC)
278280
PHP_ME(RedisCluster, zrangebylex, arginfo_zrangebylex, ZEND_ACC_PUBLIC)
279281
PHP_ME(RedisCluster, zrangebyscore, arginfo_zrangebyscore, ZEND_ACC_PUBLIC)
@@ -1844,6 +1846,30 @@ PHP_METHOD(RedisCluster, zremrangebylex) {
18441846
}
18451847
/* }}} */
18461848

1849+
/* {{{ proto array RedisCluster::zpopmax(string key) */
1850+
PHP_METHOD(RedisCluster, zpopmax) {
1851+
if (ZEND_NUM_ARGS() == 1) {
1852+
CLUSTER_PROCESS_KW_CMD("ZPOPMAX", redis_key_cmd, cluster_mbulk_resp, 0);
1853+
} else if (ZEND_NUM_ARGS() == 2) {
1854+
CLUSTER_PROCESS_KW_CMD("ZPOPMAX", redis_key_long_cmd, cluster_mbulk_resp, 0);
1855+
} else {
1856+
ZEND_WRONG_PARAM_COUNT();
1857+
}
1858+
}
1859+
/* }}} */
1860+
1861+
/* {{{ proto array RedisCluster::zpopmin(string key) */
1862+
PHP_METHOD(RedisCluster, zpopmin) {
1863+
if (ZEND_NUM_ARGS() == 1) {
1864+
CLUSTER_PROCESS_KW_CMD("ZPOPMIN", redis_key_cmd, cluster_mbulk_resp, 0);
1865+
} else if (ZEND_NUM_ARGS() == 2) {
1866+
CLUSTER_PROCESS_KW_CMD("ZPOPMIN", redis_key_long_cmd, cluster_mbulk_resp, 0);
1867+
} else {
1868+
ZEND_WRONG_PARAM_COUNT();
1869+
}
1870+
}
1871+
/* }}} */
1872+
18471873
/* {{{ proto RedisCluster::sort(string key, array options) */
18481874
PHP_METHOD(RedisCluster, sort) {
18491875
redisCluster *c = GET_CONTEXT();

redis_cluster.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ PHP_METHOD(RedisCluster, restore);
224224
PHP_METHOD(RedisCluster, setrange);
225225
PHP_METHOD(RedisCluster, smove);
226226
PHP_METHOD(RedisCluster, srandmember);
227+
PHP_METHOD(RedisCluster, zpopmin);
228+
PHP_METHOD(RedisCluster, zpopmax);
227229
PHP_METHOD(RedisCluster, zrange);
228230
PHP_METHOD(RedisCluster, zrevrange);
229231
PHP_METHOD(RedisCluster, zrangebyscore);

tests/RedisTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2409,6 +2409,29 @@ public function testZRemRangeByLex() {
24092409
$this->assertEquals($this->redis->zRemRangeByLex('key', '[a', '[c'), 2);
24102410
}
24112411

2412+
public function testZPop() {
2413+
if (version_compare($this->version, "5.0.0", "lt")) {
2414+
$this->MarkTestSkipped();
2415+
return;
2416+
}
2417+
2418+
// zPopMax and zPopMin without a COUNT argument
2419+
$this->redis->del('key');
2420+
$this->redis->zAdd('key', 0, 'a', 1, 'b', 2, 'c', 3, 'd', 4, 'e');
2421+
$this->assertTrue(array('e', '4') === $this->redis->zPopMax('key'));
2422+
$this->assertTrue(array('a', '0') === $this->redis->zPopMin('key'));
2423+
2424+
// zPopMax with a COUNT argument
2425+
$this->redis->del('key');
2426+
$this->redis->zAdd('key', 0, 'a', 1, 'b', 2, 'c', 3, 'd', 4, 'e');
2427+
$this->assertTrue(array('e', '4', 'd', '3', 'c', '2') === $this->redis->zPopMax('key', 3));
2428+
2429+
// zPopMin with a COUNT argument
2430+
$this->redis->del('key');
2431+
$this->redis->zAdd('key', 0, 'a', 1, 'b', 2, 'c', 3, 'd', 4, 'e');
2432+
$this->assertTrue(array('a', '0', 'b', '1', 'c', '2') === $this->redis->zPopMin('key', 3));
2433+
}
2434+
24122435
public function testHashes() {
24132436
$this->redis->del('h', 'key');
24142437
$this->assertTrue(0 === $this->redis->hLen('h'));

0 commit comments

Comments
 (0)