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

Skip to content

Commit 239678a

Browse files
Implement CONFIG RESETSTAT
Rework the CONFIG command and add RESETSTAT variant. Fixes #1673
1 parent 8746493 commit 239678a

8 files changed

Lines changed: 57 additions & 42 deletions

redis.c

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2663,54 +2663,43 @@ PHP_METHOD(Redis, setOption)
26632663
/* {{{ proto boolean Redis::config(string op, string key [, mixed value]) */
26642664
PHP_METHOD(Redis, config)
26652665
{
2666-
zval *object;
2666+
zend_string *op, *key = NULL, *val = NULL;
2667+
FailableResultCallback cb;
26672668
RedisSock *redis_sock;
2668-
char *key = NULL, *val = NULL, *cmd, *op = NULL;
2669-
size_t key_len, val_len, op_len;
2670-
enum {CFG_GET, CFG_SET} mode;
2669+
zval *object;
26712670
int cmd_len;
2671+
char *cmd;
26722672

2673-
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
2674-
"Oss|s", &object, redis_ce, &op, &op_len,
2675-
&key, &key_len, &val, &val_len) == FAILURE)
2673+
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OS|SS", &object,
2674+
redis_ce, &op, &key, &val) == FAILURE)
26762675
{
26772676
RETURN_FALSE;
26782677
}
26792678

2680-
/* op must be GET or SET */
2681-
if(strncasecmp(op, "GET", 3) == 0) {
2682-
mode = CFG_GET;
2683-
} else if(strncasecmp(op, "SET", 3) == 0) {
2684-
mode = CFG_SET;
2685-
} else {
2686-
RETURN_FALSE;
2687-
}
2688-
26892679
if ((redis_sock = redis_sock_get(object, 0)) == NULL) {
26902680
RETURN_FALSE;
26912681
}
26922682

2693-
if (mode == CFG_GET && val == NULL) {
2694-
cmd_len = REDIS_SPPRINTF(&cmd, "CONFIG", "ss", op, op_len, key, key_len);
2695-
2696-
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len)
2697-
if (IS_ATOMIC(redis_sock)) {
2698-
redis_mbulk_reply_zipped_raw(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
2699-
}
2700-
REDIS_PROCESS_RESPONSE(redis_mbulk_reply_zipped_raw);
2701-
2702-
} else if(mode == CFG_SET && val != NULL) {
2703-
cmd_len = REDIS_SPPRINTF(&cmd, "CONFIG", "sss", op, op_len, key, key_len, val, val_len);
2704-
2705-
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len)
2706-
if (IS_ATOMIC(redis_sock)) {
2707-
redis_boolean_response(
2708-
INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
2709-
}
2710-
REDIS_PROCESS_RESPONSE(redis_boolean_response);
2683+
if (zend_string_equals_literal_ci(op, "GET") && key != NULL) {
2684+
cmd_len = REDIS_SPPRINTF(&cmd, "CONFIG", "SS", op, key);
2685+
cb = redis_mbulk_reply_zipped_raw;
2686+
} else if (zend_string_equals_literal_ci(op, "RESETSTAT")) {
2687+
cmd_len = REDIS_SPPRINTF(&cmd, "CONFIG", "s", ZEND_STRL("RESETSTAT"));
2688+
cb = redis_boolean_response;
2689+
} else if (zend_string_equals_literal_ci(op, "SET") && key != NULL && val != NULL) {
2690+
cmd_len = REDIS_SPPRINTF(&cmd, "CONFIG", "SSS", op, key, val);
2691+
cb = redis_boolean_response;
27112692
} else {
27122693
RETURN_FALSE;
27132694
}
2695+
2696+
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len)
2697+
if (IS_ATOMIC(redis_sock)) {
2698+
cb(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
2699+
}
2700+
REDIS_PROCESS_RESPONSE(redis_boolean_response);
2701+
2702+
return;
27142703
}
27152704
/* }}} */
27162705

redis.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function close(): bool;
7070

7171
public function command(string $opt = null, string|array $arg): mixed;
7272

73-
public function config(string $operation, string $key, mixed $value = null): mixed;
73+
public function config(string $operation, ?string $key = NULL, mixed $value = null): mixed;
7474

7575
public function connect(string $host, int $port = 6379, float $timeout = 0, string $persistent_id = null, int $retry_interval = 0, float $read_timeout = 0, array $context = null): bool;
7676

