Skip to content

Commit 742cdd0

Browse files
committed
Handle async parameter for RedisCluster::flushDb and RedisCluster::flushAll
1 parent acd1040 commit 742cdd0

File tree

4 files changed

+65
-12
lines changed

4 files changed

+65
-12
lines changed

common.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,8 +1092,4 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_georadiusbymember, 0, 0, 4)
10921092
ZEND_ARG_ARRAY_INFO(0, opts, 0)
10931093
ZEND_END_ARG_INFO()
10941094

1095-
ZEND_BEGIN_ARG_INFO_EX(arginfo_flush, 0, 0, 0)
1096-
ZEND_ARG_INFO(0, async)
1097-
ZEND_END_ARG_INFO()
1098-
10991095
#endif

redis.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_config, 0, 0, 2)
108108
ZEND_ARG_INFO(0, value)
109109
ZEND_END_ARG_INFO()
110110

111+
ZEND_BEGIN_ARG_INFO_EX(arginfo_flush, 0, 0, 0)
112+
ZEND_ARG_INFO(0, async)
113+
ZEND_END_ARG_INFO()
114+
111115
ZEND_BEGIN_ARG_INFO_EX(arginfo_pubsub, 0, 0, 1)
112116
ZEND_ARG_INFO(0, cmd)
113117
#if PHP_VERSION_ID >= 50600

redis_array.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_multi, 0, 0, 1)
9797
ZEND_ARG_INFO(0, mode)
9898
ZEND_END_ARG_INFO()
9999

100+
ZEND_BEGIN_ARG_INFO_EX(arginfo_flush, 0, 0, 0)
101+
ZEND_ARG_INFO(0, async)
102+
ZEND_END_ARG_INFO()
103+
100104
zend_function_entry redis_array_functions[] = {
101105
PHP_ME(RedisArray, __call, arginfo_call, ZEND_ACC_PUBLIC)
102106
PHP_ME(RedisArray, __construct, arginfo_ctor, ZEND_ACC_PUBLIC)

redis_cluster.c

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_info, 0, 0, 1)
8484
ZEND_ARG_INFO(0, option)
8585
ZEND_END_ARG_INFO()
8686

87+
ZEND_BEGIN_ARG_INFO_EX(arginfo_flush, 0, 0, 1)
88+
ZEND_ARG_INFO(0, key_or_address)
89+
ZEND_ARG_INFO(0, async)
90+
ZEND_END_ARG_INFO()
91+
8792
/* Argument info for HSCAN, SSCAN, HSCAN */
8893
ZEND_BEGIN_ARG_INFO_EX(arginfo_kscan_cl, 0, 0, 2)
8994
ZEND_ARG_INFO(0, str_key)
@@ -136,8 +141,8 @@ zend_function_entry redis_cluster_functions[] = {
136141
PHP_ME(RedisCluster, exists, arginfo_key, ZEND_ACC_PUBLIC)
137142
PHP_ME(RedisCluster, expire, arginfo_expire, ZEND_ACC_PUBLIC)
138143
PHP_ME(RedisCluster, expireat, arginfo_key_timestamp, ZEND_ACC_PUBLIC)
139-
PHP_ME(RedisCluster, flushall, arginfo_key_or_address, ZEND_ACC_PUBLIC)
140-
PHP_ME(RedisCluster, flushdb, arginfo_key_or_address, ZEND_ACC_PUBLIC)
144+
PHP_ME(RedisCluster, flushall, arginfo_flush, ZEND_ACC_PUBLIC)
145+
PHP_ME(RedisCluster, flushdb, arginfo_flush, ZEND_ACC_PUBLIC)
141146
PHP_ME(RedisCluster, geoadd, arginfo_geoadd, ZEND_ACC_PUBLIC)
142147
PHP_ME(RedisCluster, geodist, arginfo_geodist, ZEND_ACC_PUBLIC)
143148
PHP_ME(RedisCluster, geohash, arginfo_key_members, ZEND_ACC_PUBLIC)
@@ -2334,6 +2339,50 @@ cluster_empty_node_cmd(INTERNAL_FUNCTION_PARAMETERS, char *kw,
23342339
efree(cmd);
23352340
}
23362341

2342+
static void
2343+
cluster_flush_cmd(INTERNAL_FUNCTION_PARAMETERS, char *kw, REDIS_REPLY_TYPE reply_type, cluster_cb cb)
2344+
{
2345+
redisCluster *c = GET_CONTEXT();
2346+
char *cmd;
2347+
int cmd_len;
2348+
zval *z_arg;
2349+
zend_bool async = 0;
2350+
short slot;
2351+
2352+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|b", &z_arg, &async) == FAILURE) {
2353+
RETURN_FALSE;
2354+
}
2355+
2356+
// One argument means find the node (treated like a key), and two means
2357+
// send the command to a specific host and port
2358+
slot = cluster_cmd_get_slot(c, z_arg TSRMLS_CC);
2359+
if (slot < 0) {
2360+
RETURN_FALSE;
2361+
}
2362+
2363+
// Construct our command
2364+
if (async) {
2365+
cmd_len = redis_spprintf(NULL, NULL TSRMLS_CC, &cmd, kw, "s", "ASYNC", sizeof("ASYNC") - 1);
2366+
} else {
2367+
cmd_len = redis_spprintf(NULL, NULL TSRMLS_CC, &cmd, kw, "");
2368+
}
2369+
2370+
2371+
// Kick off our command
2372+
if (cluster_send_slot(c, slot, cmd, cmd_len, reply_type TSRMLS_CC) < 0) {
2373+
zend_throw_exception(redis_cluster_exception_ce,
2374+
"Unable to send command at a specific node", 0 TSRMLS_CC);
2375+
efree(cmd);
2376+
RETURN_FALSE;
2377+
}
2378+
2379+
// Our response callback
2380+
cb(INTERNAL_FUNCTION_PARAM_PASSTHRU, c, NULL);
2381+
2382+
// Free our command
2383+
efree(cmd);
2384+
}
2385+
23372386
/* Generic routine for handling various commands which need to be directed at
23382387
* a node, but have complex syntax. We simply parse out the arguments and send
23392388
* the command as constructed by the caller */
@@ -2611,18 +2660,18 @@ PHP_METHOD(RedisCluster, bgsave) {
26112660
}
26122661
/* }}} */
26132662

2614-
/* {{{ proto RedisCluster::flushdb(string key)
2615-
* proto RedisCluster::flushdb(string host, long port) */
2663+
/* {{{ proto RedisCluster::flushdb(string key, [bool async])
2664+
* proto RedisCluster::flushdb(array host_port, [bool async]) */
26162665
PHP_METHOD(RedisCluster, flushdb) {
2617-
cluster_empty_node_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, "FLUSHDB",
2666+
cluster_flush_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, "FLUSHDB",
26182667
TYPE_LINE, cluster_bool_resp);
26192668
}
26202669
/* }}} */
26212670

2622-
/* {{{ proto RedisCluster::flushall(string key)
2623-
* proto RedisCluster::flushall(string host, long port) */
2671+
/* {{{ proto RedisCluster::flushall(string key, [bool async])
2672+
* proto RedisCluster::flushall(array host_port, [bool async]) */
26242673
PHP_METHOD(RedisCluster, flushall) {
2625-
cluster_empty_node_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, "FLUSHALL",
2674+
cluster_flush_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, "FLUSHALL",
26262675
TYPE_LINE, cluster_bool_resp);
26272676
}
26282677
/* }}} */

0 commit comments

Comments
 (0)