Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6e49417

Browse files
Allow PING to take an optional argument.
Addresses #1563
1 parent 19f3efc commit 6e49417

6 files changed

Lines changed: 106 additions & 14 deletions

File tree

redis.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ PHP_METHOD(Redis, get)
10931093
*/
10941094
PHP_METHOD(Redis, ping)
10951095
{
1096-
REDIS_PROCESS_KW_CMD("PING", redis_empty_cmd, redis_ping_response);
1096+
REDIS_PROCESS_KW_CMD("PING", redis_opt_str_cmd, redis_read_variant_reply);
10971097
}
10981098
/* }}} */
10991099

redis_cluster.c

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3002,11 +3002,69 @@ PHP_METHOD(RedisCluster, randomkey) {
30023002
}
30033003
/* }}} */
30043004

3005-
/* {{{ proto bool RedisCluster::ping(string key)
3006-
* proto bool RedisCluster::ping(array host_port) */
3005+
/* {{{ proto bool RedisCluster::ping(string key| string msg)
3006+
* proto bool RedisCluster::ping(array host_port| string msg) */
30073007
PHP_METHOD(RedisCluster, ping) {
3008-
cluster_empty_node_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, "PING",
3009-
TYPE_LINE, cluster_ping_resp);
3008+
redisCluster *c = GET_CONTEXT();
3009+
REDIS_REPLY_TYPE rtype;
3010+
void *ctx = NULL;
3011+
zval *z_node;
3012+
char *cmd, *arg = NULL;
3013+
int cmdlen;
3014+
size_t arglen;
3015+
short slot;
3016+
3017+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s!", &z_node, &arg,
3018+
&arglen) == FAILURE)
3019+
{
3020+
RETURN_FALSE;
3021+
}
3022+
3023+
/* Treat this as a readonly command */
3024+
c->readonly = CLUSTER_IS_ATOMIC(c);
3025+
3026+
/* Grab slot either by key or host/port */
3027+
slot = cluster_cmd_get_slot(c, z_node TSRMLS_CC);
3028+
if (slot < 0) {
3029+
RETURN_FALSE;
3030+
}
3031+
3032+
/* Construct our command */
3033+
if (arg != NULL) {
3034+
cmdlen = redis_spprintf(NULL, NULL TSRMLS_CC, &cmd, "PING", "s", arg, arglen);
3035+
} else {
3036+
cmdlen = redis_spprintf(NULL, NULL TSRMLS_CC, &cmd, "PING", "");
3037+
}
3038+
3039+
/* Send it off */
3040+
rtype = CLUSTER_IS_ATOMIC(c) && arg != NULL ? TYPE_BULK : TYPE_LINE;
3041+
if (cluster_send_slot(c, slot, cmd, cmdlen, rtype TSRMLS_CC) < 0) {
3042+
CLUSTER_THROW_EXCEPTION("Unable to send commnad at the specificed node", 0);
3043+
efree(cmd);
3044+
RETURN_FALSE;
3045+
}
3046+
3047+
/* We're done with our command */
3048+
efree(cmd);
3049+
3050+
/* Process response */
3051+
if (CLUSTER_IS_ATOMIC(c)) {
3052+
if (arg != NULL) {
3053+
cluster_bulk_resp(INTERNAL_FUNCTION_PARAM_PASSTHRU, c, NULL);
3054+
} else {
3055+
/* If we're atomic and didn't send an argument then we have already
3056+
* processed the reply (which must have been successful. */
3057+
RETURN_TRUE;
3058+
}
3059+
} else {
3060+
if (arg != NULL) {
3061+
CLUSTER_ENQUEUE_RESPONSE(c, slot, cluster_bulk_resp, ctx);
3062+
} else {
3063+
CLUSTER_ENQUEUE_RESPONSE(c, slot, cluster_variant_resp, ctx);
3064+
}
3065+
3066+
RETURN_ZVAL(getThis(), 1, 0);
3067+
}
30103068
}
30113069
/* }}} */
30123070

redis_commands.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,26 @@ redis_build_script_cmd(smart_string *cmd, int argc, zval *z_args)
168168
return cmd;
169169
}
170170

171+
/* Command that takes one optional string */
172+
int redis_opt_str_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, char *kw,
173+
char **cmd, int *cmd_len, short *slot, void **ctx)
174+
{
175+
char *arg = NULL;
176+
size_t arglen;
177+
178+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &arg, &arglen) == FAILURE) {
179+
return FAILURE;
180+
}
181+
182+
if (arg != NULL) {
183+
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "s", arg, arglen);
184+
} else {
185+
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "");
186+
}
187+
188+
return SUCCESS;
189+
}
190+
171191
/* Generic command where we just take a string and do nothing to it*/
172192
int redis_str_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, char *kw,
173193
char **cmd, int *cmd_len, short *slot, void **ctx)

redis_commands.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ smart_string *redis_build_script_cmd(smart_string *cmd, int argc, zval *z_args);
3333
int redis_empty_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
3434
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx);
3535

36+
int redis_opt_str_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, char *kw,
37+
char **cmd, int *cmd_len, short *slot, void **ctx);
38+
3639
int redis_str_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
3740
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx);
3841

tests/RedisClusterTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,16 @@ protected function newInstance() {
9595
* at a specific node */
9696

9797
public function testPing() {
98-
for ($i = 0; $i < 100; $i++) {
98+
for ($i = 0; $i < 20; $i++) {
9999
$this->assertTrue($this->redis->ping("key:$i"));
100+
$this->assertEquals('BEEP', $this->redis->ping("key:$i", 'BEEP'));
100101
}
102+
103+
/* Make sure both variations work in MULTI mode */
104+
$this->redis->multi();
105+
$this->redis->ping('{ping-test}');
106+
$this->redis->ping('{ping-test}','BEEP');
107+
$this->assertEquals([true, 'BEEP'], $this->redis->exec());
101108
}
102109

103110
public function testRandomKey() {

tests/RedisTest.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,18 @@ public function testMinimumVersion()
8989
$this->assertTrue(version_compare($this->version, "2.4.0", "ge"));
9090
}
9191

92-
public function testPing()
93-
{
94-
$this->assertEquals('+PONG', $this->redis->ping());
92+
public function testPing() {
93+
/* Reply literal off */
94+
$this->assertTrue($this->redis->ping());
95+
$this->assertTrue($this->redis->ping(NULL));
96+
$this->assertEquals('BEEP', $this->redis->ping('BEEP'));
9597

96-
$count = 1000;
97-
while($count --) {
98-
$this->assertEquals('+PONG', $this->redis->ping());
99-
}
98+
/* Make sure we're good in MULTI mode */
99+
$this->redis->multi();
100+
101+
$this->redis->ping();
102+
$this->redis->ping('BEEP');
103+
$this->assertEquals([true, 'BEEP'], $this->redis->exec());
100104
}
101105

102106
public function testPipelinePublish() {
@@ -6011,7 +6015,7 @@ public function testMultipleConnect() {
60116015

60126016
for($i = 0; $i < 5; $i++) {
60136017
$this->redis->connect($host, $port);
6014-
$this->assertEquals($this->redis->ping(), "+PONG");
6018+
$this->assertEquals(true, $this->redis->ping());
60156019
}
60166020
}
60176021

0 commit comments

Comments
 (0)