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

Skip to content

Commit 7207aae

Browse files
Add SLOWLOG command
Add support for the various slowlog commands you can execute in Redis, including: SLOWLOG GET [len] SLOWLOG RESET SLOWLOG LIST
1 parent 6cf2cea commit 7207aae

File tree

4 files changed

+95
-3
lines changed

4 files changed

+95
-3
lines changed

README.markdown

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ _**Description**_: Sends a string to Redis, which replies with the same string
322322
1. [save](#save) - Synchronously save the dataset to disk (wait to complete)
323323
1. [slaveof](#slaveof) - Make the server a slave of another instance, or promote it to master
324324
1. [time](#time) - Return the current server time
325+
1. [slowlog](#slowlog) - Access the Redis slowlog entries
325326

326327
### bgrewriteaof
327328
-----
@@ -539,6 +540,36 @@ the unix timestamp, and element one being microseconds.
539540
$redis->time();
540541
~~~
541542

543+
### slowlog
544+
-----
545+
_**Description**_: Access the Redis slowlog
546+
547+
##### *Parameters*
548+
*Operation* (string): This can be either `GET`, `LEN`, or `RESET`
549+
*Length* (integer), optional: If executing a `SLOWLOG GET` command, you can pass an optional length.
550+
#####
551+
552+
##### *Return value*
553+
The return value of SLOWLOG will depend on which operation was performed.
554+
SLOWLOG GET: Array of slowlog entries, as provided by Redis
555+
SLOGLOG LEN: Integer, the length of the slowlog
556+
SLOWLOG RESET: Boolean, depending on success
557+
#####
558+
559+
##### *Examples*
560+
~~~
561+
// Get ten slowlog entries
562+
$redis->slowlog('get', 10);
563+
// Get the default number of slowlog entries
564+
565+
$redis->slowlog('get');
566+
// Reset our slowlog
567+
$redis->slowlog('reset');
568+
569+
// Retrieve slowlog length
570+
$redis->slowlog('len');
571+
~~~
572+
542573
## Keys and Strings
543574

544575
### Strings

php_redis.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ PHP_METHOD(Redis, getOption);
180180
PHP_METHOD(Redis, setOption);
181181

182182
PHP_METHOD(Redis, config);
183+
PHP_METHOD(Redis, slowlog);
183184

184185
PHP_METHOD(Redis, client);
185186

redis.c

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ static zend_function_entry redis_functions[] = {
237237
/* config */
238238
PHP_ME(Redis, config, NULL, ZEND_ACC_PUBLIC)
239239

240+
/* slowlog */
241+
PHP_ME(Redis, slowlog, NULL, ZEND_ACC_PUBLIC)
242+
240243
/* introspection */
241244
PHP_ME(Redis, getHost, NULL, ZEND_ACC_PUBLIC)
242245
PHP_ME(Redis, getPort, NULL, ZEND_ACC_PUBLIC)
@@ -5885,6 +5888,56 @@ PHP_METHOD(Redis, config)
58855888
/* }}} */
58865889

58875890

5891+
/* {{{ proto boolean Redis::slowlog(string arg, [int option])
5892+
*/
5893+
PHP_METHOD(Redis, slowlog) {
5894+
zval *object;
5895+
RedisSock *redis_sock;
5896+
char *arg, *cmd;
5897+
int arg_len, cmd_len;
5898+
long option;
5899+
enum {SLOWLOG_GET, SLOWLOG_LEN, SLOWLOG_RESET} mode;
5900+
5901+
// Make sure we can get parameters
5902+
if(zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|l",
5903+
&object, redis_ce, &arg, &arg_len, &option) == FAILURE)
5904+
{
5905+
RETURN_FALSE;
5906+
}
5907+
5908+
// Figure out what kind of slowlog command we're executing
5909+
if(!strncasecmp(arg, "GET", 3)) {
5910+
mode = SLOWLOG_GET;
5911+
} else if(!strncasecmp(arg, "LEN", 3)) {
5912+
mode = SLOWLOG_LEN;
5913+
} else if(!strncasecmp(arg, "RESET", 5)) {
5914+
mode = SLOWLOG_RESET;
5915+
} else {
5916+
// This command is not valid
5917+
RETURN_FALSE;
5918+
}
5919+
5920+
// Make sure we can grab our redis socket
5921+
if(redis_sock_get(object, &redis_sock TSRMLS_CC, 0) < 0) {
5922+
RETURN_FALSE;
5923+
}
5924+
5925+
// Create our command. For everything except SLOWLOG GET (with an arg) it's just two parts
5926+
if(mode == SLOWLOG_GET && ZEND_NUM_ARGS() == 2) {
5927+
cmd_len = redis_cmd_format_static(&cmd, "SLOWLOG", "sl", arg, arg_len, option);
5928+
} else {
5929+
cmd_len = redis_cmd_format_static(&cmd, "SLOWLOG", "s", arg, arg_len);
5930+
}
5931+
5932+
// Kick off our command
5933+
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
5934+
IF_ATOMIC() {
5935+
if(redis_read_variant_reply(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL) < 0) {
5936+
RETURN_FALSE;
5937+
}
5938+
}
5939+
REDIS_PROCESS_RESPONSE(redis_read_variant_reply);
5940+
}
58885941

58895942
// Construct an EVAL or EVALSHA command, with option argument array and number of arguments that are keys parameter
58905943
PHPAPI int
@@ -6511,9 +6564,6 @@ PHP_METHOD(Redis, client) {
65116564
cmd_len = redis_cmd_format_static(&cmd, "CLIENT", "s", opt, opt_len);
65126565
}
65136566

6514-
// Handle CLIENT LIST specifically
6515-
int is_list = !strncasecmp(opt, "list", 4);
6516-
65176567
// Execute our queue command
65186568
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
65196569

tests/TestRedis.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,6 +1684,16 @@ public function testClient() {
16841684
$this->assertTrue($this->redis->client('kill', $str_addr));
16851685
}
16861686

1687+
public function testSlowlog() {
1688+
// We don't really know what's going to be in the slowlog, but make sure
1689+
// the command returns proper types when called in various ways
1690+
$this->assertTrue(is_array($this->redis->slowlog('get')));
1691+
$this->assertTrue(is_array($this->redis->slowlog('get', 10)));
1692+
$this->assertTrue(is_int($this->redis->slowlog('len')));
1693+
$this->assertTrue($this->redis->slowlog('reset'));
1694+
$this->assertFalse($this->redis->slowlog('notvalid'));
1695+
}
1696+
16871697
public function testinfo() {
16881698
$info = $this->redis->info();
16891699

0 commit comments

Comments
 (0)