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

Skip to content

Commit 765b9a7

Browse files
committed
Added BRPOPLPUSH.
1 parent 1eef6a6 commit 765b9a7

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

README.markdown

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,6 +1718,18 @@ array(3) {
17181718
}
17191719
</pre>
17201720

1721+
## brpoplpush
1722+
##### *Description*
1723+
A blocking version of `rpoplpush`, with an integral timeout in the third parameter.
1724+
1725+
##### *Parameters*
1726+
*Key*: srckey
1727+
*Key*: dstkey
1728+
*Long*: timeout
1729+
1730+
##### *Return value*
1731+
*STRING* The element that was moved in case of success, `FALSE` in case of timeout.
1732+
17211733

17221734
## zAdd
17231735
##### *Description*

php_redis.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ PHP_METHOD(Redis, object);
122122
PHP_METHOD(Redis, mset);
123123
PHP_METHOD(Redis, msetnx);
124124
PHP_METHOD(Redis, rpoplpush);
125+
PHP_METHOD(Redis, brpoplpush);
125126

126127
PHP_METHOD(Redis, hGet);
127128
PHP_METHOD(Redis, hSet);

redis.c

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ static zend_function_entry redis_functions[] = {
130130
PHP_ME(Redis, mset, NULL, ZEND_ACC_PUBLIC)
131131
PHP_ME(Redis, msetnx, NULL, ZEND_ACC_PUBLIC)
132132
PHP_ME(Redis, rpoplpush, NULL, ZEND_ACC_PUBLIC)
133+
PHP_ME(Redis, brpoplpush, NULL, ZEND_ACC_PUBLIC)
133134
PHP_ME(Redis, zAdd, NULL, ZEND_ACC_PUBLIC)
134135
PHP_ME(Redis, zDelete, NULL, ZEND_ACC_PUBLIC)
135136
PHP_ME(Redis, zRange, NULL, ZEND_ACC_PUBLIC)
@@ -3074,14 +3075,37 @@ PHP_METHOD(Redis, msetnx) {
30743075
}
30753076
/* }}} */
30763077

3078+
PHPAPI void common_rpoplpush(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
3079+
char *srckey, int srckey_len, char *dstkey, int dstkey_len, int timeout) {
3080+
3081+
char *cmd;
3082+
int cmd_len;
3083+
3084+
int srckey_free = redis_key_prefix(redis_sock, &srckey, &srckey_len TSRMLS_CC);
3085+
int dstkey_free = redis_key_prefix(redis_sock, &dstkey, &dstkey_len TSRMLS_CC);
3086+
if(timeout < 0) {
3087+
cmd_len = redis_cmd_format_static(&cmd, "RPOPLPUSH", "ss", srckey, srckey_len, dstkey, dstkey_len);
3088+
} else {
3089+
cmd_len = redis_cmd_format_static(&cmd, "BRPOPLPUSH", "ssd", srckey, srckey_len, dstkey, dstkey_len, timeout);
3090+
}
3091+
if(srckey_free) efree(srckey);
3092+
if(dstkey_free) efree(dstkey);
3093+
3094+
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
3095+
IF_ATOMIC() {
3096+
redis_string_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
3097+
}
3098+
REDIS_PROCESS_RESPONSE(redis_string_response);
3099+
3100+
}
30773101

30783102
/* {{{ proto string Redis::rpoplpush(string srckey, string dstkey)
30793103
*/
30803104
PHP_METHOD(Redis, rpoplpush)
30813105
{
30823106
zval *object;
30833107
RedisSock *redis_sock;
3084-
char *srckey = NULL, *dstkey = NULL, *cmd;
3108+
char *srckey = NULL, *dstkey = NULL;
30853109
int srckey_len, dstkey_len, cmd_len;
30863110

30873111
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oss",
@@ -3094,19 +3118,34 @@ PHP_METHOD(Redis, rpoplpush)
30943118
RETURN_FALSE;
30953119
}
30963120

3097-
int srckey_free = redis_key_prefix(redis_sock, &srckey, &srckey_len TSRMLS_CC);
3098-
int dstkey_free = redis_key_prefix(redis_sock, &dstkey, &dstkey_len TSRMLS_CC);
3099-
cmd_len = redis_cmd_format_static(&cmd, "RPOPLPUSH", "ss", srckey, srckey_len, dstkey, dstkey_len);
3100-
if(srckey_free) efree(srckey);
3101-
if(dstkey_free) efree(dstkey);
3121+
common_rpoplpush(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, srckey, srckey_len, dstkey, dstkey_len, -1);
3122+
}
3123+
/* }}} */
31023124

3103-
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
3104-
IF_ATOMIC() {
3105-
redis_string_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
3106-
}
3107-
REDIS_PROCESS_RESPONSE(redis_string_response);
3125+
/* {{{ proto string Redis::brpoplpush(string srckey, string dstkey)
3126+
*/
3127+
PHP_METHOD(Redis, brpoplpush)
3128+
{
3129+
zval *object;
3130+
RedisSock *redis_sock;
3131+
char *srckey = NULL, *dstkey = NULL;
3132+
int srckey_len, dstkey_len;
3133+
long timeout = 0;
3134+
3135+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ossl",
3136+
&object, redis_ce, &srckey, &srckey_len,
3137+
&dstkey, &dstkey_len, &timeout) == FAILURE) {
3138+
RETURN_FALSE;
3139+
}
3140+
3141+
if (redis_sock_get(object, &redis_sock TSRMLS_CC) < 0) {
3142+
RETURN_FALSE;
3143+
}
3144+
3145+
common_rpoplpush(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, srckey, srckey_len, dstkey, dstkey_len, timeout);
31083146
}
31093147
/* }}} */
3148+
31103149
/* {{{ proto long Redis::zAdd(string key, int score, string value)
31113150
*/
31123151
PHP_METHOD(Redis, zAdd) {

0 commit comments

Comments
 (0)