redis_arginfo.h

Lines changed: 3 additions & 3 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: 2c5f558917526d1a034a45b63bf7890bd8cb49e5 */
2+
* Stub hash: 122b5ca534302a09a4f072106d097f2831ba6f22 */
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")
@@ -106,9 +106,9 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Redis_command, 0, 2, IS_MI
106106
ZEND_ARG_TYPE_MASK(0, arg, MAY_BE_STRING|MAY_BE_ARRAY, NULL)
107107
ZEND_END_ARG_INFO()
108108

109-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Redis_config, 0, 2, IS_MIXED, 0)
109+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Redis_config, 0, 1, IS_MIXED, 0)
110110
ZEND_ARG_TYPE_INFO(0, operation, IS_STRING, 0)
111-
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
111+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, key, IS_STRING, 1, "NULL")
112112
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, value, IS_MIXED, 0, "null")
113113
ZEND_END_ARG_INFO()
114114

redis_cluster_arginfo.h

Lines changed: 1 addition & 1 deletion
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: 7ff59229ef9ab94d3bb918d666610b70a5676030 */
2+
* Stub hash: 97fe79bf371074b77d22e1e820903fb2103d10c9 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster___construct, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 1)

redis_cluster_legacy_arginfo.h

Lines changed: 1 addition & 1 deletion
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: 7ff59229ef9ab94d3bb918d666610b70a5676030 */
2+
* Stub hash: 97fe79bf371074b77d22e1e820903fb2103d10c9 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster___construct, 0, 0, 1)
55
ZEND_ARG_INFO(0, name)

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: 2c5f558917526d1a034a45b63bf7890bd8cb49e5 */
2+
* Stub hash: 122b5ca534302a09a4f072106d097f2831ba6f22 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
55
ZEND_ARG_INFO(0, options)
@@ -97,7 +97,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_command, 0, 0, 2)
9797
ZEND_ARG_INFO(0, arg)
9898
ZEND_END_ARG_INFO()
9999

100-
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_config, 0, 0, 2)
100+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_config, 0, 0, 1)
101101
ZEND_ARG_INFO(0, operation)
102102
ZEND_ARG_INFO(0, key)
103103
ZEND_ARG_INFO(0, value)

tests/RedisClusterTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function testCopy() { return $this->marktestSkipped(); }
6464
public function testGeoSearch() { return $this->marktestSkipped(); }
6565
public function testGeoSearchStore() { return $this->marktestSkipped(); }
6666
public function testHRandField() { return $this->marktestSkipped(); }
67+
public function testConfig() { return $this->markTestSkipped(); }
6768

6869
/* Session locking feature is currently not supported in in context of Redis Cluster.
6970
The biggest issue for this is the distribution nature of Redis cluster */

tests/RedisTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5342,6 +5342,31 @@ public function testNestedNullArray() {
53425342
$this->redis->setOption(Redis::OPT_NULL_MULTIBULK_AS_NULL, false);
53435343
}
53445344

5345+
public function testConfig() {
5346+
/* GET */
5347+
$cfg = $this->redis->config('GET', 'timeout');
5348+
$this->assertTrue(is_array($cfg) && isset($cfg['timeout']));
5349+
$sec = $cfg['timeout'];
5350+
5351+
/* SET */
5352+
foreach ([$sec + 30, $sec] as $val) {
5353+
$this->assertTrue($this->redis->config('SET', 'timeout', $val));
5354+
$cfg = $this->redis->config('GET', 'timeout');
5355+
$this->assertTrue(isset($cfg['timeout']) && $cfg['timeout'] == $val);
5356+
}
5357+
5358+
/* RESETSTAT */
5359+
$c1 = count($this->redis->info('commandstats'));
5360+
$this->assertTrue($this->redis->config('resetstat'));
5361+
$this->assertTrue(count($this->redis->info('commandstats')) < $c1);
5362+
5363+
/* Ensure invalid calls are handled by PhpRedis */
5364+
foreach (['notacommand', 'get', 'set'] as $cmd) {
5365+
$this->assertFalse($this->redis->config($cmd));
5366+
}
5367+
$this->assertFalse($this->redis->config('set', 'foo'));
5368+
}
5369+
53455370
public function testReconnectSelect() {
53465371
$key = 'reconnect-select';
53475372
$value = 'Has been set!';

0 commit comments

Comments
 (0)