Skip to content

Commit 958f1cb

Browse files
committed
Added HMSET.
1 parent 7c8e45f commit 958f1cb

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed

README.markdown

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1601,5 +1601,20 @@ Increments the value of a member from a hash by a given amount.
16011601
<pre>
16021602
$redis->delete('h');
16031603
$redis->hIncrBy('h', 'x', 2); /* returns 2: h[x] = 2 now. */
1604-
$redis->zIncrBy('h', 'x', 1); /* h[x] ← 2 + 1. Returns 3 */
1604+
$redis->hIncrBy('h', 'x', 1); /* h[x] ← 2 + 1. Returns 3 */
1605+
</pre>
1606+
1607+
## hMset
1608+
##### Description
1609+
Fills in a whole hash.
1610+
##### Parameters
1611+
*key*
1612+
*members*: key → value array
1613+
##### Return value
1614+
*BOOL*
1615+
##### Examples
1616+
<pre>
1617+
$redis->delete('user:1');
1618+
$redis->hMset('user:1', array('name' => 'Joe', 'salary' => 2000));
1619+
$redis->hIncrBy('user:1', 'salary', 100); // Joe earns 100 more now.
16051620
</pre>

php_redis.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ PHP_METHOD(Redis, hVals);
104104
PHP_METHOD(Redis, hGetAll);
105105
PHP_METHOD(Redis, hExists);
106106
PHP_METHOD(Redis, hIncrBy);
107+
PHP_METHOD(Redis, hMset);
107108

108109
PHP_METHOD(Redis, multi);
109110
PHP_METHOD(Redis, discard);

redis.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ static zend_function_entry redis_functions[] = {
130130
PHP_ME(Redis, hGetAll, NULL, ZEND_ACC_PUBLIC)
131131
PHP_ME(Redis, hExists, NULL, ZEND_ACC_PUBLIC)
132132
PHP_ME(Redis, hIncrBy, NULL, ZEND_ACC_PUBLIC)
133+
PHP_ME(Redis, hMset, NULL, ZEND_ACC_PUBLIC)
133134

134135
PHP_ME(Redis, multi, NULL, ZEND_ACC_PUBLIC)
135136
PHP_ME(Redis, discard, NULL, ZEND_ACC_PUBLIC)
@@ -3723,6 +3724,94 @@ PHP_METHOD(Redis, hIncrBy)
37233724

37243725
}
37253726

3727+
PHP_METHOD(Redis, hMset)
3728+
{
3729+
zval *object;
3730+
RedisSock *redis_sock;
3731+
char *key = NULL, *cmd, *member;
3732+
int key_len, member_len, cmd_len;
3733+
zval *z_hash, *data;
3734+
HashTable *ht_hash;
3735+
HashPosition pointer;
3736+
int i;
3737+
int element_count = 2;
3738+
char *old_cmd = NULL;
3739+
3740+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osa",
3741+
&object, redis_ce,
3742+
&key, &key_len, &z_hash) == FAILURE) {
3743+
RETURN_FALSE;
3744+
}
3745+
3746+
if (redis_sock_get(object, &redis_sock TSRMLS_CC) < 0) {
3747+
RETURN_FALSE;
3748+
}
3749+
3750+
ht_hash = Z_ARRVAL_P(z_hash);
3751+
3752+
if (zend_hash_num_elements(ht_hash) == 0) {
3753+
RETURN_FALSE;
3754+
}
3755+
3756+
cmd_len = redis_cmd_format(&cmd,
3757+
"$5" _NL "HMSET" _NL
3758+
"$%d" _NL "%s" _NL
3759+
, key_len, key, key_len);
3760+
3761+
/* looping on each item of the array */
3762+
for(i =0, zend_hash_internal_pointer_reset(ht_hash);
3763+
zend_hash_has_more_elements(ht_hash) == SUCCESS;
3764+
i++, zend_hash_move_forward(ht_hash)) {
3765+
3766+
char *hkey;
3767+
unsigned int hkey_len;
3768+
unsigned long idx;
3769+
int type;
3770+
zval *z_value, **z_value_p;
3771+
3772+
type = zend_hash_get_current_key_ex(ht_hash, &hkey, &hkey_len, &idx, 0, NULL);
3773+
3774+
if(zend_hash_get_current_data(ht_hash, (void**)&z_value_p) == FAILURE) {
3775+
continue; /* this should never happen */
3776+
}
3777+
3778+
if(type != HASH_KEY_IS_STRING) {
3779+
continue;
3780+
}
3781+
element_count += 2;
3782+
3783+
/* key is set. */
3784+
if (Z_TYPE_PP(z_value_p) != IS_STRING) {
3785+
convert_to_string(*z_value_p);
3786+
}
3787+
if(*cmd) {
3788+
old_cmd = cmd;
3789+
}
3790+
cmd_len = redis_cmd_format(&cmd, "%s"
3791+
"$%d" _NL "%s" _NL
3792+
"$%d" _NL "%s" _NL
3793+
, cmd, cmd_len
3794+
, hkey_len-1, hkey, hkey_len-1
3795+
, Z_STRLEN_PP(z_value_p), Z_STRVAL_PP(z_value_p), Z_STRLEN_PP(z_value_p));
3796+
if(old_cmd) {
3797+
efree(old_cmd);
3798+
}
3799+
}
3800+
3801+
old_cmd = cmd;
3802+
cmd_len = redis_cmd_format(&cmd, "*%d" _NL "%s"
3803+
, element_count, cmd, cmd_len);
3804+
efree(old_cmd);
3805+
3806+
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
3807+
IF_ATOMIC() {
3808+
redis_boolean_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL TSRMLS_CC);
3809+
}
3810+
REDIS_PROCESS_RESPONSE(redis_boolean_response);
3811+
}
3812+
3813+
3814+
37263815
PHPAPI int redis_response_enqueued(RedisSock *redis_sock TSRMLS_DC) {
37273816

37283817
char *response;

tests/TestRedis.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,15 @@ public function testHashes() {
15141514

15151515
$this->redis->hSet('h', 'y', 'not-a-number');
15161516
$this->assertTrue(FALSE === $this->redis->hIncrBy('h', 'y', 1));
1517+
1518+
// hmset
1519+
$this->redis->delete('h');
1520+
$this->assertTrue(TRUE === $this->redis->hMset('h', array('x' => 123, 'y' => 456, 'z' => 'abc')));
1521+
$this->assertTrue('123' === $this->redis->hGet('h', 'x'));
1522+
$this->assertTrue('456' === $this->redis->hGet('h', 'y'));
1523+
$this->assertTrue('abc' === $this->redis->hGet('h', 'z'));
1524+
$this->assertTrue(FALSE === $this->redis->hGet('h', 't'));
1525+
15171526
}
15181527

15191528
public function testMultiExec() {

0 commit comments

Comments
 (0)