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

Skip to content

Commit d87f142

Browse files
Refactor SLOWLOG command
Refactor the slowlog command to use the new argument parsing API and also change it so we no longer manually handle atomic/non-atomic logic in the handler itself.
1 parent a176f58 commit d87f142

7 files changed

Lines changed: 76 additions & 60 deletions

File tree

redis.c

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2717,57 +2717,7 @@ PHP_METHOD(Redis, config) {
27172717

27182718
/* {{{ proto boolean Redis::slowlog(string arg, [int option]) */
27192719
PHP_METHOD(Redis, slowlog) {
2720-
zval *object;
2721-
RedisSock *redis_sock;
2722-
char *arg, *cmd;
2723-
int cmd_len;
2724-
size_t arg_len;
2725-
zend_long option = 0;
2726-
enum {SLOWLOG_GET, SLOWLOG_LEN, SLOWLOG_RESET} mode;
2727-
2728-
// Make sure we can get parameters
2729-
if(zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
2730-
"Os|l", &object, redis_ce, &arg, &arg_len,
2731-
&option) == FAILURE)
2732-
{
2733-
RETURN_FALSE;
2734-
}
2735-
2736-
/* Figure out what kind of slowlog command we're executing */
2737-
if(!strncasecmp(arg, "GET", 3)) {
2738-
mode = SLOWLOG_GET;
2739-
} else if(!strncasecmp(arg, "LEN", 3)) {
2740-
mode = SLOWLOG_LEN;
2741-
} else if(!strncasecmp(arg, "RESET", 5)) {
2742-
mode = SLOWLOG_RESET;
2743-
} else {
2744-
/* This command is not valid */
2745-
RETURN_FALSE;
2746-
}
2747-
2748-
/* Make sure we can grab our redis socket */
2749-
if ((redis_sock = redis_sock_get(object, 0)) == NULL) {
2750-
RETURN_FALSE;
2751-
}
2752-
2753-
// Create our command. For everything except SLOWLOG GET (with an arg) it's
2754-
// just two parts
2755-
if (mode == SLOWLOG_GET && ZEND_NUM_ARGS() == 2) {
2756-
cmd_len = REDIS_SPPRINTF(&cmd, "SLOWLOG", "sl", arg, arg_len, option);
2757-
} else {
2758-
cmd_len = REDIS_SPPRINTF(&cmd, "SLOWLOG", "s", arg, arg_len);
2759-
}
2760-
2761-
/* Kick off our command */
2762-
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
2763-
if (IS_ATOMIC(redis_sock)) {
2764-
if(redis_read_variant_reply(INTERNAL_FUNCTION_PARAM_PASSTHRU,
2765-
redis_sock, NULL, NULL) < 0)
2766-
{
2767-
RETURN_FALSE;
2768-
}
2769-
}
2770-
REDIS_PROCESS_RESPONSE(redis_read_variant_reply);
2720+
REDIS_PROCESS_CMD(slowlog, redis_read_variant_reply);
27712721
}
27722722

27732723
/* {{{ proto Redis::wait(int num_slaves, int ms) }}} */

redis.stub.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,33 @@ public function sismember(string $key, mixed $value): Redis|bool;
521521

522522
public function slaveof(string $host = null, int $port = 6379): bool;
523523

524-
public function slowlog(string $mode, int $option = 0): mixed;
524+
/**
525+
Interact with Redis' slowlog functionality in variousu ways, depending
526+
on the value of 'operations'.
527+
528+
@param string $operation The operation you wish to perform.  This can
529+
be one of the following values:
530+
'get' - Retreive the Redis slowlog as an array.
531+
'len' - Retreive the length of the slowlog.
532+
'reset' - Remove all slowlog entries.
533+
<code>
534+
<?php
535+
$redis->slowllog('get', -1); // Retreive all slowlog entries.
536+
$redis->slowlog('len'); // Retreive slowlog length.
537+
$redis->slowlog('reset'); // Reset the slowlog.
538+
?>
539+
</code>
540+
541+
@param int $length This optional argument can be passed when operation
542+
is 'get' and will specify how many elements to retreive.
543+
If omitted Redis will send up to a default number of
544+
entries, which is configurable.
545+
546+
Note: With Redis >= 7.0.0 you can send -1 to mean "all".
547+
548+
@return mixed
549+
*/
550+
public function slowlog(string $operation, int $length = 0): mixed;
525551

526552
public function sort(string $key, ?array $options = null): mixed;
527553

redis_arginfo.h

Lines changed: 4 additions & 4 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: a024c59eff58030ac224fc22cc4040b6e926a643 */
2+
* Stub hash: 3ffe58fd2c74dcb474adf0b983f503d798c975d8 */
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")
@@ -392,7 +392,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_Redis_hMget, 0, 2, Red
392392
ZEND_ARG_TYPE_INFO(0, keys, IS_ARRAY, 0)
393393
ZEND_END_ARG_INFO()
394394

