8000 Added BRPOPLPUSH. · jrtkcoder/phpredis@23f25ce · GitHub
[go: up one dir, main page]

Skip to content

Commit 23f25ce

Browse files
committed
Added BRPOPLPUSH.
1 parent 9ad4b9e commit 23f25ce

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
@@ -1724,6 +1724,18 @@ array(3) {
17241724
}
17251725
</pre>
17261726

1727+
## brpoplpush
1728+
##### *Description*
1729+
A blocking version of `rpoplpush`, with an integral timeout in the third parameter.
1730+
1731+
##### *Parameters*
1732+
*Key*: srckey
1733+
*Key*: dstkey
1734+
*Long*: timeout
1735+
1736+
##### *Return value*
1737+
*STRING* The element that was moved in case of success, `FALSE` in case of timeout.
1738+
17271739

17281740
## zAdd
17291741
##### *Description*

php_redis.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ 8000 -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)
@@ -3076,14 +3077,37 @@ PHP_METHOD(Redis, msetnx) {
30763077
}
30773078
/* }}} */
30783079

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

30803104
/* {{{ proto string Redis::rpoplpush(string srckey, string dstkey)
30813105
*/
30823106
PHP_METHOD(Redis, rpoplpush)
30833107
{
30843108
zval *object;
30853109
RedisSock *redis_sock;
3086-
char *srckey = NULL, *dstkey = NULL, *cmd;
3110+
char *srckey = NULL, *dstkey = NULL;
30873111
int srckey_len, dstkey_len, cmd_len;
30883112

30893113
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oss",
@@ -3096,19 +3120,34 @@ PHP_METHOD(Redis, rpoplpush)
30963120
RETURN_FALSE;
30973121
}
30983122

3099-
int srckey_free = redis_key_prefix(redis_sock, &srckey, &srckey_len TSRMLS_CC);
3100-
int dstkey_free = redis_key_prefix(redis_sock, &dstkey, &dstkey_len TSRMLS_CC);
3101-
cmd_len = redis_cmd_format_static(&cmd, "RPOPLPUSH", "ss", srckey, srckey_len, dstkey, dstkey_len);
3102-
if(srckey_free) efree(srckey);
3103-
if(dstkey_free) efree(dstkey);
3123+
common_rpoplpush(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, srckey, srckey_len, dstkey, dstkey_len, -1);
3124+
}
3125+
/* }}} */
31043126

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

0 commit comments

Comments
 (0)
0