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

Skip to content

Commit 44d03ca

Browse files
INFO with multiple sections
See #2068
1 parent 8d80ca5 commit 44d03ca

10 files changed

Lines changed: 82 additions & 52 deletions

library.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3414,6 +3414,7 @@ redis_unpack(RedisSock *redis_sock, const char *src, int srclen, zval *zdst) {
34143414
}
34153415

34163416
PHP_REDIS_API int
3417+
34173418
redis_serialize(RedisSock *redis_sock, zval *z, char **val, size_t *val_len)
34183419
{
34193420
php_serialize_data_t ht;

redis.c

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,38 +1752,32 @@ PHP_METHOD(Redis, pttl) {
17521752

17531753
/* {{{ proto array Redis::info() */
17541754
PHP_METHOD(Redis, info) {
1755-
1756-
zval *object;
1755+
smart_string cmdstr = {0};
17571756
RedisSock *redis_sock;
1758-
char *cmd, *opt = NULL;
1759-
size_t opt_len;
1760-
int cmd_len;
1757+
zend_string *section;
1758+
zval *args = NULL;
1759+
int i, argc = 0;
17611760

1762-
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
1763-
"O|s", &object, redis_ce, &opt, &opt_len)
1764-
== FAILURE)
1765-
{
1766-
RETURN_FALSE;
1767-
}
1761+
ZEND_PARSE_PARAMETERS_START(0, -1)
1762+
Z_PARAM_VARIADIC('+', args, argc)
1763+
ZEND_PARSE_PARAMETERS_END();
17681764

1769-
if ((redis_sock = redis_sock_get(object, 0)) == NULL) {
1765+
if ((redis_sock = redis_sock_get(getThis(), 0)) == NULL)
17701766
RETURN_FALSE;
1771-
}
17721767

1773-
/* Build a standalone INFO command or one with an option */
1774-
if (opt != NULL) {
1775-
cmd_len = REDIS_SPPRINTF(&cmd, "INFO", "s", opt, opt_len);
1776-
} else {
1777-
cmd_len = REDIS_SPPRINTF(&cmd, "INFO", "");
1768+
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc, "INFO");
1769+
for (i = 0; i < argc; i++) {
1770+
section = zval_get_string(&args[i]);
1771+
redis_cmd_append_sstr_zstr(&cmdstr, section);
1772+
zend_string_release(section);
17781773
}
17791774

1780-
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
1775+
REDIS_PROCESS_REQUEST(redis_sock, cmdstr.c, cmdstr.len);
17811776
if (IS_ATOMIC(redis_sock)) {
17821777
redis_info_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL,
17831778
NULL);
17841779
}
17851780
REDIS_PROCESS_RESPONSE(redis_info_response);
1786-
17871781
}
17881782
/* }}} */
17891783

redis.stub.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,21 @@ public function incrBy(string $key, int $value);
269269
/** @return Redis|int|false */
270270
public function incrByFloat(string $key, float $value);
271271

272-
public function info(string $opt = null): Redis|array|false;
272+
/**
273+
Retreive information about the connected redis-server. If no arguments are passed to
274+
this function, redis will return every info field. Alternatively you may pass a specific
275+
section you want returned (e.g. 'server', or 'memory') to receive only information pertaining
276+
to that section.
277+
278+
If connected to Redis server >= 7.0.0 you may pass multiple optional sections.
279+
280+
@see https://redis.io/commands/info/
281+
282+
@param string ...$sections Optional section(s) you wish Redis server to return.
283+
284+
@return Redis|array|false
285+
*/
286+
public function info(string ...$sections): Redis|array|false;
273287

274288
public function isConnected(): bool;
275289

redis_arginfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: a8ddcde8e8af5201d72bfe8b463afc0c9dafb73d */
2+
* Stub hash: bd81918bd558ec53399e7f64647c39f288f3413e */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
55
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "null")
@@ -444,7 +444,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_incrByFloat, 0, 0, 2)
444444
ZEND_END_ARG_INFO()
445445

