Skip to content

Commit 6ea7695

Browse files
lenn0xAndrei Zmievski
authored andcommitted
Added preserve_order (boolean) to getMulti. This will return an assoc array back in same order as the keys were requested. If the value is not found, a NULL is set for that key.
Signed-off-by: Andrei Zmievski <[email protected]>
1 parent 7edcdf9 commit 6ea7695

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

memcached-api.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ public function get( $key, &$cas_token = null, $cache_cb = null ) {}
7979

8080
public function getByKey( $server_key, $key, $cache_cb = null ) {}
8181

82-
public function getMulti( array $keys, &$cas_tokens = null ) {}
82+
public function getMulti( array $keys, &$cas_tokens = null, $preserve_order = false ) {}
8383

84-
public function getMultiByKey( $server_key, array $keys, &$cas_tokens = null ) {}
84+
public function getMultiByKey( $server_key, array $keys, &$cas_tokens = null, $preserve_order = false ) {}
8585

8686
public function getDelayed( array $keys, $with_cas = null, $value_cb = null ) {}
8787

php_memcached.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -488,28 +488,30 @@ static void php_memc_getMulti_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_ke
488488
zval *cas_tokens = NULL;
489489
uint64_t orig_cas_flag;
490490
zval *value;
491+
zend_bool preserve_order = 0;
491492
int i = 0;
492493
memcached_result_st result;
493494
memcached_return status = MEMCACHED_SUCCESS;
494495
MEMC_METHOD_INIT_VARS;
495496

496497
if (by_key) {
497-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|z", &server_key,
498-
&server_key_len, &keys, &cas_tokens) == FAILURE) {
498+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|zb", &server_key,
499+
&server_key_len, &keys, &cas_tokens,&preserve_order) == FAILURE) {
499500
return;
500501
}
501502
} else {
502-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|z", &keys, &cas_tokens) == FAILURE) {
503+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|zb", &keys, &cas_tokens, &preserve_order) == FAILURE) {
503504
return;
504505
}
505506
}
506-
507+
507508
MEMC_METHOD_FETCH_OBJECT;
508509
MEMC_G(rescode) = MEMCACHED_SUCCESS;
509510

510511
num_keys = zend_hash_num_elements(Z_ARRVAL_P(keys));
511512
mkeys = safe_emalloc(num_keys, sizeof(char *), 0);
512513
mkeys_len = safe_emalloc(num_keys, sizeof(size_t), 0);
514+
array_init(return_value);
513515

514516
/*
515517
* Create the array of keys for libmemcached. If none of the keys were valid
@@ -522,6 +524,9 @@ static void php_memc_getMulti_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_ke
522524
if (Z_TYPE_PP(entry) == IS_STRING && Z_STRLEN_PP(entry) > 0) {
523525
mkeys[i] = Z_STRVAL_PP(entry);
524526
mkeys_len[i] = Z_STRLEN_PP(entry);
527+
if(preserve_order) {
528+
add_assoc_null_ex(return_value, mkeys[i], mkeys_len[i]+1);
529+
}
525530
i++;
526531
}
527532
}
@@ -568,7 +573,7 @@ static void php_memc_getMulti_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_ke
568573
zval_dtor(cas_tokens);
569574
array_init(cas_tokens);
570575
}
571-
array_init(return_value);
576+
572577
status = MEMCACHED_SUCCESS;
573578
memcached_result_create(i_obj->memc, &result);
574579
while ((memcached_fetch_result(i_obj->memc, &result, &status)) != NULL) {
@@ -2443,12 +2448,14 @@ ZEND_END_ARG_INFO()
24432448
ZEND_BEGIN_ARG_INFO_EX(arginfo_getMulti, 0, 0, 1)
24442449
ZEND_ARG_ARRAY_INFO(0, keys, 0)
24452450
ZEND_ARG_INFO(1, cas_tokens)
2451+
ZEND_ARG_INFO(0, preserve_order)
24462452
ZEND_END_ARG_INFO()
24472453

24482454
ZEND_BEGIN_ARG_INFO_EX(arginfo_getMultiByKey, 0, 0, 2)
24492455
ZEND_ARG_INFO(0, server_key)
24502456
ZEND_ARG_ARRAY_INFO(0, keys, 0)
24512457
ZEND_ARG_INFO(1, cas_tokens)
2458+
ZEND_ARG_INFO(0, preserve_order)
24522459
ZEND_END_ARG_INFO()
24532460

24542461
ZEND_BEGIN_ARG_INFO_EX(arginfo_getDelayed, 0, 0, 1)

0 commit comments

Comments
 (0)