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

Skip to content

Commit f2cef8b

Browse files
More docblocks and refactor SLAVEOF command.
Add additional complete docblocks for a few more commands. Refactor SLAVEOF handler to conform with more modern PhpRedis command handlers. Create REPLICAOF and deprecate SLAVEOF as Redis has done since 5.0.0.
1 parent 7d5db51 commit f2cef8b

6 files changed

Lines changed: 143 additions & 39 deletions

File tree

redis.c

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,41 +2559,18 @@ PHP_METHOD(Redis, bgrewriteaof)
25592559
}
25602560
/* }}} */
25612561

2562-
/* {{{ proto string Redis::slaveof([host, port]) */
2563-
PHP_METHOD(Redis, slaveof)
2564-
{
2565-
zval *object;
2566-
RedisSock *redis_sock;
2567-
char *cmd = "", *host = NULL;
2568-
size_t host_len;
2569-
zend_long port = 6379;
2570-
int cmd_len;
2571-
2572-
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
2573-
"O|sl", &object, redis_ce, &host,
2574-
&host_len, &port) == FAILURE)
2575-
{
2576-
RETURN_FALSE;
2577-
}
2578-
if (port < 0 || (redis_sock = redis_sock_get(object, 0)) == NULL) {
2579-
RETURN_FALSE;
2580-
}
2581-
2582-
if (host && host_len) {
2583-
cmd_len = REDIS_SPPRINTF(&cmd, "SLAVEOF", "sd", host, host_len, (int)port);
2584-
} else {
2585-
cmd_len = REDIS_SPPRINTF(&cmd, "SLAVEOF", "ss", "NO", 2, "ONE", 3);
2586-
}
2587-
2588-
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
2589-
if (IS_ATOMIC(redis_sock)) {
2590-
redis_boolean_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock,
2591-
NULL, NULL);
2592-
}
2593-
REDIS_PROCESS_RESPONSE(redis_boolean_response);
2562+
/* {{{ public function slaveof(string $host = NULL, int $port = NULL): Redis|bool }}} */
2563+
PHP_METHOD(Redis, slaveof) {
2564+
REDIS_PROCESS_KW_CMD("SLAVEOF", redis_replicaof_cmd, redis_boolean_response);
25942565
}
25952566
/* }}} */
25962567

