8000 Replace redis_cmd_format_static with redis_spprintf · chenyongze/phpredis@b3d00dd · GitHub
[go: up one dir, main page]

Skip to content

Commit b3d00dd

Browse files
Replace redis_cmd_format_static with redis_spprintf
1 parent 0eaeae0 commit b3d00dd

File tree

4 files changed

+138
-147
lines changed

4 files changed

+138
-147
lines changed

library.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#ifndef REDIS_LIBRARY_H
22
#define REDIS_LIBRARY_H
33

4+
/* Non cluster command helper */
45
#define REDIS_SPPRINTF(ret, kw, fmt, ...) \
5-
redis_spprintf(redis_sock, slot TSRMLS_CC, ret, kw, fmt, ##__VA_ARGS__)
6+
redis_spprintf(redis_sock, NULL TSRMLS_CC, ret, kw, fmt, ##__VA_ARGS__)
67

78
#define REDIS_CMD_APPEND_SSTR_STATIC(sstr, str) \
89
redis_cmd_append_sstr(sstr, str, sizeof(str)-1);

redis.c

Lines changed: 51 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -373,23 +373,25 @@ ZEND_GET_MODULE(redis)
373373
PHP_REDIS_API zend_class_entry *redis_get_exception_base(int root TSRMLS_DC)
374374
{
375375
#if HAVE_SPL
376-
if (!root) {
377-
if (!spl_ce_RuntimeException) {
378-
zend_class_entry *pce;
379-
380-
if ((pce = zend_hash_str_find_ptr(CG(class_table), "runtimeexception", sizeof("RuntimeException") - 1))) {
381-
spl_ce_RuntimeException = pce;
382-
return pce;
383-
}
384-
} else {
385-
return spl_ce_RuntimeException;
386-
}
376+
if (!root) {
377+
if (!spl_ce_RuntimeException) {
378+
zend_class_entry *pce;
379+
380+
if ((pce = zend_hash_str_find_ptr(CG(class_table), "runtimeexception",
381+
sizeof("RuntimeException") - 1)))
382+
{
383+
spl_ce_RuntimeException = pce;
384+
return pce;
385+
}
386+
} else {
387+
return spl_ce_RuntimeException;
387388
}
389+
}
388390
#endif
389391
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 2)
390-
return zend_exception_get_default();
392+
return zend_exception_get_default();
391393
#else
392-
return zend_exception_get_default(TSRMLS_C);
394+
return zend_exception_get_default(TSRMLS_C);
393395
#endif
394396
}
395397

@@ -400,7 +402,7 @@ static int send_discard_static(RedisSock *redis_sock TSRMLS_DC) {
400402
int resp_len, cmd_len;
401403

402404
/* format our discard command */
403-
cmd_len = redis_cmd_format_static(&cmd, "DISCARD", "");
405+
cmd_len = REDIS_SPPRINTF(&cmd, "DISCARD", "");
404406

405407
/* send our DISCARD command */
406408
if (redis_sock_write(redis_sock, cmd, cmd_len TSRMLS_CC) >= 0 &&
@@ -1698,9 +1700,9 @@ PHP_METHOD(Redis, info) {
16981700

16991701
/* Build a standalone INFO command or one with an option */
17001702
if (opt != NULL) {
1701-
cmd_len = redis_cmd_format_static(&cmd, "INFO", "s", opt, opt_len);
1703+
cmd_len = REDIS_SPPRINTF(&cmd, "INFO", "s", opt, opt_len);
17021704
} else {
1703-
cmd_len = redis_cmd_format_static(&cmd, "INFO", "");
1705+
cmd_len = REDIS_SPPRINTF(&cmd, "INFO", "");
17041706
}
17051707

17061708
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
@@ -1733,8 +1735,7 @@ PHP_METHOD(Redis, select) {
17331735
}
17341736

17351737
redis_sock->dbNumber = dbNumber;
1736-
1737-
cmd_len = redis_cmd_format_static(&cmd, "SELECT", "d", dbNumber);
1738+
cmd_len = REDIS_SPPRINTF(&cmd, "SELECT", "d", dbNumber);
17381739

17391740
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
17401741
IF_ATOMIC() {
@@ -2122,26 +2123,22 @@ PHP_METHOD(Redis, multi)
21222123

21232124
if (multi_value == PIPELINE) {
21242125
IF_PIPELINE() {
2125-
php_error_docref(NULL TSRMLS_CC, E_WARNING,
2126-
"Already in pipeline mode");
2126+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Already in pipeline mode");
21272127
} else IF_MULTI() {
2128-
php_error_docref(NULL TSRMLS_CC, E_ERROR,
2129-
"Can't activate pipeline in multi mode!");
2128+
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Can't activate pipeline in multi mode!");
21302129
RETURN_FALSE;
21312130
} else {
21322131
free_reply_callbacks(redis_sock);
21332132
redis_sock->mode = PIPELINE;
21342133
}
21352134
} else if (multi_value == MULTI) {
21362135
IF_MULTI() {
2137-
php_error_docref(NULL TSRMLS_CC, E_WARNING,
2138-
"Already in multi mode");
2136+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Already in multi mode");
21392137
} else IF_PIPELINE() {
2140-
php_error_docref(NULL TSRMLS_CC, E_ERROR,
2141-
"Can't activate multi in pipeline mode!");
2138+
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Can't activate multi in pipeline mode!");
21422139
RETURN_FALSE;
21432140
} else {
2144-
cmd_len = redis_cmd_format_static(&cmd, "MULTI", "");
2141+
cmd_len = REDIS_SPPRINTF(&cmd, "MULTI", "");
21452142
SOCKET_WRITE_COMMAND(redis_sock, cmd, cmd_len)
21462143
efree(cmd);
21472144

@@ -2225,7 +2222,7 @@ PHP_METHOD(Redis, exec)
22252222
}
22262223

22272224
IF_MULTI() {
2228-
cmd_len = redis_cmd_format_static(&cmd, "EXEC", "");
2225+
cmd_len = REDIS_SPPRINTF(&cmd, "EXEC", "");
22292226
SOCKET_WRITE_COMMAND(redis_sock, cmd, cmd_len)
22302227
efree(cmd);
22312228

@@ -2459,12 +2456,10 @@ PHP_METHOD(Redis, slaveof)
24592456
RETURN_FALSE;
24602457
}
24612458

2462-
if(host && host_len) {
2463-
cmd_len = redis_cmd_format_static(&cmd, "SLAVEOF", "sd", host,
2464-
host_len, (int)port);
2459+
if (host && host_len) {
2460+
cmd_len = REDIS_SPPRINTF(&cmd, "SLAVEOF", "sd", host, host_len, (int)port);
24652461
} else {
2466-
cmd_len = redis_cmd_format_static(&cmd, "SLAVEOF", "ss", "NO",
2467-
2, "ONE", 3);
2462+
cmd_len = REDIS_SPPRINTF(&cmd, "SLAVEOF", "ss", "NO", 2, "ONE", 3);
24682463
}
24692464

24702465
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
@@ -2568,8 +2563,7 @@ PHP_METHOD(Redis, config)
25682563
}
25692564

25702565
if (mode == CFG_GET && val == NULL) {
2571-
cmd_len = redis_cmd_format_static(&cmd, "CONFIG", "ss", op, op_len,
2572-
key, key_len);
2566+
cmd_len = REDIS_SPPRINTF(&cmd, "CONFIG", "ss", op, op_len, key, key_len);
25732567

25742568
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len)
25752569
IF_ATOMIC() {
@@ -2578,8 +2572,7 @@ PHP_METHOD(Redis, config)
25782572
REDIS_PROCESS_RESPONSE(redis_mbulk_reply_zipped_raw);
25792573

25802574
} else if(mode == CFG_SET && val != NULL) {
2581-
cmd_len = redis_cmd_format_static(&cmd, 10000 "CONFIG", "sss", op,
2582-
op_len, key, key_len, val, val_len);
2575+
cmd_len = REDIS_SPPRINTF(&cmd, "CONFIG", "sss", op, op_len, key, key_len, val, val_len);
25832576

25842577
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len)
25852578
IF_ATOMIC() {
@@ -2631,12 +2624,10 @@ PHP_METHOD(Redis, slowlog) {
26312624

26322625
// Create our command. For everything except SLOWLOG GET (with an arg) it's
26332626
// just two parts
2634-
if(mode == SLOWLOG_GET && ZEND_NUM_ARGS() == 2) {
2635-
cmd_len = redis_cmd_format_static(&cmd, "SLOWLOG", "sl", arg,
2636-
arg_len, option);
2627+
if (mode == SLOWLOG_GET && ZEND_NUM_ARGS() == 2) {
2628+
cmd_len = REDIS_SPPRINTF(&cmd, "SLOWLOG", "sl", arg, arg_len, option);
26372629
} else {
2638-
cmd_len = redis_cmd_format_static(&cmd, "SLOWLOG", "s", arg,
2639-
arg_len);
2630+
cmd_len = REDIS_SPPRINTF(&cmd, "SLOWLOG", "s", arg, arg_len);
26402631
}
26412632

26422633
/* Kick off our command */
@@ -2678,8 +2669,7 @@ PHP_METHOD(Redis, wait) {
26782669
}
26792670

26802671
// Construct the command
2681-
cmd_len = redis_cmd_format_static(&cmd, "WAIT", "ll", num_slaves,
2682-
timeout);
2672+
cmd_len = REDIS_SPPRINTF(&cmd, "WAIT", "ll", num_slaves, timeout);
26832673

26842674
/* Kick it off */
26852675
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
@@ -2697,28 +2687,22 @@ redis_build_pubsub_cmd(RedisSock *redis_sock, char **ret, PUBSUB_TYPE type,
26972687
{
26982688
HashTable *ht_chan;
26992689
zval *z_ele;
2700-
int cmd_len;
27012690
smart_string cmd = {0};
27022691

2703-
if(type == PUBSUB_CHANNELS) {
2704-
if(arg) {
2705-
// With a pattern
2706-
cmd_len = redis_spprintf(redis_sock, NULL TSRMLS_CC, ret, "PUBSUB",
2707-
"sk", "CHANNELS", sizeof("CHANNELS") - 1,
2708-
Z_STRVAL_P(arg), Z_STRLEN_P(arg));
2709-
2710-
/* Return command length */
2711-
return cmd_len;
2692+
if (type == PUBSUB_CHANNELS) {
2693+
if (arg) {
2694+
/* With a pattern */
2695+
return REDIS_SPPRINTF(ret, "PUBSUB", "sk", "CHANNELS", sizeof("CHANNELS") - 1,
2696+
Z_STRVAL_P(arg), Z_STRLEN_P(arg));
27122697
} else {
2713-
// No pattern
2714-
return redis_cmd_format_static(ret, "PUBSUB", "s", "CHANNELS", sizeof("CHANNELS")-1);
2698+
/* No pattern */
2699+
return REDIS_SPPRINTF(ret, "PUBSUB", "s", "CHANNELS", sizeof("CHANNELS") - 1);
27152700
}
2716-
} else if(type == PUBSUB_NUMSUB) {
2701+
} else if (type == PUBSUB_NUMSUB) {
27172702
ht_chan = Z_ARRVAL_P(arg);
27182703

27192704
// Add PUBSUB and NUMSUB bits
2720-
redis_cmd_init_sstr(&cmd, zend_hash_num_elements(ht_chan)+1, "PUBSUB",
2721-
sizeof("PUBSUB")-1);
2705+
redis_cmd_init_sstr(&cmd, zend_hash_num_elements(ht_chan)+1, "PUBSUB", sizeof("PUBSUB")-1);
27222706
redis_cmd_append_sstr(&cmd, "NUMSUB", sizeof("NUMSUB")-1);
27232707

27242708
/* Iterate our elements */
@@ -2731,9 +2715,8 @@ redis_build_pubsub_cmd(RedisSock *redis_sock, char **ret, PUBSUB_TYPE type,
27312715
/* Set return */
27322716
*ret = cmd.c;
27332717
return cmd.len;
2734-
} else if(type == PUBSUB_NUMPAT) {
2735-
return redis_cmd_format_static(ret, "PUBSUB", "s", "NUMPAT",
2736-
sizeof("NUMPAT")-1);
2718+
} else if (type == PUBSUB_NUMPAT) {
2719+
return REDIS_SPPRINTF(ret, "PUBSUB", "s", "NUMPAT", sizeof("NUMPAT") - 1);
27372720
}
27382721

27392722
/* Shouldn't ever happen */
@@ -2882,9 +2865,7 @@ PHP_METHOD(Redis, script) {
28822865
!strcasecmp(Z_STRVAL(z_args[0]), "kill"))
28832866
{
28842867
// Simple SCRIPT FLUSH, or SCRIPT_KILL command
2885-
cmd_len = redis_cmd_format_static(&cmd, "SCRIPT", "s",
2886-
Z_STRVAL(z_args[0]),
2887-
Z_STRLEN(z_args[0]));
2868+
cmd_len = REDIS_SPPRINTF(&cmd, "SCRIPT", "s", Z_STRVAL(z_args[0]), Z_STRLEN(z_args[0]));
28882869
} else if(!strcasecmp(Z_STRVAL(z_args[0]), "load")) {
28892870
// Make sure we have a second argument, and it's not empty. If it is
28902871
// empty, we can just return an empty array (which is what Redis does)
@@ -2897,9 +2878,8 @@ PHP_METHOD(Redis, script) {
28972878
}
28982879

28992880
// Format our SCRIPT LOAD command
2900-
cmd_len = redis_cmd_format_static(&cmd, "SCRIPT", "ss",
2901-
"LOAD", 4, Z_STRVAL(z_args[1]),
2902-
Z_STRLEN(z_args[1]));
2881+
cmd_len = REDIS_SPPRINTF(&cmd, "SCRIPT", "ss", "LOAD", 4, Z_STRVAL(z_args[1]),
2882+
Z_STRLEN(z_args[1]));
29032883
} else if(!strcasecmp(Z_STRVAL(z_args[0]), "exists")) {
29042884
/* Construct our SCRIPT EXISTS command */
29052885
cmd_len = redis_build_script_exists_cmd(&cmd, &(z_args[1]), argc-1);
@@ -3194,12 +3174,10 @@ PHP_METHOD(Redis, client) {
31943174
}
31953175

31963176
/* Build our CLIENT command */
3197-
if(ZEND_NUM_ARGS() == 2) {
3198-
cmd_len = redis_cmd_format_static(&cmd, "CLIENT", "ss",
3199-
opt, opt_len, arg, arg_len);
3177+
if (ZEND_NUM_ARGS() == 2) {
3178+
cmd_len = REDIS_SPPRINTF(&cmd, "CLIENT", "ss", opt, opt_len, arg, arg_len);
32003179
} else {
3201-
cmd_len = redis_cmd_format_static(&cmd, "CLIENT", "s",
3202-
opt, opt_len);
3180+
cmd_len = REDIS_SPPRINTF(&cmd, "CLIENT", "s", opt, opt_len);
32033181
}
32043182

32053183
/* Execute our queue command */

0 commit comments

Comments
 (0)
0