8000 Allow integer keys for MSET · devsnippet/phpredis@f295c1c · GitHub
[go: up one dir, main page]

Skip to content

Commit f295c1c

Browse files
Allow integer keys for MSET
1 parent 21d6614 commit f295c1c

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

redis.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3472,23 +3472,27 @@ generic_mset(INTERNAL_FUNCTION_PARAMETERS, char *kw, void (*fun)(INTERNAL_FUNCTI
34723472
int type;
34733473
zval **z_value_pp;
34743474
int val_free, key_free;
3475+
char buf[32];
34753476

34763477
type = zend_hash_get_current_key_ex(keytable, &key, &key_len, &idx, 0, NULL);
34773478
if(zend_hash_get_current_data(keytable, (void**)&z_value_pp) == FAILURE) {
34783479
continue; /* this should never happen, according to the PHP people. */
34793480
}
34803481

3481-
if(type != HASH_KEY_IS_STRING) { /* ignore non-string keys */
3482-
continue;
3482+
// If the key isn't a string, use the index value returned when grabbing the
3483+
// key. We typecast to long, because they could actually be negative.
3484+
if(type != HASH_KEY_IS_STRING) {
3485+
// Create string representation of our index
3486+
key_len = snprintf(buf, sizeof(buf), "%ld", (long)idx);
3487+
key = (char*)buf;
3488+
} else if(key_len > 0) {
3489+
// When not an integer key, the length will include the \0
3490+
key_len--;
34833491
}
34843492

34853493
if(step == 0)
34863494
argc++; /* found a valid arg */
34873495

3488-
if(key_len > 0) { /* string lengths include \0 when taken from array keys */
3489-
key_len--;
3490-
}
3491-
34923496
val_free = redis_serialize(redis_sock, *z_value_pp, &val, &val_len TSRMLS_CC);
34933497
key_free = redis_key_prefix(redis_sock, &key, (int*)&key_len TSRMLS_CC);
34943498

tests/TestRedis.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,6 +1599,26 @@ public function testMset() {
15991599
$this->assertEquals($this->redis->mget(array('x', 'y', 'z')), array('a', 'b', 'c')); // check x y z
16001600

16011601
$this->assertFalse($this->redis->mset(array())); // set ø → FALSE
1602+
1603+
1604+
/*
1605+
* Integer keys
1606+
*/
1607+
1608+
// No prefix
1609+
$set_array = Array(-1 => 'neg1', -2 => 'neg2', -3 => 'neg3', 1 => 'one', 2 => 'two', '3' => 'three');
1610+
$this->redis->delete(array_keys($set_array));
1611+
$this->assertTrue($this->redis->mset($set_array));
1612+
$this->assertEquals($this->redis->mget(array_keys($set_array)), array_values($set_array));
1613+
$this->redis->delete(array_keys($set_array));
1614+
1615+
// With a prefix
1616+
$this->redis->setOption(Redis::OPT_PREFIX, 'pfx:');
1617+
$this->redis->delete(array_keys($set_array));
1618+
$this->assertTrue($this->redis->mset($set_array));
1619+
$this->assertEquals($this->redis->mget(array_keys($set_array)), array_values($set_array));
1620+
$this->redis->delete(array_keys($set_array));
1621+
$this->redis->setOption(Redis::OPT_PREFIX, '');
16021622
}
16031623

16041624
public function testMsetNX() {

0 commit comments

Comments
 (0)
0