2568+
/* {{{ public function replicaof(string $host = NULL, int $port = NULL): Redis|bool }}} */
2569+
PHP_METHOD(Redis, replicaof) {
2570+
REDIS_PROCESS_KW_CMD("REPLICAOF", redis_replicaof_cmd, redis_boolean_response);
2571+
}
2572+
2573+
/* }}} */
25972574
/* {{{ proto string Redis::object(key) */
25982575
PHP_METHOD(Redis, object)
25992576
{

redis.stub.php

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1751,14 +1751,102 @@ public function setRange(string $key, int $start, string $value);
17511751
public function setOption(int $option, mixed $value): bool;
17521752

17531753
/** @return bool|Redis */
1754+
1755+
/**
1756+
* Set a Redis STRING key with a specific expiration in seconds.
1757+
*
1758+
* @param string $key The name of the key to set.
1759+
* @param int $expire The key's expiration in seconds.
1760+
* @param mixed $value The value to set the key.
1761+
*
1762+
* @return Redis|bool True on success or false on failure.
1763+
*
1764+
* <code>
1765+
* <?php
1766+
* $redis = new Redis(['host' => 'localhost']);
1767+
*
1768+
* // Set a key with a 60 second expiration
1769+
* $redis->set('some_key', 60, 'some_value');
1770+
*
1771+
* ?>php
1772+
* </code>
1773+
*/
17541774
public function setex(string $key, int $expire, mixed $value);
17551775

17561776
/** @return bool|array|Redis */
17571777
public function setnx(string $key, mixed $value);
17581778

1779+
/**
1780+
* Check whether a given value is the member of a Redis SET.
1781+
*
1782+
* @param string $key The redis set to check.
1783+
* @param mixed $value The value to test.
1784+
*
1785+
* @return Redis|bool True if the member exists and false if not.
1786+
*
1787+
* <code>
1788+
* <?php
1789+
* $redis = new Redis(['host' => 'localhost']);
1790+
*
1791+
* $redis->multi()
1792+
* ->del('myset')
1793+
* ->sadd('myset', 'foo', 'bar', 'baz')
1794+
* ->exec();
1795+
*
1796+
* // Will return true, as 'foo' is in the set
1797+
* $redis->sismember('myset', 'foo');
1798+
*
1799+
* // Will return false, as 'not-in-set' is not in the set
1800+
* $redis->sismember('myset', 'not-in-set');
1801+
* ?>
1802+
* </code>
1803+
*/
17591804
public function sismember(string $key, mixed $value): Redis|bool;
17601805

1761-
public function slaveof(string $host = null, int $port = 6379): bool;
1806+
/**
1807+
* Turn a redis instance into a replica of another or promote a replica
1808+
* to a primary.
1809+
*
1810+
* This method and the corresponding command in Redis has been marked deprecated
1811+
* and users should instead use Redis::replicaof() if connecting to redis-server
1812+
* >= 5.0.0.
1813+
*
1814+
* @deprecated
1815+
*
1816+
* @see https://redis.io/commands/slaveof
1817+
* @see https://redis.io/commands/replicaof
1818+
* @see Redis::slaveof()
1819+
*/
1820+
public function slaveof(string $host = NULL, int $port = 6379): Redis|bool;
1821+
1822+
/**
1823+
* Used to turn a Redis instance into a replica of another, or to remove
1824+
* replica status promoting the instance to a primary.
1825+
*
1826+
* @see https://redis.io/commands/replicaof
1827+
* @see https://redis.io/commands/slaveof
1828+
* @see Redis::slaveof()
1829+
*
1830+
* @param string $host The host of the primary to start replicating.
1831+
* @param string $port The port of the primary to start replicating.
1832+
*
1833+
* @return Redis|bool Success if we were successfully able to start replicating a primary or
1834+
* were able to promote teh replicat to a primary.
1835+
*
1836+
* <code>
1837+
* <?php
1838+
* $redis = new Redis(['host' => 'localhost']);
1839+
*
1840+
* // Attempt to become a replica of a Redis instance at 127.0.0.1:9999
1841+
* $redis->slaveof('127.0.0.1', 9999);
1842+
*
1843+
* // When passed no arguments, PhpRedis will deliver the command `SLAVEOF NO ONE`
1844+
* // attempting to promote the instance to a primary.
1845+
* $redis->slaveof();
1846+
* ?>
1847+
* </code>
1848+
*/
1849+
public function replicaof(string $host = NULL, int $port = 6379): Redis|bool;
17621850

17631851
/**
17641852
* Update one or more keys last modified metadata.

redis_arginfo.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 5554480fcf6749dcfd69f371ad8dbd07fd8ed4c4 */
2+
* Stub hash: 8c51e9b0082cb7939c7bc8dc226132eede02f420 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
55
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "null")
@@ -788,11 +788,13 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_Redis_sismember, 0, 2,
788788
ZEND_ARG_TYPE_INFO(0, value, IS_MIXED, 0)
789789
ZEND_END_ARG_INFO()
790790

791-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Redis_slaveof, 0, 0, _IS_BOOL, 0)
792-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, host, IS_STRING, 0, "null")
791+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_Redis_slaveof, 0, 0, Redis, MAY_BE_BOOL)
792+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, host, IS_STRING, 0, "NULL")
793793
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, port, IS_LONG, 0, "6379")
794794
ZEND_END_ARG_INFO()
795795

796+
#define arginfo_class_Redis_replicaof arginfo_class_Redis_slaveof
797+
796798
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_Redis_touch, 0, 1, Redis, MAY_BE_LONG|MAY_BE_FALSE)
797799
ZEND_ARG_TYPE_MASK(0, key_or_array, MAY_BE_ARRAY|MAY_BE_STRING, NULL)
798800
ZEND_ARG_VARIADIC_TYPE_INFO(0, more_keys, IS_STRING, 0)
@@ -1291,6 +1293,7 @@ ZEND_METHOD(Redis, setex);
12911293
ZEND_METHOD(Redis, setnx);
12921294
ZEND_METHOD(Redis, sismember);
12931295
ZEND_METHOD(Redis, slaveof);
1296+
ZEND_METHOD(Redis, replicaof);
12941297
ZEND_METHOD(Redis, touch);
12951298
ZEND_METHOD(Redis, slowlog);
12961299
ZEND_METHOD(Redis, sort);
@@ -1539,7 +1542,8 @@ static const zend_function_entry class_Redis_methods[] = {
15391542
ZEND_ME(Redis, setex, arginfo_class_Redis_setex, ZEND_ACC_PUBLIC)
15401543
ZEND_ME(Redis, setnx, arginfo_class_Redis_setnx, ZEND_ACC_PUBLIC)
15411544
ZEND_ME(Redis, sismember, arginfo_class_Redis_sismember, ZEND_ACC_PUBLIC)
1542-
ZEND_ME(Redis, slaveof, arginfo_class_Redis_slaveof, ZEND_ACC_PUBLIC)
1545+
ZEND_ME(Redis, slaveof, arginfo_class_Redis_slaveof, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
1546+
ZEND_ME(Redis, replicaof, arginfo_class_Redis_replicaof, ZEND_ACC_PUBLIC)
15431547
ZEND_ME(Redis, touch, arginfo_class_Redis_touch, ZEND_ACC_PUBLIC)
15441548
ZEND_ME(Redis, slowlog, arginfo_class_Redis_slowlog, ZEND_ACC_PUBLIC)
15451549
ZEND_ME(Redis, sort, arginfo_class_Redis_sort, ZEND_ACC_PUBLIC)

redis_commands.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,33 @@ int redis_intercard_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
11261126
return SUCCESS;
11271127
}
11281128

1129+
int redis_replicaof_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
1130+
char *kw, char **cmd, int *cmd_len, short *slot,
1131+
void **ctx)
1132+
{
1133+
zend_string *host = NULL;
1134+
zend_long port = 6379;
1135+
1136+
ZEND_PARSE_PARAMETERS_START(0, 2)
1137+
Z_PARAM_OPTIONAL
1138+
Z_PARAM_STR(host)
1139+
Z_PARAM_LONG(port)
1140+
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
1141+
1142+
if (port < 0 || port > UINT16_MAX) {
1143+
php_error_docref(NULL, E_WARNING, "Invalid port %ld", (long)port);
1144+
return FAILURE;
1145+
}
1146+
1147+
if (ZEND_NUM_ARGS() == 2) {
1148+
*cmd_len = REDIS_SPPRINTF(cmd, kw, "Sd", host, (int)port);
1149+
} else {
1150+
*cmd_len = REDIS_SPPRINTF(cmd, kw, "ss", ZEND_STRL("NO"), ZEND_STRL("ONE"));
1151+
}
1152+
1153+
return SUCCESS;
1154+
}
1155+
11291156
int
11301157
redis_zinterunion_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
11311158
char *kw, char **cmd, int *cmd_len, short *slot,

