RANDOMKEY + tests.

This commit is contained in:
Nicolas Favre-Felix
2009-11-18 15:29:36 +01:00
parent 9ce7c89e19
commit 73cd13194e
3 changed files with 56 additions and 0 deletions
+1
View File
@@ -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);
+47
View File
@@ -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)
+8
View File
@@ -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');