Refactor FLUSHDB and update docs.

Fixes #2096
This commit is contained in:
michael-grunder
2022-10-10 22:58:41 -07:00
committed by Michael Grunder
parent 457953f4fe
commit 54a084e51c
8 changed files with 58 additions and 18 deletions

View File

@@ -608,11 +608,13 @@ $redis->flushAll();
-----
_**Description**_: Remove all keys from the current database.
##### *Parameters*
*async* (bool) requires server version 4.0.0 or greater
##### *Prototype*
~~~php
$redis->flushdb(?bool $sync = NULL): Redis|bool;
~~~
##### *Return value*
*BOOL*: Always `TRUE`.
*BOOL*: This command returns true on success and false on failure.
##### *Example*
~~~php

View File

@@ -153,6 +153,8 @@ typedef enum {
Z_PARAM_STR_EX(dest, 1, 0)
#define Z_PARAM_ZVAL_OR_NULL(dest) \
Z_PARAM_ZVAL_EX(dest, 1, 0)
#define Z_PARAM_BOOL_OR_NULL(dest, is_null) \
Z_PARAM_BOOL_EX(dest, is_null, 1, 0)
#endif
#if PHPREDIS_DEBUG_LOGGING == 1

View File

@@ -132,9 +132,29 @@ class Redis {
public function pexpiretime(string $key): Redis|int|false;
public function flushAll(?bool $sync = null): bool;
/**
* Deletes every key in all Redis databases
*
* @param bool $sync Whether to perform the task in a blocking or non-blocking way.
* when TRUE, PhpRedis will execute `FLUSHALL SYNC`, and when FALSE we
* will execute `FLUSHALL ASYNC`. If the argument is omitted, we
* simply execute `FLUSHALL` and whether it is SYNC or ASYNC depends
* on Redis' `lazyfree-lazy-user-flush` config setting.
* @return bool
*/
public function flushAll(?bool $sync = null): Redis|bool;
public function flushDB(?bool $sync = null): bool;
/**
* Deletes all the keys of the currently selected database.
*
* @param bool $sync Whether to perform the task in a blocking or non-blocking way.
* when TRUE, PhpRedis will execute `FLUSHDB SYNC`, and when FALSE we
* will execute `FLUSHDB ASYNC`. If the argument is omitted, we
* simply execute `FLUSHDB` and whether it is SYNC or ASYNC depends
* on Redis' `lazyfree-lazy-user-flush` config setting.
* @return bool
*/
public function flushDB(?bool $sync = null): Redis|bool;
public function geoadd(string $key, float $lng, float $lat, string $member, mixed ...$other_triples): int;

View File

@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 73e34ca5d2f49dd1dbcbf901d3dd48019e1ba5dc */
* Stub hash: c9de2943a9517d8007381f36a47ab45ef911ae67 */
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "null")
@@ -217,7 +217,7 @@ ZEND_END_ARG_INFO()
#define arginfo_class_Redis_pexpiretime arginfo_class_Redis_expiretime
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Redis_flushAll, 0, 0, _IS_BOOL, 0)
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_Redis_flushAll, 0, 0, Redis, MAY_BE_BOOL)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, sync, _IS_BOOL, 1, "null")
ZEND_END_ARG_INFO()

View File

@@ -529,19 +529,27 @@ redis_failover_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
int redis_flush_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx)
{
int sync = -1;
smart_string cmdstr = {0};
zend_bool sync = 0;
zend_bool is_null = 1;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &sync) == FAILURE) {
return FAILURE;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL_OR_NULL(sync, is_null)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
redis_cmd_init_sstr(&cmdstr, !is_null, kw, strlen(kw));
if (!is_null) {
ZEND_ASSERT(sync == 0 || sync == 1);
if (sync == 0) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "ASYNC");
} else {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "SYNC");
}
}
if (sync < 0) {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "");
} else if (sync > 0) {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "s", "SYNC", sizeof("SYNC") - 1);
} else {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "s", "ASYNC", sizeof("ASYNC") - 1);
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}

View File

@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 73e34ca5d2f49dd1dbcbf901d3dd48019e1ba5dc */
* Stub hash: c9de2943a9517d8007381f36a47ab45ef911ae67 */
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
ZEND_ARG_INFO(0, options)

View File

@@ -65,6 +65,7 @@ class Redis_Cluster_Test extends Redis_Test {
public function testGeoSearchStore() { return $this->marktestSkipped(); }
public function testHRandField() { return $this->marktestSkipped(); }
public function testConfig() { return $this->markTestSkipped(); }
public function testFlushDB() { return $this->markTestSkipped(); }
/* Session locking feature is currently not supported in in context of Redis Cluster.
The biggest issue for this is the distribution nature of Redis cluster */

View File

@@ -2160,6 +2160,13 @@ class Redis_Test extends TestSuite
$this->assertTrue($this->redis->dbSize() === 1);
}
public function testFlushDB() {
$this->assertTrue($this->redis->flushdb());
$this->assertTrue($this->redis->flushdb(NULL));
$this->assertTrue($this->redis->flushdb(false));
$this->assertTrue($this->redis->flushdb(true));
}
public function testttl() {
$this->redis->set('x', 'y');
$this->redis->expire('x', 5);