8000 refactoring (#1140) · chenyongze/phpredis@18149e3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 18149e3

Browse files
authored
refactoring (phpredis#1140)
* refactoring * Fix read_timeout
1 parent bf499ee commit 18149e3

File tree

3 files changed

+35
-43
lines changed

3 files changed

+35
-43
lines changed

library.c

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,7 +1597,7 @@ PHP_REDIS_API int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)
15971597

15981598
php_stream_auto_cleanup(redis_sock->stream);
15991599

1600-
if(tv.tv_sec != 0 || tv.tv_usec != 0) {
1600+
if (read_tv.tv_sec != 0 || read_tv.tv_usec != 0) {
16011601
php_stream_set_option(redis_sock->stream,PHP_STREAM_OPTION_READ_TIMEOUT,
16021602
0, &read_tv);
16031603
}
@@ -1791,36 +1791,33 @@ redis_mbulk_reply_loop(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
17911791
zval *z_tab, int count, int unserialize)
17921792
{
17931793
char *line;
1794-
int len;
1795-
1796-
while(count > 0) {
1797-
line = redis_sock_read(redis_sock, &len TSRMLS_CC);
1798-
if (line != NULL) {
1799-
zval zv, *z = &zv;
1800-
int unwrap;
1801-
1802-
/* We will attempt unserialization, if we're unserializing everything,
1803-
* or if we're unserializing keys and we're on a key, or we're
1804-
* unserializing values and we're on a value! */
1805-
unwrap = unserialize == UNSERIALIZE_ALL ||
1806-
(unserialize == UNSERIALIZE_KEYS && count % 2 == 0) ||
1807-
(unserialize == UNSERIALIZE_VALS && count % 2 != 0);
1808-
1809-
if (unwrap && redis_unserialize(redis_sock, line, len, z TSRMLS_CC)) {
1794+
int i, len;
1795+
1796+
for (i = 0; i < count; ++i) {
1797+
if ((line = redis_sock_read(redis_sock, &len TSRMLS_CC)) == NULL) {
1798+
add_next_index_bool(z_tab, 0);
1799+
continue;
1800+
}
1801+
1802+
/* We will attempt unserialization, if we're unserializing everything,
1803+
* or if we're unserializing keys and we're on a key, or we're
1804+
* unserializing values and we're on a value! */
1805+
int unwrap = (
1806+
(unserialize == UNSERIALIZE_ALL) ||
1807+
(unserialize == UNSERIALIZE_KEYS && i % 2 == 0) ||
1808+
(unserialize == UNSERIALIZE_VALS && i % 2 != 0)
1809+
);
1810+
zval zv, *z = &zv;
1811+
if (unwrap && redis_unserialize(redis_sock, line, len, z TSRMLS_CC)) {
18101812
#if (PHP_MAJOR_VERSION < 7)
1811-
MAKE_STD_ZVAL(z);
1812-
*z = zv;
1813+
MAKE_STD_ZVAL(z);
1814+
*z = zv;
18131815
#endif
1814-
add_next_index_zval(z_tab, z);
1815-
} else {
1816-
add_next_index_stringl(z_tab, line, len);
1817-
}
1818-
efree(line);
1816+
add_next_index_zval(z_tab, z);
18191817
} else {
1820-
add_next_index_bool(z_tab, 0);
1818+
add_next_index_stringl(z_tab, line, len);
18211819
}
1822-
1823-
count--;
1820+
efree(line);
18241821
}
18251822
}
18261823

redis.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ PHP_METHOD(Redis, pconnect)
788788
if (redis_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1) == FAILURE) {
789789
RETURN_FALSE;
790790
} else {
791+
/* FIXME: should we remove whole `else` block? */
791792
/* reset multi/exec state if there is one. */
792793
RedisSock *redis_sock;
793794
if ((redis_sock = redis_sock_get(getThis() TSRMLS_CC, 0)) == NULL) {

redis_commands.c

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3138,7 +3138,6 @@ void redis_setoption_handler(INTERNAL_FUNCTION_PARAMETERS,
31383138
char *val_str;
31393139
struct timeval read_tv;
31403140
strlen_t val_len;
3141-
int test_val;
31423141

31433142
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &option,
31443143
&val_str, &val_len) == FAILURE)
@@ -3149,18 +3148,15 @@ void redis_setoption_handler(INTERNAL_FUNCTION_PARAMETERS,
31493148
switch(option) {
31503149
case REDIS_OPT_SERIALIZER:
31513150
val_long = atol(val_str);
3152-
test_val = val_long == REDIS_SERIALIZER_NONE || val_long == REDIS_SERIALIZER_PHP;
3151+
if (val_long == REDIS_SERIALIZER_NONE || val_long == REDIS_SERIALIZER_PHP
31533152
#ifdef HAVE_REDIS_IGBINARY
3154-
test_val = test_val || val_long == REDIS_SERIALIZER_IGBINARY;
3153+
|| val_long == REDIS_SERIALIZER_IGBINARY
31553154
#endif
3156-
if(test_val)
3157-
{
3158-
redis_sock->serializer = val_long;
3159-
RETURN_TRUE;
3160-
} else {
3161-
RETURN_FALSE;
3162-
}
3163-
break;
3155+
) {
3156+
redis_sock->serializer = val_long;
3157+
RETURN_TRUE;
3158+
}
3159+
break;
31643160
case REDIS_OPT_PREFIX:
31653161
if(redis_sock->prefix) {
31663162
efree(redis_sock->prefix);
@@ -3190,7 +3186,6 @@ void redis_setoption_handler(INTERNAL_FUNCTION_PARAMETERS,
31903186
redis_sock->scan = val_long;
31913187
RETURN_TRUE;
31923188
}
3193-
RETURN_FALSE;
31943189
break;
31953190
case REDIS_OPT_FAILOVER:
31963191
val_long = atol(val_str);
@@ -3201,12 +3196,11 @@ void redis_setoption_handler(INTERNAL_FUNCTION_PARAMETERS,
32013196
{
32023197
c->failover = val_long;
32033198
RETURN_TRUE;
3204-
} else {
3205-
RETURN_FALSE;
32063199
}
3207-
default:
3208-
RETURN_FALSE;
3200+
break;
3201+
EMPTY_SWITCH_DEFAULT_CASE()
32093202
}
3203+
RETURN_FALSE;
32103204
}
32113205

32123206
void redis_prefix_handler(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock) {

0 commit comments

Comments
 (0)
0