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

Skip to content

Commit 8cb6dd1

Browse files
Refactor MGET command.
1 parent 121d330 commit 8cb6dd1

3 files changed

Lines changed: 36 additions & 46 deletions

File tree

redis.c

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -894,52 +894,8 @@ PHP_METHOD(Redis, decrBy){
894894

895895
/* {{{ proto array Redis::mget(array keys)
896896
*/
897-
PHP_METHOD(Redis, mget)
898-
{
899-
zval *object, *z_args, *z_ele;
900-
HashTable *hash;
901-
RedisSock *redis_sock;
902-
smart_string cmd = {0};
903-
int arg_count;
904-
905-
/* Make sure we have proper arguments */
906-
if(zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oa",
907-
&object, redis_ce, &z_args) == FAILURE) {
908-
RETURN_FALSE;
909-
}
910-
911-
/* We'll need the socket */
912-
if ((redis_sock = redis_sock_get(object, 0)) == NULL) {
913-
RETURN_FALSE;
914-
}
915-
916-
/* Grab our array */
917-
hash = Z_ARRVAL_P(z_args);
918-
919-
/* We don't need to do anything if there aren't any keys */
920-
if((arg_count = zend_hash_num_elements(hash)) == 0) {
921-
RETURN_FALSE;
922-
}
923-
924-
/* Build our command header */
925-
redis_cmd_init_sstr(&cmd, arg_count, "MGET", 4);
926-
927-
/* Iterate through and grab our keys */
928-
ZEND_HASH_FOREACH_VAL(hash, z_ele) {
929-
zend_string *zstr = zval_get_string(z_ele);
930-
redis_cmd_append_sstr_key(&cmd, ZSTR_VAL(zstr), ZSTR_LEN(zstr), redis_sock, NULL);
931-
zend_string_release(zstr);
932-
} ZEND_HASH_FOREACH_END();
933-
934-
/* Kick off our command */
935-
REDIS_PROCESS_REQUEST(redis_sock, cmd.c, cmd.len);
936-
if (IS_ATOMIC(redis_sock)) {
937-
if(redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAM_PASSTHRU,
938-
redis_sock, NULL, NULL) < 0) {
939-
RETURN_FALSE;
940-
}
941-
}
942-
REDIS_PROCESS_RESPONSE(redis_sock_read_multibulk_reply);
897+
PHP_METHOD(Redis, mget) {
898+
REDIS_PROCESS_CMD(mget, redis_sock_read_multibulk_reply);
943899
}
944900

945901
/* {{{ proto boolean Redis::exists(string $key, string ...$more_keys)

redis_commands.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,6 +2273,37 @@ int redis_set_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
22732273
return SUCCESS;
22742274
}
22752275

2276+
/* MGET */
2277+
int redis_mget_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
2278+
char **cmd, int *cmd_len, short *slot, void **ctx)
2279+
{
2280+
smart_string cmdstr = {0};
2281+
HashTable *keys = NULL;
2282+
zval *zkey;
2283+
2284+
/* RedisCluster has a custom MGET implementation */
2285+
ZEND_ASSERT(slot == NULL);
2286+
2287+
ZEND_PARSE_PARAMETERS_START(1, 1)
2288+
Z_PARAM_ARRAY_HT(keys)
2289+
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
2290+
2291+
if (zend_hash_num_elements(keys) == 0)
2292+
return FAILURE;
2293+
2294+
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, zend_hash_num_elements(keys), "MGET");
2295+
2296+
ZEND_HASH_FOREACH_VAL(keys, zkey) {
2297+
ZVAL_DEREF(zkey);
2298+
redis_cmd_append_sstr_key_zval(&cmdstr, zkey, redis_sock, slot);
2299+
} ZEND_HASH_FOREACH_END();
2300+
2301+
*cmd = cmdstr.c;
2302+
*cmd_len = cmdstr.len;
2303+
2304+
return SUCCESS;
2305+
}
2306+
22762307
int
22772308
redis_getex_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
22782309
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
@@ -212,6 +212,9 @@ int redis_hincrbyfloat_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
212212
int redis_hmget_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
213213
char **cmd, int *cmd_len, short *slot, void **ctx);
214214

215+
int redis_mget_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
216+
char **cmd, int *cmd_len, short *slot, void **ctx);
217+
215218
int redis_hmset_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
216219
char **cmd, int *cmd_len, short *slot, void **ctx);
217220

0 commit comments

Comments
 (0)