redis_commands.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ char *redis_variadic_str_cmd(char *kw, zval *argv, int argc, int *cmd_len);
3737
* we can write one function to handle all of them. For example, there are
3838
* many COMMAND key value commands, or COMMAND key commands. */
3939

40+
int redis_replicaof_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
41+
char *kw, char **cmd, int *cmd_len, short *slot,
42+
void **ctx);
43+
4044
int redis_empty_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
4145
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx);
4246

redis_legacy_arginfo.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 5554480fcf6749dcfd69f371ad8dbd07fd8ed4c4 */
2+
* Stub hash: 8c51e9b0082cb7939c7bc8dc226132eede02f420 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
55
ZEND_ARG_INFO(0, options)
@@ -673,6 +673,8 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_slaveof, 0, 0, 0)
673673
ZEND_ARG_INFO(0, port)
674674
ZEND_END_ARG_INFO()
675675

676+
#define arginfo_class_Redis_replicaof arginfo_class_Redis_slaveof
677+
676678
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_touch, 0, 0, 1)
677679
ZEND_ARG_INFO(0, key_or_array)
678680
ZEND_ARG_VARIADIC_INFO(0, more_keys)
@@ -1129,6 +1131,7 @@ ZEND_METHOD(Redis, setex);
11291131
ZEND_METHOD(Redis, setnx);
11301132
ZEND_METHOD(Redis, sismember);
11311133
ZEND_METHOD(Redis, slaveof);
1134+
ZEND_METHOD(Redis, replicaof);
11321135
ZEND_METHOD(Redis, touch);
11331136
ZEND_METHOD(Redis, slowlog);
11341137
ZEND_METHOD(Redis, sort);
@@ -1377,7 +1380,8 @@ static const zend_function_entry class_Redis_methods[] = {
13771380
ZEND_ME(Redis, setex, arginfo_class_Redis_setex, ZEND_ACC_PUBLIC)
13781381
ZEND_ME(Redis, setnx, arginfo_class_Redis_setnx, ZEND_ACC_PUBLIC)
13791382
ZEND_ME(Redis, sismember, arginfo_class_Redis_sismember, ZEND_ACC_PUBLIC)
1380-
ZEND_ME(Redis, slaveof, arginfo_class_Redis_slaveof, ZEND_ACC_PUBLIC)
1383+
ZEND_ME(Redis, slaveof, arginfo_class_Redis_slaveof, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
1384+
ZEND_ME(Redis, replicaof, arginfo_class_Redis_replicaof, ZEND_ACC_PUBLIC)
13811385
ZEND_ME(Redis, touch, arginfo_class_Redis_touch, ZEND_ACC_PUBLIC)
13821386
ZEND_ME(Redis, slowlog, arginfo_class_Redis_slowlog, ZEND_ACC_PUBLIC)
13831387
ZEND_ME(Redis, sort, arginfo_class_Redis_sort, ZEND_ACC_PUBLIC)

0 commit comments

Comments
 (0)