Sitelet https://github.com/phpredis/phpredis/commit/79c9d224
Skip to content

Commit 79c9d22

Browse files
Refactor rawCommand and WAIT
1 parent 5bcaaa5 commit 79c9d22

3 files changed

Lines changed: 35 additions & 75 deletions

File tree

‎redis.c‎

Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,40 +2523,7 @@ PHP_METHOD(Redis, slowlog) {
25232523

25242524
/* {{{ proto Redis::wait(int num_slaves, int ms) }}} */
25252525
PHP_METHOD(Redis, wait) {
2526-
zval *object;
2527-
RedisSock *redis_sock;
2528-
zend_long num_slaves, timeout;
2529-
char *cmd;
2530-
int cmd_len;
2531-
2532-
/* Make sure arguments are valid */
2533-
if(zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oll",
2534-
&object, redis_ce, &num_slaves, &timeout)
2535-
==FAILURE)
2536-
{
2537-
RETURN_FALSE;
2538-
}
2539-
2540-
/* Don't even send this to Redis if our args are negative */
2541-
if(num_slaves < 0 || timeout < 0) {
2542-
RETURN_FALSE;
2543-
}
2544-
2545-
/* Grab our socket */
2546-
if ((redis_sock = redis_sock_get(object, 0)) == NULL) {
2547-
RETURN_FALSE;
2548-
}
2549-
2550-
// Construct the command
2551-
cmd_len = REDIS_SPPRINTF(&cmd, "WAIT", "ll", num_slaves, timeout);
2552-
2553-
/* Kick it off */
2554-
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
2555-
if (IS_ATOMIC(redis_sock)) {
2556-
redis_long_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL,
2557-
NULL);
2558-
}
2559-
REDIS_PROCESS_RESPONSE(redis_long_response);
2526+
REDIS_PROCESS_KW_CMD("WAIT", redis_long_long_cmd, redis_long_response);
25602527
}
25612528

25622529
/*
@@ -2588,46 +2555,9 @@ PHP_METHOD(Redis, evalsha_ro) {
25882555
REDIS_PROCESS_KW_CMD("EVALSHA_RO", redis_eval_cmd, redis_read_raw_variant_reply);
25892556
}
25902557

2591-
/* {{{ proto status Redis::script('flush')
2592-
* {{{ proto status Redis::script('kill')
2593-
* {{{ proto string Redis::script('load', lua_script)
2594-
* {{{ proto int Reids::script('exists', script_sha1 [, script_sha2, ...])
2595-
*/
2558+
/* {{{ public function script($args...): mixed }}} */
25962559
PHP_METHOD(Redis, script) {
2597-
zval *z_args;
2598-
RedisSock *redis_sock;
2599-
smart_string cmd = {0};
2600-
int argc = ZEND_NUM_ARGS();
2601-
2602-
/* Attempt to grab our socket */
2603-
if (argc < 1 || (redis_sock = redis_sock_get(getThis(), 0)) == NULL) {
2604-
RETURN_FALSE;
2605-
}
2606-
2607-
/* Allocate an array big enough to store our arguments */
2608-
z_args = ecalloc(argc, sizeof(zval));
2609-
2610-
/* Make sure we can grab our arguments, we have a string directive */
2611-
if (zend_get_parameters_array(ht, argc, z_args) == FAILURE ||
2612-
redis_build_script_cmd(&cmd, argc, z_args) == NULL
2613-
) {
2614-
efree(z_args);
2615-
RETURN_FALSE;
2616-
}
2617-
2618-
/* Free our allocated arguments */
2619-
efree(z_args);
2620-
2621-
// Kick off our request
2622-
REDIS_PROCESS_REQUEST(redis_sock, cmd.c, cmd.len);
2623-
if (IS_ATOMIC(redis_sock)) {
2624-
if(redis_read_variant_reply(INTERNAL_FUNCTION_PARAM_PASSTHRU,
2625-
redis_sock, NULL, NULL) < 0)
2626-
{
2627-
RETURN_FALSE;
2628-
}
2629-
}
2630-
REDIS_PROCESS_RESPONSE(redis_read_variant_reply);
2560+
REDIS_PROCESS_KW_CMD("SCRIPT", redis_vararg_cmd, redis_read_variant_reply);
26312561
}
26322562

26332563
/* {{{ proto DUMP key */

‎redis_commands.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,34 @@ int redis_key_str_arr_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
18141814
VAL_TYPE_STRINGS, cmd, cmd_len, slot, ctx);
18151815
}
18161816

1817+
/* Generic function that takes one or more non-serialized arguments */
1818+
int redis_vararg_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
1819+
char *kw, char **cmd, int *cmd_len, short *slot,
1820+
void **ctx)
1821+
{
1822+
smart_string cmdstr = {0};
1823+
zval *argv = NULL;
1824+
zend_string *arg;
1825+
int argc = 0;
1826+
1827+
ZEND_PARSE_PARAMETERS_START(1, -1)
1828+
Z_PARAM_VARIADIC('*', argv, argc)
1829+
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
1830+
1831+
redis_cmd_init_sstr(&cmdstr, ZEND_NUM_ARGS(), kw, strlen(kw));
1832+
1833+
for (uint32_t i = 0; i < argc; i++) {
1834+
arg = zval_get_string(&argv[i]);
1835+
redis_cmd_append_sstr_zstr(&cmdstr, arg);
1836+
zend_string_release(arg);
1837+
}
1838+
1839+
*cmd = cmdstr.c;
1840+
*cmd_len = cmdstr.len;
1841+
1842+
return SUCCESS;
1843+
}
1844+
18171845
/* Generic function that takes a variable number of keys, with an optional
18181846
* timeout value. This can handle various SUNION/SUNIONSTORE/BRPOP type
18191847
* commands. */

‎redis_commands.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,10 @@ int redis_expire_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
337337
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx);
338338

339339
int redis_varkey_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
340-
char *kw, char **cmd, int *cmd_len, short *slot,
341-
void **ctx);
340+
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx);
341+
342+
int redis_vararg_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
343+
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx);
342344

343345
int redis_sentinel_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
344346
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx);

0 commit comments

Comments
 (0)