Skip to content

Commit b9788ef

Browse files
author
andrei
committed
Implement addServers() method.
1 parent 62abe67 commit b9788ef

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

Diff for: memcached-api.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ class Memcached {
3636

3737
const OPT_DISTRIBUTION_CONSISTENT;
3838

39-
const OPT_BUFER_REQUESTS;
39+
const OPT_LIBKETAMA_COMPATIBLE;
40+
41+
const OPT_BUFFER_REQUESTS;
4042

4143
const OPT_BINARY_PROTOCOL;
4244

@@ -129,7 +131,9 @@ public function getOption( $option ) {}
129131

130132
public function setOption( $option, $value ) {}
131133

132-
public function addServer( $host, $port, $weight = 0 );
134+
public function addServer( $host, $port, $weight = 0 ) {}
135+
136+
public function addServers( array $servers ) {}
133137

134138
public function getServerList( ) {}
135139

Diff for: php_memcached.c

+71
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,71 @@ PHP_METHOD(Memcached, addServer)
13021302
}
13031303
/* }}} */
13041304

1305+
/* {{{ Memcached::addServers(array servers)
1306+
Adds the given memcache servers to the server list */
1307+
PHP_METHOD(Memcached, addServers)
1308+
{
1309+
zval *servers;
1310+
zval **entry;
1311+
zval **z_host, **z_port, **z_weight = NULL;
1312+
uint32_t weight = 0;
1313+
int entry_size, i = 0;
1314+
memcached_server_st *list = NULL;
1315+
memcached_return status;
1316+
MEMC_METHOD_INIT_VARS;
1317+
1318+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &servers) == FAILURE) {
1319+
return;
1320+
}
1321+
1322+
MEMC_METHOD_FETCH_OBJECT;
1323+
MEMC_G(rescode) = MEMCACHED_SUCCESS;
1324+
1325+
for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(servers)), i = 0;
1326+
zend_hash_get_current_data(Z_ARRVAL_P(servers), (void **)&entry) == SUCCESS;
1327+
zend_hash_move_forward(Z_ARRVAL_P(servers)), i++) {
1328+
1329+
if (Z_TYPE_PP(entry) != IS_ARRAY) {
1330+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "server list entry #%d is not an array", i+1);
1331+
continue;
1332+
}
1333+
1334+
entry_size = zend_hash_num_elements(Z_ARRVAL_PP(entry));
1335+
1336+
if (entry_size > 1) {
1337+
zend_hash_index_find(Z_ARRVAL_PP(entry), 0, (void **)&z_host);
1338+
zend_hash_index_find(Z_ARRVAL_PP(entry), 1, (void **)&z_port);
1339+
convert_to_string_ex(z_host);
1340+
convert_to_long_ex(z_port);
1341+
1342+
weight = 0;
1343+
if (entry_size > 2) {
1344+
zend_hash_index_find(Z_ARRVAL_PP(entry), 2, (void **)&z_weight);
1345+
convert_to_long_ex(z_weight);
1346+
weight = Z_LVAL_PP(z_weight);
1347+
}
1348+
1349+
list = memcached_server_list_append_with_weight(list, Z_STRVAL_PP(z_host),
1350+
Z_LVAL_PP(z_port), weight, &status);
1351+
1352+
if (php_memc_handle_error(status TSRMLS_CC) == 0) {
1353+
continue;
1354+
}
1355+
}
1356+
1357+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "could not add entry #%d to the server list", i+1);
1358+
}
1359+
1360+
status = memcached_server_push(i_obj->memc, list);
1361+
memcached_server_list_free(list);
1362+
if (php_memc_handle_error(status TSRMLS_CC) < 0) {
1363+
RETURN_FALSE;
1364+
}
1365+
1366+
RETURN_TRUE;
1367+
}
1368+
/* }}} */
1369+
13051370
/* {{{ Memcached::getServerList()
13061371
Returns the list of the memcache servers in use */
13071372
PHP_METHOD(Memcached, getServerList)
@@ -2362,6 +2427,11 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_addServer, 0, 0, 2)
23622427
ZEND_ARG_INFO(0, weight)
23632428
ZEND_END_ARG_INFO()
23642429

2430+
static
2431+
ZEND_BEGIN_ARG_INFO(arginfo_addServers, 0)
2432+
ZEND_ARG_ARRAY_INFO(0, servers, 0)
2433+
ZEND_END_ARG_INFO()
2434+
23652435
static
23662436
ZEND_BEGIN_ARG_INFO(arginfo_getServerList, 0)
23672437
ZEND_END_ARG_INFO()
@@ -2425,6 +2495,7 @@ static zend_function_entry memcached_class_methods[] = {
24252495
MEMC_ME(decrement, arginfo_decrement)
24262496

24272497
MEMC_ME(addServer, arginfo_addServer)
2498+
MEMC_ME(addServers, arginfo_addServers)
24282499
MEMC_ME(getServerList, arginfo_getServerList)
24292500
MEMC_ME(getServerByKey, arginfo_getServerByKey)
24302501

0 commit comments

Comments
 (0)