diff --git a/README.markdown b/README.markdown index 329abcf..2106f96 100644 --- a/README.markdown +++ b/README.markdown @@ -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 diff --git a/common.h b/common.h index f1b85b2..7abaecd 100644 --- a/common.h +++ b/common.h @@ -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 diff --git a/redis.stub.php b/redis.stub.php index 41f9fff..bdffb41 100644 --- a/redis.stub.php +++ b/redis.stub.php @@ -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; diff --git a/redis_arginfo.h b/redis_arginfo.h index 61c40e7..5f9b501 100644 --- a/redis_arginfo.h +++ b/redis_arginfo.h @@ -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() diff --git a/redis_commands.c b/redis_commands.c index f74b279..098b3a9 100644 --- a/redis_commands.c +++ b/redis_commands.c @@ -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; } diff --git a/redis_legacy_arginfo.h b/redis_legacy_arginfo.h index d2a8c22..e2ff15c 100644 --- a/redis_legacy_arginfo.h +++ b/redis_legacy_arginfo.h @@ -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) diff --git a/tests/RedisClusterTest.php b/tests/RedisClusterTest.php index 132c374..4030fae 100644 --- a/tests/RedisClusterTest.php +++ b/tests/RedisClusterTest.php @@ -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 */ diff --git a/tests/RedisTest.php b/tests/RedisTest.php index c55f65a..2e869e9 100644 --- a/tests/RedisTest.php +++ b/tests/RedisTest.php @@ -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);