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

Skip to content

Commit 3754f3c

Browse files
author
Nicolas Favre-Felix
committed
Added RPOPLPUSH + tests.
1 parent f9bcff0 commit 3754f3c

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

php_redis.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ PHP_METHOD(Redis, ttl);
7979
PHP_METHOD(Redis, info);
8080
PHP_METHOD(Redis, select);
8181
PHP_METHOD(Redis, move);
82+
8283
PHP_METHOD(Redis, mset);
84+
PHP_METHOD(Redis, rpoplpush);
8385

8486
#ifdef PHP_WIN32
8587
#define PHP_REDIS_API __declspec(dllexport)

redis.c

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ zend_function_entry redis_functions[] = {
9595

9696
/* 1.1 */
9797
PHP_ME(Redis, mset, NULL, ZEND_ACC_PUBLIC)
98+
PHP_ME(Redis, rpoplpush, NULL, ZEND_ACC_PUBLIC)
9899

99100
/* aliases */
100101
PHP_MALIAS(Redis, open, connect, NULL, ZEND_ACC_PUBLIC)
@@ -704,7 +705,7 @@ PHP_METHOD(Redis, set)
704705
zval *object;
705706
RedisSock *redis_sock;
706707
char *key = NULL, *val = NULL, *cmd;
707-
int key_len, val_len, cmd_len, count;
708+
int key_len, val_len, cmd_len;
708709

709710
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oss",
710711
&object, redis_ce, &key, &key_len,
@@ -734,7 +735,7 @@ PHP_METHOD(Redis, setnx)
734735
zval *object;
735736
RedisSock *redis_sock;
736737
char *key = NULL, *val = NULL, *cmd;
737-
int key_len, val_len, cmd_len, count;
738+
int key_len, val_len, cmd_len;
738739

739740
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oss",
740741
&object, redis_ce, &key, &key_len,
@@ -1790,7 +1791,7 @@ PHPAPI int generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAMETERS, char *keyword
17901791
{
17911792
zval *object, **z_args;
17921793
char **keys, *cmd;
1793-
int cmd_len, count, *keys_len;
1794+
int cmd_len, *keys_len;
17941795
int i, argc = ZEND_NUM_ARGS();
17951796

17961797
if(argc < min_argc) {
@@ -2066,7 +2067,7 @@ PHP_METHOD(Redis, setTimeout) {
20662067
zval *object;
20672068
RedisSock *redis_sock;
20682069
char *key = NULL, *cmd;
2069-
int key_len, cmd_len, count;
2070+
int key_len, cmd_len;
20702071
long timeout;
20712072

20722073
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osl",
@@ -2496,4 +2497,40 @@ PHP_METHOD(Redis, mset) {
24962497
}
24972498
/* }}} */
24982499

2500+
2501+
/* {{{ proto string Redis::rpoplpush(string srckey, string dstkey)
2502+
*/
2503+
PHP_METHOD(Redis, rpoplpush)
2504+
{
2505+
zval *object;
2506+
RedisSock *redis_sock;
2507+
char *srckey = NULL, *dstkey = NULL, *cmd, *response;
2508+
int srckey_len, dstkey_len, cmd_len, response_len;
2509+
2510+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oss",
2511+
&object, redis_ce, &srckey, &srckey_len,
2512+
&dstkey, &dstkey_len) == FAILURE) {
2513+
RETURN_FALSE;
2514+
}
2515+
2516+
if (redis_sock_get(object, &redis_sock TSRMLS_CC) < 0) {
2517+
RETURN_FALSE;
2518+
}
2519+
2520+
cmd_len = redis_cmd_format(&cmd, "RPOPLPUSH %s %d\r\n%s\r\n",
2521+
srckey, srckey_len,
2522+
dstkey_len,
2523+
dstkey, dstkey_len);
2524+
2525+
if (redis_sock_write(redis_sock, cmd, cmd_len) < 0) {
2526+
efree(cmd);
2527+
RETURN_FALSE;
2528+
}
2529+
efree(cmd);
2530+
if ((response = redis_sock_read(redis_sock, &response_len TSRMLS_CC)) == NULL) {
2531+
RETURN_FALSE;
2532+
}
2533+
RETURN_STRINGL(response, response_len, 0);
2534+
}
2535+
24992536
/* vim: set tabstop=4 expandtab: */

tests/TestRedis.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,28 @@ public function testMset() {
12671267

12681268
$this->assertFalse($this->redis->mset(array())); // set ø → FALSE
12691269
}
1270+
1271+
public function testRpopLpush() {
1272+
1273+
// standard case.
1274+
$this->redis->delete('x', 'y');
1275+
$this->redis->lpush('x', 'abc');
1276+
$this->redis->lpush('x', 'def'); // x = [def, abc]
1277+
1278+
$this->redis->lpush('y', '123');
1279+
$this->redis->lpush('y', '456'); // y = [456, 123]
1280+
1281+
$this->assertEquals($this->redis->rpoplpush('x', 'y'), 'abc'); // we RPOP x, yielding abc.
1282+
$this->assertEquals($this->redis->lgetRange('x', 0, -1), array('def')); // only def remains in x.
1283+
$this->assertEquals($this->redis->lgetRange('y', 0, -1), array('abc', '456', '123')); // abc has been lpushed to y.
1284+
1285+
// with an empty source, expecting no change.
1286+
$this->redis->delete('x', 'y');
1287+
$this->assertTrue(FALSE === $this->redis->rpoplpush('x', 'y'));
1288+
$this->assertTrue(array() === $this->redis->lgetRange('x', 0, -1));
1289+
$this->assertTrue(array() === $this->redis->lgetRange('y', 0, -1));
1290+
1291+
}
12701292
}
12711293

12721294
?>

0 commit comments

Comments
 (0)