395-
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_Redis_hMset, 0, 2, Redis, MAY_BE_BOOL|MAY_BE_FALSE)
395+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_Redis_hMset, 0, 2, Redis, MAY_BE_BOOL)
396396
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
397397
ZEND_ARG_TYPE_INFO(0, keyvals, IS_ARRAY, 0)
398398
ZEND_END_ARG_INFO()
@@ -787,8 +787,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Redis_slaveof, 0, 0, _IS_B
787787
ZEND_END_ARG_INFO()
788788

789789
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Redis_slowlog, 0, 1, IS_MIXED, 0)
790-
ZEND_ARG_TYPE_INFO(0, mode, IS_STRING, 0)
791-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, option, IS_LONG, 0, "0")
790+
ZEND_ARG_TYPE_INFO(0, operation, IS_STRING, 0)
791+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, length, IS_LONG, 0, "0")
792792
ZEND_END_ARG_INFO()
793793

794794
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Redis_sort, 0, 1, IS_MIXED, 0)

redis_commands.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2549,6 +2549,43 @@ int redis_lcs_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
25492549
return SUCCESS;
25502550
}
25512551

2552+
int redis_slowlog_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
2553+
char **cmd, int *cmd_len, short *slot, void **ctx)
2554+
{
2555+
enum {SLOWLOG_GET, SLOWLOG_LEN, SLOWLOG_RESET} mode;
2556+
smart_string cmdstr = {0};
2557+
zend_string *op = NULL;
2558+
zend_long arg = 0;
2559+
2560+
ZEND_PARSE_PARAMETERS_START(1, 2)
2561+
Z_PARAM_STR(op)
2562+
Z_PARAM_OPTIONAL
2563+
Z_PARAM_LONG(arg)
2564+
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
2565+
2566+
if (zend_string_equals_literal_ci(op, "GET")) {
2567+
mode = SLOWLOG_GET;
2568+
} else if (zend_string_equals_literal_ci(op, "LEN")) {
2569+
mode = SLOWLOG_LEN;
2570+
} else if (zend_string_equals_literal_ci(op, "RESET")) {
2571+
mode = SLOWLOG_RESET;
2572+
} else {
2573+
php_error_docref(NULL, E_WARNING, "Unknown SLOWLOG operation '%s'", ZSTR_VAL(op));
2574+
return FAILURE;
2575+
}
2576+
2577+
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1 + (mode == SLOWLOG_GET && ZEND_NUM_ARGS() == 2), "SLOWLOG");
2578+
redis_cmd_append_sstr_zstr(&cmdstr, op);
2579+
2580+
if (mode == SLOWLOG_GET && ZEND_NUM_ARGS() == 2)
2581+
redis_cmd_append_sstr_long(&cmdstr, arg);
2582+
2583+
*cmd = cmdstr.c;
2584+
*cmd_len = cmdstr.len;
2585+
2586+
return SUCCESS;
2587+
}
2588+
25522589
void redis_get_restore_options(redisRestoreOptions *dst, HashTable *ht) {
25532590
zend_string *key;
25542591
zend_long lval;

redis_commands.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ int redis_intercard_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
132132
char *kw, char **cmd, int *cmd_len, short *slot,
133133
void **ctx);
134134

135+
int redis_slowlog_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
136+
char **cmd, int *cmd_len, short *slot, void **ctx);
137+
135138
int redis_lcs_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
136139
char **cmd, int *cmd_len, short *slot, void **ctx);
137140

redis_legacy_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: a024c59eff58030ac224fc22cc4040b6e926a643 */
2+
* Stub hash: 3ffe58fd2c74dcb474adf0b983f503d798c975d8 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
55
ZEND_ARG_INFO(0, options)
@@ -664,8 +664,8 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_slaveof, 0, 0, 0)
664664
ZEND_END_ARG_INFO()
665665

666666
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_slowlog, 0, 0, 1)
667-
ZEND_ARG_INFO(0, mode)
668-
ZEND_ARG_INFO(0, option)
667+
ZEND_ARG_INFO(0, operation)
668+
ZEND_ARG_INFO(0, length)
669669
ZEND_END_ARG_INFO()
670670

671671
#define arginfo_class_Redis_sort arginfo_class_Redis_getEx

tests/RedisTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2291,7 +2291,7 @@ public function testSlowlog() {
22912291
$this->assertTrue(is_array($this->redis->slowlog('get', 10)));
22922292
$this->assertTrue(is_int($this->redis->slowlog('len')));
22932293
$this->assertTrue($this->redis->slowlog('reset'));
2294-
$this->assertFalse($this->redis->slowlog('notvalid'));
2294+
$this->assertFalse(@$this->redis->slowlog('notvalid'));
22952295
}
22962296

22972297
public function testWait() {

0 commit comments

Comments
 (0)