446446
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_Redis_info, 0, 0, Redis, MAY_BE_ARRAY|MAY_BE_FALSE)
447-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, opt, IS_STRING, 0, "null")
447+
ZEND_ARG_VARIADIC_TYPE_INFO(0, sections, IS_STRING, 0)
448448
ZEND_END_ARG_INFO()
449449

450450
#define arginfo_class_Redis_isConnected arginfo_class_Redis_close

redis_cluster.c

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2578,39 +2578,38 @@ PHP_METHOD(RedisCluster, lastsave) {
25782578
* proto array RedisCluster::info(array host_port, [string $arg]) */
25792579
PHP_METHOD(RedisCluster, info) {
25802580
redisCluster *c = GET_CONTEXT();
2581+
zval *node = NULL, *args = NULL;
2582+
smart_string cmdstr = {0};
25812583
REDIS_REPLY_TYPE rtype;
2582-
char *cmd, *opt = NULL;
2583-
int cmd_len;
2584-
size_t opt_len = 0;
2584+
zend_string *section;
25852585
void *ctx = NULL;
2586-
2587-
zval *z_arg;
2586+
int i, argc;
25882587
short slot;
25892588

2590-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|s", &z_arg, &opt,
2591-
&opt_len) == FAILURE)
2592-
{
2589+
ZEND_PARSE_PARAMETERS_START(1, -1)
2590+
Z_PARAM_ZVAL(node)
2591+
Z_PARAM_OPTIONAL
2592+
Z_PARAM_VARIADIC('*', args, argc)
2593+
ZEND_PARSE_PARAMETERS_END();
2594+
2595+
if ((slot = cluster_cmd_get_slot(c, node)) < 0)
25932596
RETURN_FALSE;
2594-
}
25952597

2596-
/* Treat INFO as non read-only, as we probably want the master */
2597-
c->readonly = 0;
2598+
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc, "INFO");
25982599

2599-
slot = cluster_cmd_get_slot(c, z_arg);
2600-
if (slot < 0) {
2601-
RETURN_FALSE;
2602-
}
2600+
/* Direct this command at the master */
2601+
c->readonly = 0;
26032602

2604-
if (opt != NULL) {
2605-
cmd_len = redis_spprintf(NULL, NULL, &cmd, "INFO", "s", opt, opt_len);
2606-
} else {
2607-
cmd_len = redis_spprintf(NULL, NULL, &cmd, "INFO", "");
2603+
for (i = 0; i < argc; i++) {
2604+
section = zval_get_string(&args[i]);
2605+
redis_cmd_append_sstr_zstr(&cmdstr, section);
2606+
zend_string_release(section);
26082607
}
26092608

26102609
rtype = CLUSTER_IS_ATOMIC(c) ? TYPE_BULK : TYPE_LINE;
2611-
if (cluster_send_slot(c, slot, cmd, cmd_len, rtype) < 0) {
2610+
if (cluster_send_slot(c, slot, cmdstr.c, cmdstr.len, rtype) < 0) {
26122611
CLUSTER_THROW_EXCEPTION("Unable to send INFO command to specific node", 0);
2613-
efree(cmd);
2612+
efree(cmdstr.c);
26142613
RETURN_FALSE;
26152614
}
26162615

@@ -2620,7 +2619,7 @@ PHP_METHOD(RedisCluster, info) {
26202619
CLUSTER_ENQUEUE_RESPONSE(c, slot, cluster_info_resp, ctx);
26212620
}
26222621

2623-
efree(cmd);
2622+
efree(cmdstr.c);
26242623
}
26252624
/* }}} */
26262625

redis_cluster.stub.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,23 @@ public function incrby(string $key, int $value): RedisCluster|int|false;
187187

188188
public function incrbyfloat(string $key, float $value): RedisCluster|float|false;
189189

190-
public function info(string|array $key_or_address, ?string $section = null): RedisCluster|array|false;
190+
/**
191+
Retreive information about the connected redis-server. If no arguments are passed to
192+
this function, redis will return every info field. Alternatively you may pass a specific
193+
section you want returned (e.g. 'server', or 'memory') to receive only information pertaining
194+
to that section.
195+
196+
If connected to Redis server >= 7.0.0 you may pass multiple optional sections.
197+
198+
@see https://redis.io/commands/info/
199+
200+
@param string|array $key_or_address Either a key name or array with host and port indicating
201+
which cluster node we want to send the command to.
202+
@param string ...$sections Optional section(s) you wish Redis server to return.
203+
204+
@return Redis|array|false
205+
*/
206+
public function info(string|array $key_or_address, string ...$sections): RedisCluster|array|false;
191207

192208
public function keys(string $pattern): RedisCluster|array|false;
193209

redis_cluster_arginfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: ccb418a312ff22f03e2354de508ff8bd9e4d266e */
2+
* Stub hash: d3d58cb90bf0884a4153cd01f3d0850b42fcd910 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster___construct, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 1)
@@ -385,7 +385,7 @@ ZEND_END_ARG_INFO()
385385

