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

Skip to content

Commit f9bcff0

Browse files
author
Nicolas Favre-Felix
committed
Added MSET, part of redis 1.1
1 parent 0b3c0f1 commit f9bcff0

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

php_redis.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ PHP_METHOD(Redis, ttl);
7979
PHP_METHOD(Redis, info);
8080
PHP_METHOD(Redis, select);
8181
PHP_METHOD(Redis, move);
82+
PHP_METHOD(Redis, mset);
8283

8384
#ifdef PHP_WIN32
8485
#define PHP_REDIS_API __declspec(dllexport)

redis.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,14 @@ zend_function_entry redis_functions[] = {
9393
PHP_ME(Redis, select, NULL, ZEND_ACC_PUBLIC)
9494
PHP_ME(Redis, move, NULL, ZEND_ACC_PUBLIC)
9595

96+
/* 1.1 */
97+
PHP_ME(Redis, mset, NULL, ZEND_ACC_PUBLIC)
98+
99+
/* aliases */
96100
PHP_MALIAS(Redis, open, connect, NULL, ZEND_ACC_PUBLIC)
97101
PHP_MALIAS(Redis, lLen, lSize, NULL, ZEND_ACC_PUBLIC)
98102
PHP_MALIAS(Redis, sMembers, sGetMembers, NULL, ZEND_ACC_PUBLIC)
103+
PHP_MALIAS(Redis, mget, getMultiple, NULL, ZEND_ACC_PUBLIC)
99104
{NULL, NULL, NULL}
100105
};
101106

@@ -2416,4 +2421,79 @@ PHP_METHOD(Redis, move) {
24162421
redis_1_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock TSRMLS_CC);
24172422
}
24182423
/* }}} */
2424+
2425+
/* {{{ proto bool Redis::mset(array (key => value, ...))
2426+
*/
2427+
PHP_METHOD(Redis, mset) {
2428+
zval *object;
2429+
RedisSock *redis_sock;
2430+
2431+
char *cmd;
2432+
int cmd_len;
2433+
zval *z_array;
2434+
2435+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oa",
2436+
&object, redis_ce, &z_array) == FAILURE) {
2437+
RETURN_FALSE;
2438+
}
2439+
2440+
if (redis_sock_get(object, &redis_sock TSRMLS_CC) < 0) {
2441+
RETURN_FALSE;
2442+
}
2443+
2444+
if(zend_hash_num_elements(Z_ARRVAL_P(z_array)) == 0) {
2445+
RETURN_FALSE;
2446+
}
2447+
2448+
cmd_len = redis_cmd_format(&cmd, "*%d\r\n", 1 + 2 * zend_hash_num_elements(Z_ARRVAL_P(z_array)));
2449+
2450+
if (redis_sock_write(redis_sock, cmd, cmd_len) < 0) {
2451+
efree(cmd);
2452+
RETURN_FALSE;
2453+
}
2454+
efree(cmd);
2455+
2456+
if (redis_sock_write(redis_sock, "$4\r\nMSET\r\n", 10) < 0) {
2457+
RETURN_FALSE;
2458+
}
2459+
2460+
HashTable *keytable = Z_ARRVAL_P(z_array);
2461+
for(zend_hash_internal_pointer_reset(keytable);
2462+
zend_hash_has_more_elements(keytable) == SUCCESS;
2463+
zend_hash_move_forward(keytable)) {
2464+
2465+
char *key, *val;
2466+
int key_len, val_len;
2467+
unsigned long idx;
2468+
int type;
2469+
zval **z_value_pp;
2470+
2471+
type = zend_hash_get_current_key_ex(keytable, &key, &key_len, &idx, 0, NULL);
2472+
if(zend_hash_get_current_data(keytable, (void**)&z_value_pp) == FAILURE) {
2473+
continue; /* this should never happen, according to the PHP people. */
2474+
}
2475+
2476+
if(type != HASH_KEY_IS_STRING) { /* ignore non-string keys */
2477+
continue;
2478+
}
2479+
2480+
val = Z_STRVAL_PP(z_value_pp);
2481+
val_len = Z_STRLEN_PP(z_value_pp);
2482+
2483+
if(key_len > 0) {
2484+
key_len--;
2485+
}
2486+
2487+
cmd_len = redis_cmd_format(&cmd, "$%d\r\n%s\r\n$%d\r\n%s\r\n",
2488+
key_len, key, key_len,
2489+
val_len, val, val_len);
2490+
2491+
redis_sock_write(redis_sock, cmd, cmd_len);
2492+
efree(cmd);
2493+
}
2494+
2495+
redis_boolean_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock TSRMLS_CC);
2496+
}
2497+
/* }}} */
2498+
24192499
/* vim: set tabstop=4 expandtab: */

tests/TestRedis.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,19 @@ public function testSelect() {
12541254
$this->assertFalse($this->redis->select(-1));
12551255
$this->assertTrue($this->redis->select(0));
12561256
}
1257+
1258+
public function testMset() {
1259+
$this->redis->delete('x', 'y', 'z'); // remove x y z
1260+
$this->assertTrue($this->redis->mset(array('x' => 'a', 'y' => 'b', 'z' => 'c'))); // set x y z
1261+
1262+
$this->assertEquals($this->redis->mget(array('x', 'y', 'z')), array('a', 'b', 'c')); // check x y z
1263+
1264+
$this->redis->delete('x'); // delete just x
1265+
$this->assertTrue($this->redis->mset(array('x' => 'a', 'y' => 'b', 'z' => 'c'))); // set x y z
1266+
$this->assertEquals($this->redis->mget(array('x', 'y', 'z')), array('a', 'b', 'c')); // check x y z
1267+
1268+
$this->assertFalse($this->redis->mset(array())); // set ø → FALSE
1269+
}
12571270
}
12581271

12591272
?>

0 commit comments

Comments
 (0)