diff options
author | michael-grunder <michael.grunder@gmail.com> | 2013-08-28 08:26:51 +0400 |
---|---|---|
committer | michael-grunder <michael.grunder@gmail.com> | 2013-08-28 08:26:51 +0400 |
commit | 7207aae8aafd2795e6a11219e710b806a421650d (patch) | |
tree | ceeae0d880fb29700c965831fdb2bda76ebc0572 | |
parent | 6cf2cea4f671052b6c738edb75c921d041d186ac (diff) |
Add SLOWLOG command
Add support for the various slowlog commands you can execute
in Redis, including:
SLOWLOG GET [len]
SLOWLOG RESET
SLOWLOG LIST
-rw-r--r-- | README.markdown | 31 | ||||
-rw-r--r-- | php_redis.h | 1 | ||||
-rw-r--r-- | redis.c | 56 | ||||
-rw-r--r-- | tests/TestRedis.php | 10 |
4 files changed, 95 insertions, 3 deletions
diff --git a/README.markdown b/README.markdown index ce978c05..93544434 100644 --- a/README.markdown +++ b/README.markdown @@ -322,6 +322,7 @@ _**Description**_: Sends a string to Redis, which replies with the same string 1. [save](#save) - Synchronously save the dataset to disk (wait to complete) 1. [slaveof](#slaveof) - Make the server a slave of another instance, or promote it to master 1. [time](#time) - Return the current server time +1. [slowlog](#slowlog) - Access the Redis slowlog entries ### bgrewriteaof ----- @@ -539,6 +540,36 @@ the unix timestamp, and element one being microseconds. $redis->time(); ~~~ +### slowlog +----- +_**Description**_: Access the Redis slowlog + +##### *Parameters* +*Operation* (string): This can be either `GET`, `LEN`, or `RESET` +*Length* (integer), optional: If executing a `SLOWLOG GET` command, you can pass an optional length. +##### + +##### *Return value* +The return value of SLOWLOG will depend on which operation was performed. +SLOWLOG GET: Array of slowlog entries, as provided by Redis +SLOGLOG LEN: Integer, the length of the slowlog +SLOWLOG RESET: Boolean, depending on success +##### + +##### *Examples* +~~~ +// Get ten slowlog entries +$redis->slowlog('get', 10); +// Get the default number of slowlog entries + +$redis->slowlog('get'); +// Reset our slowlog +$redis->slowlog('reset'); + +// Retrieve slowlog length +$redis->slowlog('len'); +~~~ + ## Keys and Strings ### Strings diff --git a/php_redis.h b/php_redis.h index a8f99be0..c0c11f68 100644 --- a/php_redis.h +++ b/php_redis.h @@ -180,6 +180,7 @@ PHP_METHOD(Redis, getOption); PHP_METHOD(Redis, setOption); PHP_METHOD(Redis, config); +PHP_METHOD(Redis, slowlog); PHP_METHOD(Redis, client); @@ -237,6 +237,9 @@ static zend_function_entry redis_functions[] = { /* config */ PHP_ME(Redis, config, NULL, ZEND_ACC_PUBLIC) + /* slowlog */ + PHP_ME(Redis, slowlog, NULL, ZEND_ACC_PUBLIC) + /* introspection */ PHP_ME(Redis, getHost, NULL, ZEND_ACC_PUBLIC) PHP_ME(Redis, getPort, NULL, ZEND_ACC_PUBLIC) @@ -5885,6 +5888,56 @@ PHP_METHOD(Redis, config) /* }}} */ +/* {{{ proto boolean Redis::slowlog(string arg, [int option]) + */ +PHP_METHOD(Redis, slowlog) { + zval *object; + RedisSock *redis_sock; + char *arg, *cmd; + int arg_len, cmd_len; + long option; + enum {SLOWLOG_GET, SLOWLOG_LEN, SLOWLOG_RESET} mode; + + // Make sure we can get parameters + if(zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|l", + &object, redis_ce, &arg, &arg_len, &option) == FAILURE) + { + RETURN_FALSE; + } + + // Figure out what kind of slowlog command we're executing + if(!strncasecmp(arg, "GET", 3)) { + mode = SLOWLOG_GET; + } else if(!strncasecmp(arg, "LEN", 3)) { + mode = SLOWLOG_LEN; + } else if(!strncasecmp(arg, "RESET", 5)) { + mode = SLOWLOG_RESET; + } else { + // This command is not valid + RETURN_FALSE; + } + + // Make sure we can grab our redis socket + if(redis_sock_get(object, &redis_sock TSRMLS_CC, 0) < 0) { + RETURN_FALSE; + } + + // Create our command. For everything except SLOWLOG GET (with an arg) it's just two parts + if(mode == SLOWLOG_GET && ZEND_NUM_ARGS() == 2) { + cmd_len = redis_cmd_format_static(&cmd, "SLOWLOG", "sl", arg, arg_len, option); + } else { + cmd_len = redis_cmd_format_static(&cmd, "SLOWLOG", "s", arg, arg_len); + } + + // Kick off our command + REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len); + IF_ATOMIC() { + if(redis_read_variant_reply(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL) < 0) { + RETURN_FALSE; + } + } + REDIS_PROCESS_RESPONSE(redis_read_variant_reply); +} // Construct an EVAL or EVALSHA command, with option argument array and number of arguments that are keys parameter PHPAPI int @@ -6511,9 +6564,6 @@ PHP_METHOD(Redis, client) { cmd_len = redis_cmd_format_static(&cmd, "CLIENT", "s", opt, opt_len); } - // Handle CLIENT LIST specifically - int is_list = !strncasecmp(opt, "list", 4); - // Execute our queue command REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len); diff --git a/tests/TestRedis.php b/tests/TestRedis.php index 2c0f7e70..ddf828fe 100644 --- a/tests/TestRedis.php +++ b/tests/TestRedis.php @@ -1684,6 +1684,16 @@ class Redis_Test extends TestSuite $this->assertTrue($this->redis->client('kill', $str_addr)); } + public function testSlowlog() { + // We don't really know what's going to be in the slowlog, but make sure + // the command returns proper types when called in various ways + $this->assertTrue(is_array($this->redis->slowlog('get'))); + $this->assertTrue(is_array($this->redis->slowlog('get', 10))); + $this->assertTrue(is_int($this->redis->slowlog('len'))); + $this->assertTrue($this->redis->slowlog('reset')); + $this->assertFalse($this->redis->slowlog('notvalid')); + } + public function testinfo() { $info = $this->redis->info(); |