Phpredis: RPUSH values from an array

Created on 5 Aug 2013  路  6Comments  路  Source: phpredis/phpredis

I need to RPUSH values from an array with a variable number of elements. Currently phpredis rpush doesn't accept arrays, so I'm left with:
1) Rpushing each element of the array individually producing lots of TCP requests.
2) Using php call_user_func_array function. But it is considered 3 times as slow as direct function calling and I'll have to prepend the list name to the array which will cause shifting of all the elements.

It would be nice if phpredis rpush accepted arays or (as a temporary alternative) had a function to execute raw commands.

Most helpful comment

Again to help others, you should be able to avoid the call_user_func_array by using argument unpacking.

$redis->rpush('x', ...$a);

All 6 comments

Its useful to support rpush array variable.
Both call_user_func_array & exec long command has a low efficiency.

It makes sense only when serializer is disabled

I don't want doing this because it will change behavior according to serializer.
I made some tests and call_user_func_array works fine.

for ($i = 0; $i < 1000; ++$i) {
    $redis->rpush('x', $i);
}

0.0740sec

$a = array();
for ($i = 0; $i < 1000; ++$i) {
    $a[] = $i;
}
call_user_func_array(array($redis, 'rpush'), $a);

0.0015sec

$a = array();
for ($i = 0; $i < 1000; ++$i) {
$a[] = $i;
}
call_user_func_array(array($redis, 'rpush'), $a);

Unless I'm missing something, doesn't the first element of $a need to be set to 'x' - i.e. the key?

(sorry to resurrect a long dead issue, but I found it via Google and thought it may help others).

Again to help others, you should be able to avoid the call_user_func_array by using argument unpacking.

$redis->rpush('x', ...$a);

@jrchamp - I didn't even know that was a thing in PHP! You learn everything every day! Although it appears its in PHP 5.6+, so no use for us (at the moment, that'll change very soon though, finally!)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lyrixx picture lyrixx  路  4Comments

xkenner picture xkenner  路  3Comments

zacharyhamm picture zacharyhamm  路  3Comments

RobChen picture RobChen  路  4Comments

btrazzini picture btrazzini  路  6Comments