mirror of
https://github.com/phpredis/phpredis.git
synced 2026-06-19 07:35:31 +00:00
RANDOMKEY + tests.
This commit is contained in:
@@ -29,6 +29,7 @@ PHP_METHOD(Redis, get);
|
||||
PHP_METHOD(Redis, set);
|
||||
PHP_METHOD(Redis, setnx);
|
||||
PHP_METHOD(Redis, getSet);
|
||||
PHP_METHOD(Redis, randomKey);
|
||||
PHP_METHOD(Redis, add);
|
||||
PHP_METHOD(Redis, getMultiple);
|
||||
PHP_METHOD(Redis, exists);
|
||||
|
||||
@@ -43,6 +43,7 @@ zend_function_entry redis_functions[] = {
|
||||
PHP_ME(Redis, set, NULL, ZEND_ACC_PUBLIC)
|
||||
PHP_ME(Redis, setnx, NULL, ZEND_ACC_PUBLIC)
|
||||
PHP_ME(Redis, getSet, NULL, ZEND_ACC_PUBLIC)
|
||||
PHP_ME(Redis, randomKey, NULL, ZEND_ACC_PUBLIC)
|
||||
PHP_ME(Redis, add, NULL, ZEND_ACC_PUBLIC)
|
||||
PHP_ME(Redis, getMultiple, NULL, ZEND_ACC_PUBLIC)
|
||||
PHP_ME(Redis, exists, NULL, ZEND_ACC_PUBLIC)
|
||||
@@ -720,6 +721,52 @@ PHP_METHOD(Redis, getSet)
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ proto string Redis::randomKey()
|
||||
*/
|
||||
PHP_METHOD(Redis, randomKey)
|
||||
{
|
||||
|
||||
zval *object;
|
||||
RedisSock *redis_sock;
|
||||
char *cmd, *response, *ret;
|
||||
int cmd_len, response_len;
|
||||
|
||||
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
|
||||
&object, redis_ce) == FAILURE) {
|
||||
RETURN_FALSE;
|
||||
}
|
||||
|
||||
if (redis_sock_get(object, &redis_sock TSRMLS_CC) < 0) {
|
||||
RETURN_FALSE;
|
||||
}
|
||||
|
||||
cmd_len = redis_cmd_format(&cmd, "RANDOMKEY\r\n");
|
||||
|
||||
if (redis_sock_write(redis_sock, cmd, cmd_len) < 0) {
|
||||
efree(cmd);
|
||||
RETURN_FALSE;
|
||||
}
|
||||
efree(cmd);
|
||||
if ((response = redis_sock_read(redis_sock, &response_len TSRMLS_CC)) == NULL) {
|
||||
RETURN_FALSE;
|
||||
}
|
||||
|
||||
if(response == NULL) {
|
||||
RETURN_FALSE;
|
||||
}
|
||||
|
||||
if(response[0] == '+') {
|
||||
ret = estrndup(response+1, response_len-1);
|
||||
efree(response);
|
||||
RETURN_STRINGL(ret, response_len-1, 0); // need to remove the '+'
|
||||
} else {
|
||||
efree(response);
|
||||
RETURN_FALSE;
|
||||
}
|
||||
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ proto string Redis::get(string key)
|
||||
*/
|
||||
PHP_METHOD(Redis, get)
|
||||
|
||||
@@ -118,6 +118,14 @@ class Redis_Test extends PHPUnit_Framework_TestCase
|
||||
$this->assertTrue($this->redis->getSet('key', '123') === '123');
|
||||
}
|
||||
|
||||
public function testRandomKey() {
|
||||
|
||||
for($i = 0; $i < 1000; $i++) {
|
||||
$k = $this->redis->randomKey();
|
||||
$this->assertTrue($this->redis->exists($k));
|
||||
}
|
||||
}
|
||||
|
||||
public function testMultiple() {
|
||||
|
||||
$this->redis->delete('k1');
|
||||
|
||||
Reference in New Issue
Block a user