386386
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_RedisCluster_info, 0, 1, RedisCluster, MAY_BE_ARRAY|MAY_BE_FALSE)
387387
ZEND_ARG_TYPE_MASK(0, key_or_address, MAY_BE_STRING|MAY_BE_ARRAY, NULL)
388-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, section, IS_STRING, 1, "null")
388+
ZEND_ARG_VARIADIC_TYPE_INFO(0, sections, IS_STRING, 0)
389389
ZEND_END_ARG_INFO()
390390

391391
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_RedisCluster_keys, 0, 1, RedisCluster, MAY_BE_ARRAY|MAY_BE_FALSE)

redis_cluster_legacy_arginfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: ccb418a312ff22f03e2354de508ff8bd9e4d266e */
2+
* Stub hash: d3d58cb90bf0884a4153cd01f3d0850b42fcd910 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster___construct, 0, 0, 1)
55
ZEND_ARG_INFO(0, name)
@@ -327,7 +327,7 @@ ZEND_END_ARG_INFO()
327327

328328
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster_info, 0, 0, 1)
329329
ZEND_ARG_INFO(0, key_or_address)
330-
ZEND_ARG_INFO(0, section)
330+
ZEND_ARG_VARIADIC_INFO(0, sections)
331331
ZEND_END_ARG_INFO()
332332

333333
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster_keys, 0, 0, 1)

redis_legacy_arginfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: a8ddcde8e8af5201d72bfe8b463afc0c9dafb73d */
2+
* Stub hash: bd81918bd558ec53399e7f64647c39f288f3413e */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
55
ZEND_ARG_INFO(0, options)
@@ -375,7 +375,7 @@ ZEND_END_ARG_INFO()
375375
#define arginfo_class_Redis_incrByFloat arginfo_class_Redis_append
376376

377377
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_info, 0, 0, 0)
378-
ZEND_ARG_INFO(0, opt)
378+
ZEND_ARG_VARIADIC_INFO(0, sections)
379379
ZEND_END_ARG_INFO()
380380

381381
#define arginfo_class_Redis_isConnected arginfo_class_Redis___destruct

tests/RedisTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,6 +2363,12 @@ public function testInfo() {
23632363
$this->assertTrue(in_array($k, array_keys($info)));
23642364
}
23652365
}
2366+
2367+
if (!$this->minVersionCheck("7.0.0"))
2368+
return;
2369+
2370+
$res = $this->redis->info('server', 'memory');
2371+
$this->assertTrue(is_array($res) && isset($res['redis_version']) && isset($res['used_memory']));
23662372
}
23672373

23682374
public function testInfoCommandStats() {

0 commit comments

Comments
 (0)